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

今天使用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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一缕阳光直射你的心扉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值