Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring表单中必须使用commandName属性吗?_Java_Spring Mvc - Fatal编程技术网

Java 在spring表单中必须使用commandName属性吗?

Java 在spring表单中必须使用commandName属性吗?,java,spring-mvc,Java,Spring Mvc,这是我的表格 <form:form name="UserRegistrationForm" id="UserRegistrationForm" method="post" action="user" commandName="userData" autocomplete="off"> ..... </form:form> 我得到下面的错误 Caused by: java.lang.IllegalStateException: Neither BindingResult

这是我的表格

<form:form name="UserRegistrationForm" id="UserRegistrationForm" method="post" action="user" commandName="userData" autocomplete="off">
.....
</form:form>
我得到下面的错误

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141) ~[spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
原因:java.lang.IllegalStateException:bean名称“command”的BindingResult和普通目标对象都不能作为请求属性使用
在org.springframework.web.servlet.support.BindStatus.(BindStatus.java:141)~[spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
如果我在表单标记中添加
commandName=“userData
”属性,它可以正常工作

在spring表单中必须使用commandName属性吗?

根据我的理解,commandName属性仅在开发人员在控制器的modelAttribute(比如userData)下设置了某些内容时才是必需的 并希望不使用前缀userData访问其属性。例如,如果userData包含email字段,那么开发人员可以直接使用path
下的email,前提是表单标记中指定了commandName=“userData”
否则,他可以使用EL,即$userData.email。那么为什么spring在这里需要commandName属性呢?

它有一个默认值,即“command”

如果在控制器中,可以设置“主”窗体属性的名称:

model.addAttribute("command", foo); 

但我认为最好明确地将模型属性和表单属性命名为反映对象类型的东西,在您的例子中是“userData”。

您需要使用commandName来触发表单标记,否则您将得到下面的异常。当您对html元素使用spring标记时,就会发生这种情况 IllegalStateException:BindingResult和普通目标对象“command”都不能作为请求参数使用


另请参见

如果我想在控制器中设置model.addAttribute(“userData”,foo)并通过表达式语言使用它(不使用attribute commandName=“userData”)?
model.addAttribute("command", foo);