2013-02-27 79 views
1

Form:Options工作得很好,但如果我在表单中提交了错误的数据,form:optionsnewContact.jsp显示departmentList的第一个元素被选中。使用表单进行表单验证:选项

的ContactController:

@RequestMapping(value="/saveContact", method=RequestMethod.GET) 
public ModelAndView newuserForm(){ 
    ModelAndView mav = new ModelAndView("newContact"); 
// Contact contact = new Contact();   
// Department department = new Department(); 
    User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
    String name = user.getUsername(); 
    mav.addObject("username", name); 
    mav.addObject("newContact", new Contact()); 
// mav.addObject("department", department); 
    mav.addObject("departmentList", departmentService.listDepartment()); 

    //mav.getModelMap().put("newContact", contact); 
    return mav; 
} 

@RequestMapping(value="/saveContact", method=RequestMethod.POST) 
public String create(@ModelAttribute("newContact")Contact contact, BindingResult result, SessionStatus status,Map<String, Object> map) 
{ 
    User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
    String name = user.getUsername(); 

    map.put("departmentList", departmentService.listDepartment()); 
    map.put("username", name); 

    contactFormvalidator.validate(contact, result); 
    if (result.hasErrors()) 
    {    
     return "newContact"; 
    } 
    contactService.addContact(contact); 
    status.setComplete(); 
    //return "redirect:/showContacts.do"; 
    return "newContactSuccess"; 
} 

newContact.jsp:

<%@include file="header.jsp"%> 

<div id="menu"> 
    <div id="subMenu"></div> 
</div> 


<div id="main"> 

    <h2>Contact Manager</h2> 

    <form:form method="post" action="saveContact.do" commandName="newContact" > 

     <table> 
      <tr> 
       <td><form:label path="firstname"> 
         <spring:message code="label.firstname" /> 
        </form:label></td> 
       <td><form:input path="firstname" /></td> 
       <td><form:errors path="firstname" cssStyle="color:red"></form:errors> </td> 
      </tr> 
      <tr> 
       <td><form:label path="lastname"> 
         <spring:message code="label.lastname" /> 
        </form:label></td> 
       <td><form:input path="lastname" /></td> 
       <td><form:errors path="lastname" cssStyle="color:red"></form:errors> </td> 
      </tr> 
      <tr> 
       <td><form:label path="email"> 
         <spring:message code="label.email" /> 
        </form:label></td> 
       <td><form:input path="email" /></td> 
       <td><form:errors path="email" cssStyle="color:red"></form:errors> </td> 
      </tr> 
      <tr> 
       <td><form:label path="telephone"> 
         <spring:message code="label.telephone" /> 
        </form:label></td> 
       <td><form:input path="telephone" /></td> 
       <td><form:errors path="telephone" cssStyle="color:red"></form:errors> </td> 
      </tr> 

      <tr>  
       <td><form:label path="department"> 
         <spring:message code="label.department" /> 
        </form:label></td> 
        <td><form:select path="department" >   
        <form:option label="**SELECT**" value="0"></form:option>       
          <form:options items="${departmentList}" itemValue="id" itemLabel="name"></form:options>       
         </form:select> </td> 
        <td><form:errors path="department" cssStyle="color:red"></form:errors> </td> 
      </tr> 



      <tr> 
       <td colspan="2"><input type="submit" 
        value="<spring:message code="label.addcontact"/>" /></td> 
      </tr> 



     </table> 
    </form:form>   

</div> 

<%@ include file="footer.jsp"%> 

ContactFormValidator:

package pl.ivmx.contact.validator; 

import java.util.regex.*; 

import org.springframework.stereotype.Component; 
import org.springframework.validation.Errors; 
import org.springframework.validation.Validator; 
import pl.ivmx.contact.form.Contact;; 

//@Component("contactFormValidator") 
public class ContactFormValidator implements Validator{ 
    @Override 
    public boolean supports(Class clazz) { 
     return Contact.class.isAssignableFrom(clazz); 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    public void validate(Object obj, Errors errors) {  
     Contact contact = (Contact) obj; 
     Pattern p = Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*.[a-zA-Z]*"); 
     Matcher m = p.matcher(contact.getEmail()); 
     boolean b = m.matches(); 
     if (b != true) { 
      errors.rejectValue("email", "error.is.not.valid", 
        "Email does not Valid "); 
     } 

     if (contact.getFirstname() == null || contact.getFirstname().length() < 3) { 
      errors.rejectValue("firstname", "error.empty.field", 
        "Please Enter First Name"); 
     } 

     if (contact.getLastname() == null || contact.getLastname().length() < 4) { 
      errors.rejectValue("lastname", "error.empty.field", 
        "Please Enter Last Name"); 
     } 

     if (contact.getTelephone() == 0 || String.valueOf(contact.getTelephone()).trim().length() < 2 || 
       String.valueOf(contact.getTelephone()).trim().length() > 9) { 
      errors.rejectValue("telephone", "error.empty.field", 
        "Please Enter Telephone"); 
     } 

     if (contact.getDepartment() == null) { 
      errors.rejectValue("department", "error.empty.field", 
        "Please select department"); 
     } 
    } 
} 

回答

0

而是在你提交方法返回的ModelAndView返回字符串的...

@RequestMapping(value="/saveContact", method=RequestMethod.POST) 
public ModelAndView create(@ModelAttribute("newContact")Contact contact, BindingResult result, SessionStatus status,ModelMap map) 
    { 
     User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
     String name = user.getUsername(); 

     map.addAttribute("departmentList", departmentService.listDepartment()); 
     map.addAttribute("username", name); 

     contactFormvalidator.validate(contact, result); 
     if (result.hasErrors()) 
     {    
     return new ModelAndView("newContact",map); 
     } 
     contactService.addContact(contact); 
     status.setComplete(); 
     //return new ModelAndView("redirect:/showContacts.do"); 
     return new ModelAndView("newContactSuccess"); 

}

+0

问题是一样的。 – Patrick 2013-02-28 12:41:28

+0

您用于在部门ID(存储在表单中的值)和部门对象之间进行转换的转换策略是什么?你应该使用某种类型的ID到实体格式化程序,然后查看是否为Department对象正确实现了“hashCode”和“equals”。发生什么事情成功提交表格?部门领域设置是否正确? – 2013-02-28 18:59:44

+0

我添加了验证类(上图)。 如果我成功提交表单evrythings工作良好,但如果我在* Patrick 2013-02-28 19:18:53