Pul*_*kit 91 forms spring-mvc modelattribute
在Spring 3中,我在jsp中的form标签中看到了两个不同的属性
<form:form method="post" modelAttribute="login">
Run Code Online (Sandbox Code Playgroud)
在此属性modelAttribute是表单对象的名称,其属性用于填充表单.我用它来发布一个表单,在控制器中,我习惯于@ModelAttribute捕获值,调用验证器,应用业务逻辑.这里一切都很好.现在
<form:form method="post" commandName="login">
Run Code Online (Sandbox Code Playgroud)
这个属性的期望是,它还是一个表单对象,我们将填充它们的属性?
Sot*_*lis 124
如果你查看支持你的元素的FormTag(4.3.x)的源代码<form>,你会注意到这一点
/**
* Set the name of the form attribute in the model.
* <p>May be a runtime expression.
*/
public void setModelAttribute(String modelAttribute) {
this.modelAttribute = modelAttribute;
}
/**
* Get the name of the form attribute in the model.
*/
protected String getModelAttribute() {
return this.modelAttribute;
}
/**
* Set the name of the form attribute in the model.
* <p>May be a runtime expression.
* @see #setModelAttribute
*/
public void setCommandName(String commandName) {
this.modelAttribute = commandName;
}
/**
* Get the name of the form attribute in the model.
* @see #getModelAttribute
*/
protected String getCommandName() {
return this.modelAttribute;
}
Run Code Online (Sandbox Code Playgroud)
它们都指的是同一个领域,因此具有相同的效果.
但是,正如字段名称所示,modelAttribute应该是首选,正如其他人也指出的那样.
die*_*eke 17
OLD WAY = commandName
...
<spring:url value="/manage/add.do" var="action" />
<form:form action="${action}" commandName="employee">
<div>
<table>
....
Run Code Online (Sandbox Code Playgroud)
NEW WAY = modelAttribute
..
<spring:url value="/manage/add.do" var="action" />
<form:form action="${action}" modelAttribute="employee">
<div>
<table>
..
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75689 次 |
| 最近记录: |