java - 没有 commandName 的 Spring 表单

标签 java spring jakarta-ee spring-mvc

我是 Spring 的新手,遇到了一个问题。我有一个用于向 Controller 发送信息的表格。我不需要也不想让 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

我可以看到 here当您不为 commandName 赋值时,它会使用默认的 'command',但是,我还需要配置其他任何东西吗?我应该在 dispatcher-servlet.xml 中放置一个 'command' bean 吗?那bean怎么会?

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

最佳答案

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

所以,改变

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

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

命令对象确实不是强制性的。仅当您使用像 <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

关于java - 没有 commandName 的 Spring 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14697143/

相关文章:

c# - Java (Android) 的 GUID 等价物

java - Spring JMS ActiveMQ 消息传递咨询主题不起作用

JAVA GC : ParNew (promotion failed) , 并发模式失败

java - 如何调用远程java类(方法)

jakarta-ee - maven-embedded-glassfish-plugin 上的 CDI 注入(inject)失败 -- org.jboss.weld.exceptions.DeploymentException : WELD-001408 Unsatisfied dependencies for type

Java Spring JDBC Oracle 存储过程返回 null

java如何调用外部unix命令

java - 使用 SQLite 填充饼图

java - Spring @Transactional - 隔离、传播

java - Spring jpa实体和动态调度