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

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

问题描述

我正在尝试使用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.

如何将其他参数(不存在于Model对象中)从JSP传递给Controller?我是否使用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输入标记中删除path,然后使用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的名称字段不是年龄。因此,添加年龄字段时没有路径。如果没有path属性,对象属性就不会绑定到这个字段。

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.

在Controller上我将不得不使用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";
   }

访问视图中的数据时,

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

somedata是提供name属性的bean,age是控制器显式设置的属性。

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

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

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