1.applicationContext.xml
//spring 验证对应的message资源. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>WEB-INF/classes/message</value> </list> </property> </bean> <bean id="beanValidator" class="org.springmodules.validation.commons.DefaultBeanValidator"> <property name="validatorFactory" ref="validatorFactory"/> </bean> <bean id="validatorFactory" class="org.springmodules.validation.commons.DefaultValidatorFactory"> <property name="validationConfigLocations"> <list> <value>/WEB-INF/validator-rules.xml</value> <value>/WEB-INF/validator.xml</value> </list> </property> </bean>
2.validator-ruls.xml 网上有。
3.validator.xml自己定义的规则:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1.dtd"> <form-validation> <formset> <form name="itemUpdateForm"> <field property="item.name.fullName" depends="required"> <arg0 key="item.name.fullName" /> </field> </form> <form name="administratorEditForm"> <field property="administrator.username" depends="required,minlength,maxlength"> <!-- msg标签用于覆盖资源文件的内容,name用户指验证规则,key与资源文件的属性名一致 --> <!-- arg标签用于向资源文件传递参数,position用于指定参数的位置,key用于指定传递的值 --> <arg0 key="User name" /> <arg1 name="minlength" key="${var:minlength}"/> <arg1 name="maxlength" key="${var:maxlength}"/> <var> <var-name>minlength</var-name> <var-value>2</var-value> </var> <var> <var-name>maxlength</var-name> <var-value>6</var-value> </var> </field> <field property="administrator.password" depends="required,minlength,maxlength"> <!-- msg标签用于覆盖资源文件的内容,name用户指验证规则,key与资源文件的属性名一致 --> <!-- arg标签用于向资源文件传递参数,position用于指定参数的位置,key用于指定传递的值 --> <arg0 key="Password" /> <arg1 name="minlength" key="${var:minlength}"/> <arg1 name="maxlength" key="${var:maxlength}"/> <var> <var-name>minlength</var-name> <var-value>2</var-value> </var> <var> <var-name>maxlength</var-name> <var-value>6</var-value> </var> </field> </form> <form name="currencyEditForm"> <field property="currency.currencyCode" depends="required"> <arg0 key="currency.currencyCode" /> </field> <field property="currency.name.fullName" depends="required"> <arg0 key="currency.name.fullName" /> </field> </form> <form name="categoryEditForm"> <field property="category.name.fullName" depends="required"> <arg0 key="category.name.fullName" /> </field> </form> </formset> </form-validation>
4.message.proerties.
data.empty=Please input data. data.error=Incorrect data. username.or.password.invalid=Username or password is invalid. errors.required={0} is required. errors.minlength={0} can not be less than {1} characters. errors.maxlength={0} can not be greater than {1} characters. errors.invalid={0} is invalid. errors.byte={0} must be a byte. errors.short={0} must be a short. errors.integer={0} must be an integer. errors.long={0} must be a long. errors.float={0} must be a float. errors.double={0} must be a double. errors.date={0} is not a date. errors.range={0} is not in the range {1} through {2}. errors.creditcard={0} is an invalid credit card number. errors.email={0} is an invalid e-mail address. item.name.fullName=Item Name. errors.userName.exists=administrator.username has exists. errors.currency.fullName.exists=currency.name.fullName has exists. errors.currency.currencyCode.exists=currency.currencyCode has exists. errors.category.fullName.exists=category.name.fullName has exists.
5.java
@Controller @RequestMapping("/admin/administrator/*") public class AdministratorController { @Autowired private Validator validator; @RequestMapping(value = "/admin/administrator/saveOrUpdate.do", method = RequestMethod.POST) public String onSubmit(@ModelAttribute("administratorEditForm") AdministratorEditForm administratorForm, BindingResult result, ModelMap model, HttpServletRequest request) { //spring 规则验证 required,email等等 validator.validate(administratorForm, result); //后台自定义去重判断.. if (!isAdd && !administratorService.validName(administratorForm.getAdministrator().getUsername(), administratorForm.getAdministrator().getObjectId()) || isAdd && !administratorService.validName(administratorForm.getAdministrator().getUsername())) { result.rejectValue("administrator.username", "errors.userName.exists"); } if (result.hasErrors()) { model.addAttribute("itemForm", administratorForm); if (!isAdd) { return "admin/administrator/update"; } else { return "admin/administrator/save"; } } }
6.jsp
<tr> <td>User Name:</td> <td> <form:input path="administrator.username" cssClass="input" maxlength="50"/> <form:errors path="administrator.username" cssClass="red"/> </td> </tr> <tr> <td>Password:</td> <td> <form:password path="administrator.password" cssClass="input" maxlength="50"/> <form:errors path="administrator.password" cssClass="red"/> </td> </tr>