在 Spring MVC 中将参数从 JSP 传递到控制器 [英] Passing parameters from JSP to Controller in Spring MVC

查看:47
本文介绍了在 Spring MVC 中将参数从 JSP 传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Spring MVC 注释控制器的示例项目.到目前为止,我在网上找到的所有示例都将 JSP 绑定到特定模型,并且控制器使用 @ModelAttribute 在处理程序方法中检索模型对象.

I am trying out a sample project using Spring MVC annotated Controllers. All the examples I have found online so far bind the JSP to a particular model and the controller uses @ModelAttribute to retrive the model object in the handler method.

如何将其他参数(不存在于模型对象中)从 JSP 传递到控制器?我是否使用 JavaScript 来执行此操作?也有人可以澄清 HttpServletRequest 对象应该用于什么.

How do I go about passing other parameters (not present in the Model object) from the JSP to Controller? Do I use JavaScript to do this? Also can someone clarify what the HttpServletRequest object should be used for.

谢谢.

推荐答案

只需从 jsp 输入标签中删除路径",并使用 HttpServletRequest 检索剩余数据.

Just remove the "path" from the jsp input tag and use HttpServletRequest to retrieve the remaining data.

例如我有一个像

public class SomeData {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

然后在jsp中我将有额外的数据字段以普通的html标签发送

Then in the jsp i will have the additional data fields to be send in normal html tag

<form:form method="post" action="somepage" commandName="somedata">
    <table>
    <tr>
        <td>name</td>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <td>age</td>
        <!--Notice, this is normal html tag, will not be bound to an object -->
        <td><input name="age" type="text"/></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="send"/>
        </td>
    </tr>
</table>
</form:form>

请注意,somedata bean 具有年龄不是的名称字段.所以添加年龄字段时没有路径".如果没有路径属性,对象属性将不会绑定到该字段.

Notice, the somedata bean has the name field the age is not. So the age field is added without "path". Without the path attribute the object property wont be bound to this field.

在控制器上,我将不得不使用 HttpServletRequest 之类的,

on the Controller i will have to use the HttpServletRequest like,

@RequestMapping("/somepage")
public String someAction(@ModelAttribute("somedata") SomeData data, Map<String, Object> map,
                                HttpServletRequest request) {

       System.out.println("Name=" + data.getName() + " age=" + request.getParameter("age"));

       /* do some process and send back the data */
        map.put("somedata", data);
        map.put("age", request.getParameter("age"));

        return "somepage";
   }

在访问视图上的数据时,

while accessing the data on the view,

<table>
    <tr>
        <td>name</td>
        <td>${somedata.name}</td>
    </tr>
    <tr>
        <td>age</td>
        <td>${age}</td>
    </tr>
 </table>

somedata 是提供 name 属性的 bean,并且年龄是由控制器显式设置的属性.

somedata is the bean which provides the name property and age is explicitly set attribute by the controller.

这篇关于在 Spring MVC 中将参数从 JSP 传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆