我有一个具有名字字段形式的spring mvc应用程序。该字段至少应包含3个字符。我在后备bean类中使用休眠@Size验证器,如下所示:
@Size(min=3, message="message.key")
String firstName;
当此验证失败时,将显示相应的错误消息。但是,失败后重新加载页面时,将从输入字段中清除为名字输入的值。如何获取该值以保留在字段中进行编辑?如果可能的话....
代码如下:
JSP代码段-这不是Portlet应用程序
<portlet:renderURL var="createUserAction">
<portlet:param name="action" value="createUser"/>
</portlet:renderURL>
<form:form method="post" commandName="userInformation" action="${createUserAction}" htmlEscape="false">
<h2>
<fmt:message key="msg.label.form.title" bundle="${msg}" />
</h2>
<form:errors path="*" cssClass="errorblock" element="div"></form:errors>
<p>
<form:label path="firstName">
<fmt:message key="msg.label.form.fname" bundle="${msg}"/>
</form:label>
<form:input path="firstName" />
</p>
<p>
<form:label path="lastName">
<fmt:message key="msg.label.form.lname" bundle="${msg}"/>
</form:label>
<form:input path="lastName" />
</p>
<div style="margin-left: 150px; margin-top: 20px">
<input type="submit" value="Save" />
<input type="reset" value="Reset" />
</div>
</form:form>
豆
@Component
public class UserInformation {
@NotEmpty(message="msg.error.required.lname")
private String lastName;
@NotEmpty(message="msg.error.required.fname")
@Size(min=3, message="msg.error.length.fname")
private String firstName;
public UserInformation() {
super();
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
控制者
@Controller
@RequestMapping("VIEW")
public class UserManagement {
@Autowired
private UserInformation userInformation;
@Autowired
private Validator validator;
@Autowired
private MessageSource messageSource;
@RequestMapping(params="page=addUser")
public String addUser(Model model){
userInformation = new UserInformation();
model.addAttribute("userInformation", userInformation);
return Page.ADD_USER;
}
@RequestMapping(params="action=createUser")
public String createUser(
@ModelAttribute(value="userInformation") UserInformation userInformation,
BindingResult result, Model model) throws ApplicationException{
// get values
String firstName = userInformation.getFirstName();
System.out.println("fname="+firstName);
Set<ConstraintViolation<UserInformation>> constraintViolations =
validator.validate(userInformation);
for(ConstraintViolation<UserInformation> constraintViolation : constraintViolations) {
String propertyPath = constraintViolation.getPropertyPath().toString();
String message = constraintViolation.getMessage();
result.addError(
new FieldError(
"member",
propertyPath,
messageSource.getMessage(
message,
null,
Locale.ENGLISH
)
)
);
}
// Errors found
if(result.hasErrors()){
return UMConstants.Page.ADD_USER;
}
String successMsg = messageSource.getMessage(
"msg.user.added",
new Object[]{userInformation.getFirstName()},
Locale.ENGLISH
);
model.addAttribute("successMsg", successMsg);
return UMConstants.Page.INDEX;
}
}
用户将单击执行addUser方法的链接,以使用上述JSP代码段指示的表单加载页面。当用户单击提交按钮时,将调用createUser方法。这是进行验证的地方。
最佳答案
我遇到了同样的问题,并通过(痛苦地)遍历Spring MVC form:input
jsp标记的源代码找到了解决方案……以为我会在这里共享它,也许会对某人有所帮助。
简而言之:问题来自form:input
标记。此标记在评估其值时,将检查与此字段相关的错误(path
属性),如果发现任何错误,它将使用rejectedValue
实例的FieldError
属性而不是该字段的值从命令或表单对象。
在控制器方法的参数中使用@Valid
注释时,Spring将正确填充rejectedValue
对象上的FieldError
属性。
但是,如果您使用简短的构造函数(3个参数)手动创建FieldError
对象,则不会设置rejectedValue
的FieldError
属性,并且标记将使用此null值进行显示...
解决方案是在创建FieldError
对象时使用构造函数的长版本,例如:
result.addError(
new FieldError(
"member",
"firstName",
userInformation.getFirstName(),
false,
new String[0],
new Object[0],
messageSource.getMessage(
message,
null,
Locale.ENGLISH
)
)
);
form:input
标记将使用rejectedValue
的FieldError
属性,这是此较长的构造函数中的第三个参数,并且在使用较简单的3 args构造函数时未设置...请注意,命令或表单对象(此处为userInformation)上的实际值始终是正确的,这使得此错误/怪癖很难跟踪。
希望能帮助某人,最终...