2013-12-11 200 views
2

我有一個表單,可以通過實現Spring Validator接口進行驗證。 驗證只涉及我的表單中的字段。我沒有這個問題,但是我想要做什麼,我不知道如何繼續就是在我的控制器中調用一個服務方法,如果這個方法返回一個特定的值,返回到表單頁面並顯示錯誤消息在jsp頁面中。驗證彈簧服務層

驗證:

public class TrunkValidator implements Validator{ 

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

public void validate(Object obj, Errors errors) { 
    Trunk trunk = (Trunk) obj; 

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "compagnyName", "field.required", "Nom emtreprise est obligatoire"); 

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "pbxName", "field.required", "Nom Pbx est obligatoire"); 


    if (! errors.hasFieldErrors("compagnyName")) { 
     if ("trunk".equals(trunk.getCompagnyName())) 
      errors.rejectValue("compagnyName", "sssss", "déja existant"); 
    }  
    } 
} 

控制器:

@RequestMapping(value = "/createTrunk", method = RequestMethod.POST) 
    protected ModelAndView createTrunk(@Valid Trunk trunk, 
      BindingResult bindingResult) { 
       // form validation 
     if (bindingResult.hasErrors()) { 
      ModelAndView model = new ModelAndView("/jsp/createTrunk.jsp"); 
      model.addObject(ATTRIBUTE_BASE_URL, baseUrl); 
      return model; 
     } else { 

        // if service method retun error message or exception 
        // display form with error message 

     } 
    } 

JSP:

<form:form method="POST" commandName="trunk" action="${baseUrl}/createTrunk"> 
    <form:errors path="*" cssClass="errorblock" element="div" /> 
    <table> 
     <tr> 
      <td>Nom de l'entreprise :</td> 
      <td><form:input path="compagnyName" /></td> 
      <td><form:errors path="compagnyName" cssClass="error" /></td> 
     </tr> 
     <tr> 
      <td>Nom de PBX :</td> 
      <td><form:input path="pbxName" /></td> 
      <td><form:errors path="pbxName" cssClass="error" /></td> 
     </tr> 

     <tr> 
      <td>N° de commande :</td> 
      <td><form:input path="orderNum" /></td> 
      <td><form:errors path="orderNum" cssClass="error" /></td> 
     </tr> 



     <tr> 
      <td>LDI :</td> 
      <td><form:input path="ldi" /></td> 
      <td><form:errors path="ldi" cssClass="error" /></td> 
     </tr> 

     <tr> 
      <td>INSEE :</td> 
      <td><form:input path="insee" /></td> 
      <td><form:errors path="insee" cssClass="error" /></td> 
     </tr> 

回答

0

在控制器中添加@InitBinder

@InitBinder("trunk") 
public void initBinderAll(WebDataBinder binder) { 
     binder.setValidator(new TrunkValidator()); 
} 
0

使用註釋和春天@Autowired特點:

設置你的servlet.xml中像這樣...

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

<context:component-scan base-package="sk.eglu.khl" /> 

<bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/WEB-INF/pages/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

<mvc:resources mapping="/resources/**" location="/resources/mytheme/" /> 

<mvc:annotation-driven /> 

...然後你可以使用標註 - 使創建的界面「TrunkValidator」,並添加到您的服務層的註釋是這樣的:

public interface TrunkValidatorInt { 

    boolean validate(Object obj, Errors errors); 

} 

@Service 
public class TrunkValidatorImpl implements TrunkValidatorInt { 

    @Override 
    boolean validate(Object obj, Errors errors){ 
     return true; 
    } 

} 

@Controller 
@RequestMapping("/welcome") 
public class HelloWorldController{ 

     @Autowired(required = true) 
     TrunkValidatorInt trunkValidator; 

    @RequestMapping(method = RequestMethod.GET) 
    public ModelAndView helloWorld(){ 

    final ModelAndView model = new ModelAndView("HelloWorldPage"); 

      if(trunkValidator.validate(someValue)){ 
     model.addObject("msg", "good!"); 
      } else { 
     model.addObject("msg", "bad!"); 
      } 
    return model; 
    } 
} 

你也可以對這個項目的出發看看爪哇,你可以看到圖層如何溝通:

https://github.com/majusko/GeneralJavaRest