我正在使用Spring 3.1和hibernate验证器4.2.0
在客户端,我使用commandName
将我的对象(比如User)映射到表单假设我在用户
中有两个字段@NotEmpty
@Size(min = 8, max = 100)
@Pattern(regexp="[0-9a-zA-Z_.]+")
private String userId;
@NotEmpty
@Size(min = 3, max = 1000)
private String name;
当我输入名称字段的无效输入时,它将在表单中清除,但不会在userId中清除。当我从userId中删除@Pattern时,它将被清除。可能是什么原因造成的?
此外,我尝试在控制器中修改User对象,但它没有被修改。
请找到控制器代码:
@RequestMapping(value="AddUser.do", method=RequestMethod.POST)
public String handleAddUser(@Valid User user, BindingResult result, ModelMap model){
//checking for malicious html
if(!InputValidator.isInputValid(user.getUserId())){
result.addError(new FieldError("User","userId",ConsoleMessages.getMessage("User.userId.malicious")));
}
if(!InputValidator.isInputValid(user.getPassword())){
result.addError(new FieldError("User","password",ConsoleMessages.getMessage("User.password.malicious")));
}
if(!InputValidator.isInputValid(user.getConfirmPassword())){
result.addError(new FieldError("User","confirmPassword",ConsoleMessages.getMessage("User.confirmPassword.malicious")));
}
if(!InputValidator.isInputValid(user.getName())){
result.addError(new FieldError("User","name",ConsoleMessages.getMessage("User.name.malicious")));
}
if(!InputValidator.isInputValid(user.getEmail())){
result.addError(new FieldError("User","email",ConsoleMessages.getMessage("User.email.malicious")));
}
if(!InputValidator.isInputValid(user.getEnterpriseId())){
result.addError(new FieldError("User","enterpriseId",ConsoleMessages.getMessage("User.enterpriseId.malicious")));
}
if(!InputValidator.isInputValid(user.getMobile())){
result.addError(new FieldError("User","mobile",ConsoleMessages.getMessage("User.mobile.malicious")));
}
model.addAttribute("success", "");
model.addAttribute("duplicateUserId", "");
model.addAttribute("sameUserIdPwd", "");
model.addAttribute("twoPwdsDifferent", "");
if(userOp.isDuplicateUser(user.getUserId())){
model.addAttribute("duplicateUserId", "User with User ID <b>" + user.getUserId() + "</b> already exists, please give a different User ID.");
}
else if(user.getUserId().equals(user.getPassword())){
model.addAttribute("sameUserIdPwd", "User ID and password cannot be same.");
}
else if(!user.getConfirmPassword().equals(user.getPassword())){
model.addAttribute("twoPwdsDifferent", "Confirm password is not matching with password.");
user.setPassword("");
user.setConfirmPassword("");
}
else if(!result.hasErrors())
{
userOp.saveUser(user);//saving to database
success = "User <b>" + user.getUserId() + "</b> added successfully.";
return "redirect:AddUserSuccess.html";
}
return "CreateUser";//mapped to CreateUser.jsp.
}
我的用户bean:
@Entity
@Table(name="USER_MASTER")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@NotEmpty
@Size(min = 8, max = 100)
@Pattern(regexp="[0-9a-zA-Z_.]+")
private String userId;
@NotEmpty
@Size(min = 3, max = 1000)
private String name;
@NotEmpty
@Size(min = 8, max = 200)
@Pattern(regexp="^(?=.*\\d)(?=.*[a-zA-Z]).+$")
private String password;
@Transient
private String confirmPassword;
@NotEmpty
@Pattern(regexp="[+]{0,1}[0-9]{0,2}(((\\(\\d{3}\\) ?)|(\\d{3}-))?\\d{3}-\\d{4}|[0-9]{10})")
private String mobile;
@Email
@Size(min = 6, max=200)
private String email;
private Boolean isEnabled;
private Boolean isDeleted;
@Temporal(TemporalType.TIMESTAMP)
private Date insertTimestamp;
@Temporal(TemporalType.TIMESTAMP)
private Date updateTimestamp;
private String insertId;
private String updateId;
@Size(max = 1000)
private String enterpriseId;
}
JSP代码
<form:form name ="userform" action="AddUser.do" onsubmit="return validateUser()" method="post" commandName="user">
<%-- <form:hidden id="user_updateId" name="user_updateId" path="updateId" value=""/><security:authentication property="name" /> --%>
<div id="OAV" class="OAV">
<div class="formbg">
<div class="title">Add User</div>
<div><font color="red" size="2px">${duplicateUserId}</font></div>
<div><font color="red" size="2px">${sameUserIdPwd}</font></div>
<div><font color="red" size="2px">${twoPwdsDifferent}</font></div>
<div><font color="red" size="2px"><form:errors path="userId" /></font></div>
<div><font color="red" size="2px"><form:errors path="password" /></font></div>
<div><font color="red" size="2px"><form:errors path="name" /></font></div>
<div><font color="red" size="2px"><form:errors path="email" /></font></div>
<div><font color="red" size="2px"><form:errors path="mobile" /></font></div>
<div><font color="green" size="2px">${success}</font></div>
<div class="cont">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td class="t1" >User ID:</td>
<td class="t2"><form:input style="width:90%" id="user_id" name="user_id" path="userId"/></td>
</tr>
<tr>
<td class="t1">Password:</td>
<td class="t2"><form:password style="width:90%" id="user_password" name="user_password" path="password" /></td>
</tr>
<tr>
<td class="t1">Confirm Password:</td>
<td class="t2"><form:password style="width:90%" id="user_password_confirm" name="user_password_confirm" path="confirmPassword" /></td>
</tr>
<tr>
<td class="t1">User Name:</td>
<td class="t2">
<form:input style="width:90%" id="user_name" name="user_name" path="name" />
</td>
</tr>
<tr>
<td class="t1">Email:</td>
<td class="t2"><form:input style="width:90%" id="user_email" name="user_email" path="email"/></td>
</tr>
<tr>
<td class="t1">Mobile:</td>
<td class="t2"><form:input style="width:90%" id="user_mobile" name="user_mobile" path="mobile"/></td>
</tr>
<tr>
<td class="t1">Enterprise User ID:</td>
<td class="t2"><form:input style="width:90%" id="user_eid" name="user_eid" path="enterpriseId"/></td>
</tr>
</table>
</div>
<div class="add_footer">
<div><a href="#"><input class="button" value="Cancel" type="button" onclick="self.close()"/></a>
<a href="#"><input style="margin-right:10px" class="button" value="Ok" type="submit" /></a></div>
</div>
</div>
</div>