如何在 Spring MVC 中使用表单 bean?
我是 Spring MVC (3.1.1)
的新手。来自 Rails 和 Struts1 的世界。
在 Struts 中,我们的 FormBeans
自动映射到我们的控制器。例如:
# JS file
ExtJS.ajax({action:'update', value:42});
那么我们的控制器可以:
bean.getAction(); // 'update'
bean.getValue(); // 42
我们实例化 bean,如下所示:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
MyBean bean = (MyBean) form;
....
}
请原谅我的无知,但我如何在 Spring MVC 中做同样的事情?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 jsp 中执行如下操作:
只需将支持对象添加到控制器方法中的模型中即可:
In jsp do something like this :
And just add the backing object to the model in your controller method :
我来自 Spring MVC - Rails。我更喜欢 Rails :-)。 Spring MVC 有很多入门教程。但是 spring 有一个很好的教程,你可以遵循。但首先您必须从 spring-dispatch-servlet.xml 开始。您将在那里定义视图、控制器和表单之间的关联。例如在下面的代码片段中:
但我想你可能会从我获取代码的地方得到一个完整的图片
http://static.springsource.org /docs/Spring-MVC-step-by-step/part4.html#step4.5 可以获取 Spring MVC 示例代码库的位置是 https://src.springframework.org/svn/spring-samples/mvc-basic
Am going from, Spring MVC - Rails. I like Rails lot better :-). There are lot of start up tutorials for Spring MVC. But spring has a good tutorial that you can follow. But to begin with you will have to start with the spring-dispatch-servlet.xml. There you will define your association between views, controller and form. For example in this following snippet:
<form:form method="post" commandName="priceIncrease">
<table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
<tr>
<td align="right" width="20%">Increase (%):</td>
<td width="20%">
<form:input path="percentage"/>
</td>
<td width="60%">
<form:errors path="percentage" cssClass="error"/>
</td>
</tr>
</table>
But I think you might get a complete picture from this place where I picked up the code from
http://static.springsource.org/docs/Spring-MVC-step-by-step/part4.html#step4.5 and the place where you can get Spring MVC's example code base is https://src.springframework.org/svn/spring-samples/mvc-basic
我今天根据我继承的项目中的一些代码寻找这个答案。我发现如果您在 multiActionController 上创建控制器操作。表单值可以映射到一个 bean,该 bean 将成为处理程序操作的第三个参数。
就我而言,我有一个像这样的控制器,
我的问题是弄清楚表单中的值如何映射到控制器操作。我的 jsp 表单中根本没有引用 SomeBean。事实证明,Spring 根据控制器操作参数和表单中使用的名称发挥了神奇作用。它基本上将表单中的值映射到一个 bean,该 bean 用作操作方法中的第三个参数。
例如,我的 SomeBean 有一些名为 byday 和 bymonth 的字段。它还具有相应的按日和按月设置器和获取器(即 getBymonth()、setBymonth)。在我的提交操作中,我的提交操作是在提交时映射的,并且我有按天和按月的输入值,如下所示:
我必须深入研究映射的具体完成方式。我查看了 MultiActionController 源代码,发现 if 执行了以下操作:
目前看来它只适用于字符串属性。我想如果您想在 bean 中使用字符串以外的属性,则必须以某种方式扩展 MVC。
I was looking for this answer today based on some code in a project I inherited. What I found was that the if you create a controller action on a multiActionController. The form values can be mapped to a bean that would be the third paarameter on your handler action.
In my case I had a controller like this
My issue was in figuring out how the values in my form were mapped to the controller action. There was no reference at all to the SomeBean within my jsp form. As it turns out Spring works the magic based on the controller action parameters and the names used in the form. It basically maps the values in the form to a bean that is used as the third parameter in the action method.
For example my SomeBean has some fields named byday and bymonth. It also has the corresponding byday and bymonth setters and getters (i.e. getBymonth(), setBymonth). In my for my submit action is mapped on submit and I have input values for byday and by month like so:
I had to do some digging on how exactly the mappings were being done. I peeked into the MultiActionController source code to see that if does the following:
At this point it seems like it only works with properties that are strings. I imagine that you would have to extend MVC somehow if you wanted to work with properties other than strings within your bean.