2013-02-04 37 views
6

我是Spring的新手,我遇到了问题。我有一个用于向控制器发送信息的表单。我并不需要或想要有一颗豆备份形式,所以我留在空白表单的commandName属性是这样的:不带命令的弹簧形式

<form:form action="getReportFile.html" method="post"> 
      <table> 
       <tr> 
        <td><form:label path="field1">Field1:</form:label></td> 
       </tr> 
       <tr> 
        <td><form:select path="field1" items="${FieldMap}" />       
        </td> 
       </tr> 
       <tr> 
        <td><form:label path="field2">Field2:</form:label></td> 
       </tr> 
       <tr> 
        <td><form:input path="field2"/></td> 
       </tr> 
       <tr> 
        <td><input type="submit" value="Submit" /></td> 
       </tr> 
      </table> 
     </form:form> 

,我发现了以下错误:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute 

我可以看到here,当你不给commandName的值时,它使用默认的'command',但是,然后,我需要配置其他的东西吗?我应该在dispatcher-servlet.xml中加入'command'豆吗?那个豆怎么样?

我只是想要一个表单发送信息给控制器。 我真的必须创建一个bean来支持它吗?

+0

嗯,其实问题是这个复制品,因为这个之前问过:) @SotiriosDelimanolis –

回答

8

如果你根本不需要命令对象,那么就避免使用Spring表单,并简单地使用HTML表单。

所以,改变

<form:form action="getReportFile.html" method="post"> 
    . 
    . 
    . 
</form:form> 

<form action="getReportFile.html" method="post"> 
    . 
    . 
    . 
</form> 

Command对象确实不是强制性的。只有使用以下库的<form:form></form:form>这样的Spring形式才是必需的。

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

您可以选择使用request.getParameter("paramName")方法,如果您使用HTML表单来接收请求参数。


if you don't have a form backing bean, you can't use the Spring tag since it does require one! Your "path" attribute on that tag is supposed to specify the path to the model bean's property for data binding.

http://forum.springsource.org/showthread.php?83532-how-to-have-form-without-command-object&p=279807#post279807

+0

谢谢!真的有帮助 –

+0

不客气。如果您需要使用类似'HibernateValidator'的服务器端验证,那么您的回答中必须具有@jfrank所描述的支持bean。曾几何时,我也需要避免支持bean(支持bean实际上不是必需的,因为JSP页面上的所有内容都只能由Spring JSON处理)。 – Lion

+1

这么简单,但显然非常罕见。每个教程都使用form:form,这会带来很多不经常被解释的绑定包袱。我不认为想要一个与模型无关的表单(例如搜索表单,联系表单)并不少见,但几乎没有教程能够涵盖它。 – mmcrae

3

我不知道是否可以在Spring MVC中创建一个没有支持bean的表单,但是我认为如果不使用bean,你不会得到太多的Spring MVC。那些你正在使用的form标签(比如form:input)绑定到backing bean的属性上,这可以让你附加验证,错误消息,类型转换等等。你的意图是简单地将表单发布到Spring控制器,并使用类似“request.getParameter('field1')”的代码处理服务器上的所有表单域?然后我会建议只使用纯HTML表单。

-1

只需添加CommandName的属性,形成春季标签。

<form:form method="post" action="save.web" commandName="person">