Spring MVC 中从 JSP 向 Controller 传递参数

发布于 2024-10-30 23:52:45 字数 235 浏览 1 评论 0原文

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

如何将其他参数(模型对象中不存在的参数)从 JSP 传递到控制器?我是否使用 JavaScript 来执行此操作?另外有人可以澄清 HttpServletRequest 对象的用途。

谢谢。

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.

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.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶葆Ⅱㄣ 2024-11-06 23:52:45

只需从 jsp 输入标记中删除“路径”并使用 HttpServletRequest 检索剩余的数据即可。

例如,我有一个像这样的bean,

public class SomeData {
    private String name;

    public String getName() {
        return name;
    }

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

}

然后在jsp中我将有附加的数据字段要在普通的html标签中发送

<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具有名称字段,而年龄则没有。因此添加年龄字段时没有“路径”。如果没有路径属性,对象属性将不会绑定到该字段。

在控制器上,我必须使用 HttpServletRequest 之类的方法,

@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 是由控制器显式设置的属性。

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

For example I have a bean like

public class SomeData {
    private String name;

    public String getName() {
        return name;
    }

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

}

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>

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.

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 is the bean which provides the name property and age is explicitly set attribute by the controller.

涫野音 2024-11-06 23:52:45

如果不想创建另一个类(bean),尽管它应该在那里,那么除了@ModelAttrbute之外,还可以使用@RequestParam

public String someAction(@RequestParam("somedata") String data)
{
------
}

If one doesn't want to create another class (bean) though it should be there, then apart from @ModelAttrbute one can also use @RequestParam.

public String someAction(@RequestParam("somedata") String data)
{
------
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文