Spring mvc之JSR303框架实现数据格式化,数据验证,资源国际化
来源:互联网 发布:php用户注册系统源码 编辑:程序博客网 时间:2024/05/16 15:33
今天使用JSR303框架实现 mvc的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
如有问题,请留言交流.
- Spring mvc之JSR303框架实现数据格式化,数据验证,资源国际化
- Spring MVC JSR303数据校验
- spring JSR303进行数据验证
- spring mvc jsr303验证
- spring mvc国际化资源实现
- Spring mvc 数据验证框架注解
- Spring MVC 数据验证
- springmvc之JSR303数据校验
- 在spring mvc中实现JSR303验证并将验证结果传回给视图层
- Spring MVC数据校验与国际化
- spring MVC JSR303校验
- 浅析JSR303 验证作用、使用及使用场景、spring mvc下使用JSR303
- 使用JSR-303规范实现Spring MVC数据验证
- Spring MVC学习(七)-------SpringMVC数据格式化
- Spring mvc数据转换与格式化
- Spring mvc数据转换与格式化
- Spring mvc中数据绑定和格式化
- Spring mvc数据转换与格式化
- 在用enum定义错误码时, 最好显式赋值
- Android源码的差分升级
- HTTP对外接口,如何增加签名机制
- 关于瑞典城市Idre城市天气未获取到的问题
- 文件遍历os.walk方法
- Spring mvc之JSR303框架实现数据格式化,数据验证,资源国际化
- CAAnimation动画/CAAnimation Group
- nfs服务器的简单搭建过程-记录方法
- 视图模型转换矩阵的推导也是关于gluLookAt函数即视图转换矩阵的推导及代码实现
- svn的import命令
- AE开发报错解决
- Android平台定义的主题样式
- 安卓网络优化(5)
- 二次注入审计问题