Spring mvc之JSR303框架实现数据格式化,数据验证,资源国际化

来源:互联网 发布:php用户注册系统源码 编辑:程序博客网 时间:2024/05/16 15:33

今天使用JSR303框架实现 mvc的3个重要的部分

  1. 数据格式化
  2. 数据验证
  3. 资源国际化

要使用到的3个jar包:

  • hibernate-validator-4.3.2.Final.jar
  • validation-api-1.0.0.GA.jar
  • jboss-logging-3.1.4.GA.jar

首先要给 spring mvc配置国际化资源和数据验证资源,配置代码如下

<!-- 开启注解的功能 加载数据验证配置 -->    <mvc:annotation-driven validator="validator">        <mvc:message-converters>            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">                <property name="supportedMediaTypes">                    <list>                        <value>text/html;charset=UTF-8</value>                    </list>                </property>            </bean>        </mvc:message-converters>    </mvc:annotation-driven><!-- 配置国际化的资源 -->    <bean id="messageSource"        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">        <property name="basenames">            <list>                <value>/WEB-INF/resources/errors</value>                <value>/WEB-INF/resources/labels</value>                <value>/WEB-INF/resources/messages</value>            </list>        </property>        <property name="cacheSeconds" value="0"></property>    </bean>    <!--Spring mvc 读取国际化资源  -->    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">        <property name="validationMessageSource" ref="messageSource"></property>    </bean>

以上配置表名:国际化资源的路径在/WEB-INF/resources/目录下
文件名称,须按约定的名称
- *_en_US.properties 英文
- *_zh_CN.properties 中文

如图:
这里写图片描述

读取国际化资源后,使用spring标签库可直接进行国际化输出

<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%><%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf"%>

使用 <s:message code=""/>标签即可,code写国际化的key值


这样即可实现国际化

配置完毕后,要对实体类进行添加注解实现数据格式化和验证

//此处省略其它属性以及get set和构造函数@NotNull(message="{error.empmgr.emp.notnull}")    @Length(min=5,max=10,message="{error.empmgr.emp.ename.length}")    private String ename;@NotNull(message="{error.empmgr.emp.notnull}")    @Past(message="{error.empmgr.emp.hiredate.past}")       @DateTimeFormat(pattern="yyyy-MM-dd")    private Date hiredate;

然后就是对控制器的编码控制,

/**省略其它用不到方法...*/@RequestMapping("/add")    public ModelAndView add(@Valid Emp emp, BindingResult result) {        ModelAndView model = new ModelAndView();        if (!result.hasErrors()) {            if (iEmpBiz.addEmp(emp)) {                model.setViewName("redirect:/emps");            } else {                model.setViewName("emp_add");            }        } else {            model.setViewName("emp_add");        }        return model;    }

提交表单前,要进行预添加出来,在resquest范围,放置null的实体类对象。
预添加的方法如下:
@RequestMapping("/pre_add")
public ModelAndView preadd() {
ModelAndView model = new ModelAndView();
model.addObject("emp", new Emp());
model.setViewName("emp_add");
return model;
}


前台表单如下处理呢?先添加spring标签库

<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%><%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf"%>

表单代码:

<sf:form action="add" method="post" commandName="emp">        <table width="500">            <tr>                <th><s:message code="lable.empmgr.emp.name" /><sf:input path="ename"/><sf:errors path="ename"/> </th>                <th><s:message code="lable.empmgr.emp.sal" /><sf:input path="sal"/></th>                <th><s:message code="lable.empmgr.emp.hiredate" /><sf:input path="hiredate"/><sf:errors path="hiredate" />  </th>                <th><s:message code="lable.empmgr.emp.job" /><sf:input path="job"/></th>                <th><s:message code="lable.empmgr.emp.partment" /><sf:input path="mgr"/></th>            </tr>            <tr/><td colspan="5">            <input type="submit" value="<s:message code='lable.empmgr.emp.op.add' />" /></td> </tr>        </table>    </sf:form>

此事例,仅验证ename和hiredate属性。

项目代码:

链接:http://pan.baidu.com/s/1i3i2COd 密码:dq4f

如有问题,请留言交流.

0 0
原创粉丝点击