如何在 Spring MVC 中使用表单 bean?

发布于 2025-01-08 22:32:29 字数 612 浏览 0 评论 0 原文

我是 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 中做同样的事情?

I'm new to Spring MVC (3.1.1). Coming from a Rails and Struts1 world that is.

In Struts, our FormBeans automatically map to our controllers. For example:

# JS file
ExtJS.ajax({action:'update', value:42});

Then our controller can:

bean.getAction();   // 'update'
bean.getValue();    // 42

We instantiate the bean like:

public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    MyBean bean = (MyBean) form;
    ....
}

Please forgive my ignorance, but how would I do the same thing in Spring MVC?

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

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

发布评论

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

评论(3

一片旧的回忆 2025-01-15 22:32:29

在 jsp 中执行如下操作:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<form:form name="frmFoo" id="frmFoo" action="/FormSubmitUrl" method="POST" modelAttribute="foo">
   <form:select path="myField">
   <form:errors path="myField" />
</form:form>

只需将支持对象添加到控制器方法中的模型中即可:

@RequestMapping(value = "/MyFooForm", method = RequestMethod.GET)
public String getFoo(final Model model)
{
   model.addAttribute("foo", foo);
   return "fooForm.jsp";
}

In jsp do something like this :

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<form:form name="frmFoo" id="frmFoo" action="/FormSubmitUrl" method="POST" modelAttribute="foo">
   <form:select path="myField">
   <form:errors path="myField" />
</form:form>

And just add the backing object to the model in your controller method :

@RequestMapping(value = "/MyFooForm", method = RequestMethod.GET)
public String getFoo(final Model model)
{
   model.addAttribute("foo", foo);
   return "fooForm.jsp";
}
不奢求什么 2025-01-15 22:32:29

我来自 Spring MVC - Rails。我更喜欢 Rails :-)。 Spring MVC 有很多入门教程。但是 spring 有一个很好的教程,你可以遵循。但首先您必须从 spring-dispatch-servlet.xml 开始。您将在那里定义视图、控制器和表单之间的关联。例如在下面的代码片段中:

<bean name="/priceincrease.htm" class="springapp.web.PriceIncreaseFormController">
    <property name="sessionForm" value="true"/>
    <property name="commandName" value="priceIncrease"/>
    <property name="commandClass" value="springapp.service.PriceIncrease"/>
    <property name="validator">
        <bean class="springapp.service.PriceIncreaseValidator"/>
    </property>
    <property name="formView" value="priceincrease"/>
    <property name="successView" value="hello.htm"/>
    <property name="productManager" ref="productManager"/>
</bean

Controller 是 PriceIncreaseFormController,priceIncrease 是等效的命令对象。对应的视图即JSP会有

<表格宽度=“95%”bgcolor=“f8f8ff”边框=“0”cellspacing=“0”cellpadding=“5”>

增加(%): ;


但我想你可能会从我获取代码的地方得到一个完整的图片
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:

<bean name="/priceincrease.htm" class="springapp.web.PriceIncreaseFormController">
    <property name="sessionForm" value="true"/>
    <property name="commandName" value="priceIncrease"/>
    <property name="commandClass" value="springapp.service.PriceIncrease"/>
    <property name="validator">
        <bean class="springapp.service.PriceIncreaseValidator"/>
    </property>
    <property name="formView" value="priceincrease"/>
    <property name="successView" value="hello.htm"/>
    <property name="productManager" ref="productManager"/>
</bean

Controller is the PriceIncreaseFormController and the priceIncrease is the equivalent command object. The corresponding view i.e. JSP will have

<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

<逆流佳人身旁 2025-01-15 22:32:29

我今天根据我继承的项目中的一些代码寻找这个答案。我发现如果您在 multiActionController 上创建控制器操作。表单值可以映射到一个 bean,该 bean 将成为处理程序操作的第三个参数。

就我而言,我有一个像这样的控制器,

public void submit(HttpServletRequest request, 
                    HttpServletResponse response, SomeBean sb) {
{
    //do something with sb - SomeBean
}

我的问题是弄清楚表单中的值如何映射到控制器操作。我的 jsp 表单中根本没有引用 SomeBean。事实证明,Spring 根据控制器操作参数和表单中使用的名称发挥了神奇作用。它基本上将表单中的值映射到一个 bean,该 bean 用作操作方法中的第三个参数。

例如,我的 SomeBean 有一些名为 byday 和 bymonth 的字段。它还具有相应的按日和按月设置器和获取器(即 getBymonth()、setBymonth)。在我的提交操作中,我的提交操作是在提交时映射的,并且我有按天和按月的输入值,如下所示:

<select id="weekly_option" name="byday">
            <option value="MON">Monday</option>
            <option value="TUE">Tuesday</option>
            <option value="WED">Wednesday</option>
            <option value="THU">Thursday</option>
            <option value="FRI">Friday</option>
            <option value="SAT">Saturday</option>
            <option value="SUN">Sunday</option>
          </select>

我必须深入研究映射的具体完成方式。我查看了 MultiActionController 源代码,发现 if 执行了以下操作:

  1. 控制器确定操作中是否有第三个参数(它不能是 HttpSession 类型)。
  2. 然后,MultiActionController 会更新第三个参数 (SomeBean) 的实例。根据 http 请求值,它将所有值从 HttpServleRequest 推送到 SomeBean 对象。

目前看来它只适用于字符串属性。我想如果您想在 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

public void submit(HttpServletRequest request, 
                    HttpServletResponse response, SomeBean sb) {
{
    //do something with sb - SomeBean
}

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:

<select id="weekly_option" name="byday">
            <option value="MON">Monday</option>
            <option value="TUE">Tuesday</option>
            <option value="WED">Wednesday</option>
            <option value="THU">Thursday</option>
            <option value="FRI">Friday</option>
            <option value="SAT">Saturday</option>
            <option value="SUN">Sunday</option>
          </select>

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:

  1. The controller figures out if you have a third parameter in your action (it must not be of the type HttpSession).
  2. The MultiActionController then news up an instance of the third parameter (SomeBean). Based on the http request values it pushes over all the values from the HttpServleRequest over to the SomeBean object.

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.

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