Spring 5验证程序

时间:2018-07-23 06:46:39

标签: java spring hibernate

我有User类,因此我使用Spring的Validator接口创建了UserValidator.java,以便在注册时验证User。

问题是验证已成功应用,但是jsp页面未显示错误消息。

UserValidator.java

@Component
public class UserValidator implements Validator {

public boolean supports(Class<?> clazz) {
     return User.class.equals(clazz);
}

public void validate(Object obj, Errors errors) {
      ValidationUtils.rejectIfEmpty(errors, "userName", "Enter a valid name");
      ValidationUtils.rejectIfEmpty(errors, "address", " Enter Address");
      ValidationUtils.rejectIfEmpty(errors, "mobNo", "Enter valid mobile number");
      ValidationUtils.rejectIfEmpty(errors, "emailId", "Enter a valid email");

      User user = (User) obj;
      System.out.println(""+user.getUserName());
      Pattern pattern = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$",
              Pattern.CASE_INSENSITIVE);
        if (!(pattern.matcher(user.getEmailId()).matches())) {
            errors.rejectValue("emailId", "Invalid email! Please enter valid email");
        }

}}

RegistrationController.java

@Controller
 public class RegistrationController {
 @Autowired
  UserValidator userValidator;

 @InitBinder
 public void initBinder(WebDataBinder webDataBinder){
        webDataBinder.setValidator(userValidator);
 }

@Autowired
private DepartmentDao departmentDao;  

@Autowired
private DesignationDao designationDao;

@Autowired
private ConfigDao configDao;

@Autowired
private HintQuestionDao hintQuestionDao;

@Autowired
private GroupDao groupDao;

 @Autowired
 private UserDao userDao;

@RequestMapping("Register")
public String getRegisterPage(@ModelAttribute("user") User user, Model model) {

      model.addAttribute("departmentList", departmentDao.getDepartments());
      model.addAttribute("designationList", designationDao.getDesignation());
      model.addAttribute("levelList", configDao.getLevel());
      model.addAttribute("questionList",hintQuestionDao.getQuestions());
      model.addAttribute("groupList", groupDao.getGroup());
      return "Registration";
}

@RequestMapping("newRegisterUser")
public String createUser(@ModelAttribute("user") User user, Model model,@RequestParam("deptId") int deptid,
@RequestParam("desgId") int desgId,@RequestParam("grpId")int grpId,HttpSession session, BindingResult bindingResult) throws Exception{
    User userSess= (User) session.getAttribute("userObj");
      if(userSess!=null) {

          userValidator.validate(user, bindingResult);
          if (bindingResult.hasErrors()) {

                 return "redirect:Register";
          }
      user.setMobNo(user.getMobNo());
      user.setDepartment(departmentDao.getDepartmentById(deptid));
      user.setDesignation(designationDao.getDesignationById(desgId));
      user.setGroup(groupDao.getGroupById(grpId));
      user.setUpw1(user.getUserName().replace(" " , "").substring(0,4)+user.getEmailId().substring(0,2)+"#"+user.getMobNo().toString().substring(8));
      user.setCreatedBy(userSess.getUserName());
      user.setCreationDate(new Timestamp(System.currentTimeMillis()));
      user.setLoginStatus("A");
          userDao.createUser(user);
      }
      else {
          model.addAttribute("AddMessage","Login First");
      }
      return "redirect:ListOfUser";
}}

Registration.jsp

<form:form class="form" modelAttribute="user" method="post" action="newRegisterUser">
	             
	                 <div class="form-group row">
	                     <label class="col-lg-3 col-form-label form-control-label">Name:</label>
	                     <div class="col-lg-9">
	                         <form:input class="form-control" type="text" path="userName"/>
	                        <form:errors path = "userName" cssClass = "error" />
	                     </div>
	                 </div>
	                 <div class="form-group row">
	                     <label class="col-lg-3 col-form-label form-control-label">Address:</label>
	                     <div class="col-lg-9">
	                        <form:textarea class="form-control" rows="2" path="address"></form:textarea>
	                          <form:errors path = "address" cssClass = "error" />
	                     </div>
	                 </div>
	                 <div class="form-group row">
	                     <label class="col-lg-3 col-form-label form-control-label">Mobile:</label>
	                     <div class="col-lg-3">
	                         <form:input class="form-control" type="text" path="mobNo"/>
	                        
	                     </div>
	                     <label class="col-lg-2 col-form-label form-control-label">Email:</label>
	                     <div class="col-lg-4">
	                         <form:input class="form-control" type="email" path="emailId"/>
	                          <form:errors path = "emailId" cssClass = "error" />
	                     </div>
	                 </div>
	              
	                 <div class="form-group row">
	                     <label class="col-lg-3 col-form-label form-control-label">Department:</label>
	                     <div class="col-lg-3">
	                       <select name="deptId" id="dept_Id" class="form-control" size="0">
	                          <option value="0">--Select--</option>
								<c:forEach var="depts" items="${departmentList}">
									<option value="<c:out value="${depts.deptid}" />">${depts.deptnm}</option>
								</c:forEach>
							</select>
							
	                   </div>
	                    <label class="col-lg-2 col-form-label form-control-label">Designation:</label>
	                     <div class="col-lg-4">
	                       <select name="desgId" id="design_Id" class="form-control" size="0">
	                         <option value="0">--Select--</option>
								<c:forEach var="desig" items="${designationList}">
									<option value="<c:out value="${desig.desgid}" />">${desig.desgnm}</option>
								</c:forEach>
							</select>
						
	                     </div>
	                 </div>
	                
	                 <div class="form-group row">
	                     <label class="col-lg-3 col-form-label form-control-label">Level:</label>
	                     <div class="col-lg-3">
	                        <select name="userLevel" id="lvl_Id" class="form-control" size="0">
	                           <option value="-1">--Select--</option>
								 <c:forEach var="levelList" items="${levelList}">
							      <option value="<c:out value="${levelList.sk}"/>">${levelList.ds}</option>
								</c:forEach>
							</select>
							
	                     </div>
	                     <label class="col-lg-2 col-form-label form-control-label">Group:</label>
	                     <div class="col-lg-4">
	                          <select name="grpId" id="grp_Id" class="form-control" size="0">
	                            <option value="-1">--Select--</option>
								<c:forEach var="groupList" items="${groupList}">
									<option value="<c:out value="${groupList.grpid}" />">${groupList.grpnm}</option>
								</c:forEach>
							</select>
							
	                     </div>
	                 </div>
	               
	                  <div class="form-group row">
	                     <label class="col-lg-3 col-form-label form-control-label">Hint Question:</label>
	                     <div class="col-lg-3">
	                       <select name="hintId" id="que_Id" class="form-control" size="0">
	                         <option value="0">--Select--</option>
								<c:forEach var="questionList" items="${questionList}">
									<option value="<c:out value="${questionList.hintid}" />">${questionList.hintquestion}</option>
								</c:forEach>
							</select>
							
	                     </div>
	                     <label class="col-lg-2 col-form-label form-control-label">Answer:</label>
	                       <div class="col-lg-4">
	                         <form:input class="form-control" type="text" path="hintAns"/>
	                       </div>
	                 </div>
	                
	                 <div class="form-group row">
	                     <label class="col-lg-5 col-form-label form-control-label"></label>
	                     <div class="col-lg-7">
	                         <input type="submit" class="btn btn-info" value="Register">
	                         <input type="reset" class="btn btn-primary" value="Cancel">
	                     </div>
	                 </div>
	             </form:form>

有人可以帮我吗?

我在这方面做错了什么?

谢谢。

0 个答案:

没有答案