我有一个带有名字字段的表单的 spring mvc 应用程序。此字段应至少包含 3 个或更多字符。我在支持 bean 类中使用 hibernate @Size 验证器,如下所示:
@Size(min=3, message="message.key")
String firstName;
当此验证失败时,将显示相应的错误消息。但是,当页面在失败后重新加载时,输入的名字值会从输入字段中清除。如何使该值保留在字段中进行编辑?如果可能的话....
代码如下:
JSP Snippet - 这不是 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方法。这是完成验证的地方。