- 内容提要
- 作者简介
- 译者简介
- 前言
- HTTP
- Servlet 和 JSP
- 下载 Spring 或使用 STS 与 Maven/Gradle
- 手动下载 Spring
- 使用 STS 和 Maven/Gradle
- 下载 Spring 源码
- 本书内容简介
- 下载示例应用
- 第 1 章Spring 框架
- 第 2 章模型 2 和 MVC 模式
- 第 3 章Spring MVC 介绍
- 第 4 章基于注解的控制器
- 第 5 章数据绑定和表单标签库
- 第 6 章转换器和格式化
- 第 7 章验证器
- 第 8 章表达式语言
- 第 9 章JSTL
- 第 10 章国际化
- 第 11 章上传文件
- 第 12 章下载文件
- 第 13 章应用测试
- 附录 A Tomcat
- 附录 B Spring Tool Suite 和 Maven
- 附录 C Servlet
- 附录 D JavaServer Pages
- 附录 E 部署描述符
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
6.3 用 Registrar 注册 Formatter
注册 Formatter 的另一种方法是使用 Registrar。例如,清单 6.7 就是注册 DateFormatter 的一个例子。
清单 6.7 MyFormatterRegistrar 类
package formatter;
import org.springframework.format.FormatterRegistrar;
import org.springframework.format.FormatterRegistry;
public class MyFormatterRegistrar implements FormatterRegistrar {
private String datePattern;
public MyFormatterRegistrar(String datePattern) {
this.datePattern = datePattern;
}
@Override
public void registerFormatters(FormatterRegistry registry) {
registry.addFormatter(new LocalDateFormatter(datePattern));
// register more formatters here
}
}
有了 Registrar,就不需要在 Spring MVC 配置文件中注册任何 Formatter 了,只在 Spring 配置文件中注册 Registrar 就可以了,如清单 6.8 所示。
清单 6.8 在 springmvc-config.xml 文件中注册 Registrar
< ?xml version="1.0" encoding="UTF-8"?>
< beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/springcontext.xsd">
< context:component-scan base-package="controller" />
< context:component-scan base-package="formatter" />
< mvc:annotation-driven conversion-service="conversionService" />
< mvc:resources mapping="/css/ **" location="/css/" /> < mvc:resources mapping="/ *.html" location="/" /> < bean id="viewResolver" class="org.springframework.web.servlet.view. →InternalResourceViewResolver"> < property name="prefix" value="/WEB-INF/jsp/" /> < property name="suffix" value=".jsp" /> < /bean> < bean id="conversionService" class="org.springframework.format.support. →FormattingConversionServiceFactoryBean"> < property name="formatterRegistrars"> < set> < bean class="formatter.MyFormatterRegistrar"> < constructor-arg type="java.lang.String" value="MM-dd-yyyy" /> < /bean> < /set> < /property> < /bean> < /beans>
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论