没有CommandName的Spring表单


问题内容

我是Spring的新手,但是有问题。我有一个用于向控制器发送信息的表格。我不需要或不想有一个bean来备份表单,因此我将表单中的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

我在这里可以看到,当您不给commandName它一个值时,它会使用default 'command',但是,
我是否还需要配置其他内容? 我应该'command'在里面放豆dispatcher-servlet.xml吗?那豆怎么样

我只希望有一个表单将信息发送到控制器。 我真的必须创建一个bean来支持它吗?


问题答案:

如果根本不需要命令对象,则避免使用Spring表单,而只需使用HTML表单。

所以,改变

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

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

命令对象确实不是强制性的。仅当您使用Spring的形式(如<form:form></form:form>使用以下库)时,才必须执行此操作。

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

request.getParameter("paramName")如果使用HTML表单,则必须使用方法接收请求参数。


如果您没有表单支持bean,则不能使用Spring标记,因为它确实需要一个!您在该标签上的“ path”属性应该为数据绑定指定模型bean属性的路径。