springMVC 格式化

来源:互联网 发布:魏强斌 知乎 编辑:程序博客网 时间:2024/06/05 23:57

自定义格式化(用于显示在JSP页面)

 

1、定义model类(包含属性类Date)

package hb.base.privilege.model;import java.util.Date;import javax.validation.constraints.NotNull;import org.hibernate.validator.constraints.Length;public class Resource {@NotNull(message = "用户名不能为空")private String resid;//资源的唯一标示@Length(min=5, max=20, message="用户名长度必须在5-20之间")private String resname;//资源的名称private String commen;//备注private Date age;public Date getAge() {return age;}public void setAge(Date age) {this.age = age;}public String getResid() {return resid;}public void setResid(String resid) {this.resid = resid;}public String getResname() {return resname;}public void setResname(String resname) {this.resname = resname;}public String getCommen() {return commen;}public void setCommen(String commen) {this.commen = commen;}}

 

2、自定义需要继承Formatter(泛型)接口

package hb.base.convert;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import org.springframework.format.Formatter;public class DateFormatter implements Formatter<Date> {private String dateFormatPattern="yyyy-MM-dd";public DateFormatter(String dateFormatPattern) {this.dateFormatPattern = dateFormatPattern;}//如果将对象在JSP页面展示的时候则运行下面的方法@Overridepublic String print(Date date, Locale arg1) {if(date == null) {return "";}DateFormat df = new SimpleDateFormat(dateFormatPattern);return new StringBuilder().append(df.format(date)).toString();}//提交表单的时候遇到的属性是Date类型则会执行下面方法@Overridepublic Date parse(String src, Locale arg1) throws ParseException {if(src == null || "".equals(src)) {return null;}DateFormat df = new SimpleDateFormat(dateFormatPattern);return df.parse(src);//return null;}}

 

3、配置如下(在整个项目中一直都起作用)

<!-- ①注册ConversionService --><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><!--<property name="converters">   <list><bean class="hb.base.convert.DateConverter"><constructor-arg value="yyyy-MM-dd"/></bean></list></property>--><!-- 格式化显示的配置--><property name="formatters"><list><bean class="hb.base.convert.DateFormatter"><constructor-arg value="yyyy-MM-dd"/></bean></list></property></bean><!-- ②使用 ConfigurableWebBindingInitializer 注册conversionService --><bean id="webBindingInitializer"class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"><property name="conversionService" ref="conversionService" /><property name="validator" ref="validator" /></bean><!--Spring3.1开始的注解 HandlerAdapter --><beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"><!--线程安全的访问session --><property name="synchronizeOnSession" value="true" /><property name="webBindingInitializer" ref="webBindingInitializer"/></bean>

 

备注:配置和类型转换相似,只是FormattingConversionServiceFactoryBean类需要注入formatters属性。

 

4、JSP页面展示

JSP页面需要引用<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %><%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>1)使用表单的方式显示<form:form commandName="resource">     <form:input path="age"/><br/> </form:form>2)只是显示文字内容<spring:bind path="resource.age">${status.value}</spring:bind>备注:${status.value}一定写,并且内容不能变,这个表示是当前状态的值

 

可以参考 http://hbiao68.iteye.com/blog/2020171 这边文章,可以注解@InitBinder 来实现针对于具体的controller类来完成“局部格式化”;通过继承PropertyEditorSupport 类

 

0 0
原创粉丝点击