隨筆-13  評論-9  文章-9  trackbacks-0

          采取的是spring action 中的例子
          驗(yàn)證一個Student對象
          ???1.implements Validator接口(org.springframework.validation.Validator)
          ?????????public class StudentValidator implements Validator
          ?????????{
          ????????????public boolean supports(Class clazz)//必須實(shí)現(xiàn)的方法
          ????????????{return clazz.equals(Student.class)?;
          ??? ????????}
          ?????????? ?public void validate(Object command,Errors errors)
          ?????????? {
          ????????????? Student student =(Student)command;
          ????????????//?ValidationUtils.rejectIfEmpty? (Errors errors, String field, String errorCode, String defaultMessage)

          ???????????????ValidationUtils.rejectIfEmpty(errors,"login","required.login","login is required");
          ???????????????ValidationUtils.rejectIfEmpty(errors,"password","required.password","Password is required");
          ???????????????ValidationUtils.rejectIfEmpty(errors,"firstName","required.firstName","firstNameis required");
          ???????????????ValidationUtils.rejectIfEmpty(errors,"lastName","required.lastName","lastNameis required");
          ???????????????ValidationUtils.rejectIfEmpty(errors,"city","required.login","cityis required");
          ????????????????????????????????????????????validateEmail(student .email,errors);
          ???????????????validatePhone(student .phone,errors);
          ????????????}
          ????????????
          ????????????private static final String PHONE_REGXP="/(\\({0,1}(\\d{3})))/"
          ????????????private void validatePhone(String phone, Errors erros)
          ????????????{
          ?????????????ValidationUtils.rejectIfEmpty(errors,"phone","required.phone","phoneis required");
          ???????????? Perl5Util per5Util=new Perl5Util();
          ???????????? if(!per5Util.math(PHONE_REGXP,phone))
          ????????????{
          ??????????????errors.reject("invalid.phone","Phone number is invalid");
          ???????????????}
          ???????????????}????
          ??????????????private static final String Email_REGXP="/(\\({0,1}(\\d{3})))/"
          ????????????private void validateEmail(String phone, Errors erros)
          ????????????{
          ?????????????ValidationUtils.rejectIfEmpty(errors,"phone","required.phone","phoneis required");
          ???????????? Perl5Util per5Util=new Perl5Util();
          ???????????? if(!per5Util.math(Email_REGXP,email))
          ????????????{
          ??????????????errors.reject("invalid.phone","Phone number is invalid");
          ???????????????}
          ???????????????}?????????
          ?????

          }
          2.配置bean
          ??????<bean id="methodNameResolver"
          class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
          <property name="paramName"><value>method</value>
          </property>
          <property name="defaultMethodName"><value>view</value>
          </property>
          </bean>
          <property name="validator"><bean class="com.StudentValidator "/>?
          </property>
          <property name="defaultMethodName"><value>view</value>
          </property>
          <bean name="/jsp/test.do" class="org.nightwalker.spaces.web.controller.TestController">
          <property name="methodNameResolver">
          <ref local="methodNameResolver"/>
          </property>
          </bean>

          ?

          查看源代碼:
          MultiActionController類:
          ??protected void bind(ServletRequest request, Object command) throws Exception {
          ??logger.debug("Binding request parameters onto MultiActionController command");
          ??ServletRequestDataBinder binder = createBinder(request, command);
          ??binder.bind(request);
          ??if (this.validators != null) {
          ???for (int i = 0; i < this.validators.length; i++) {
          ??????//首先調(diào)用supports
          ??? ??if (this.validators[i].supports(command.getClass())) {
          ?????ValidationUtils.invokeValidator(this.validators[i], command, binder.getErrors());
          ????}
          ???}
          ??}
          ??binder.closeNoCatch();
          ?}

          類ValidationUtils:
          public static void invokeValidator(Validator validator, Object obj, Errors errors) {
          ??if (validator != null) {
          ???if (logger.isDebugEnabled()) {
          ????logger.debug("Invoking validator [" + validator + "]");
          ???}
          ???if (obj != null && !validator.supports(obj.getClass())) {
          ????throw new IllegalArgumentException("Validator " + validator.getClass() +
          ??????" does not support " + obj.getClass());
          ???}
          ???validator.validate(obj, errors);
          ???if (logger.isDebugEnabled()) {
          ????if (errors.hasErrors()) {
          ?????logger.debug("Validator found " + errors.getErrorCount() + " errors");
          ????}
          ????else {
          ?????logger.debug("Validator found no errors");
          ????}
          ???}
          ??}
          ?}
          ?

          posted on 2006-06-21 09:39 Dragonofson 閱讀(6043) 評論(1)  編輯  收藏 所屬分類: Spring

          評論:
          # re: Spring MVC表單驗(yàn)證 2013-03-12 15:12 | vgf
          kljlkjkljl  回復(fù)  更多評論
            
          主站蜘蛛池模板: 曲水县| 石渠县| 新化县| 涡阳县| 卢湾区| 阜南县| 安岳县| 马公市| 宁晋县| 临邑县| 沁水县| 徐水县| 山阳县| 布拖县| 林州市| 海伦市| 锡林郭勒盟| 遂昌县| 湖州市| 通州市| 务川| 汉寿县| 讷河市| 仁怀市| 盐城市| 民勤县| 清丰县| 湟中县| 泌阳县| 奇台县| 长兴县| 新兴县| 台中市| 岳西县| 宁远县| 兴业县| 桃江县| 高雄县| 华池县| 峨山| 永宁县|