使用FormatterRegister注册Formatter

来源:互联网 发布:合肥怎么样知乎 编辑:程序博客网 时间:2024/06/06 11:36

 代码:

registerForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>测试FormatterRegistrar接口</title></head><body><h3>注册页面</h3><form action="register" method="post">     <table>         <tr>         <td><label>登录名: </label></td>             <td><input type="text" id="loginname" name="loginname" ></td>         </tr>          <tr>         <td><label>生日: </label></td>             <td><input type="text" id="birthday" name="birthday" ></td>         </tr>         <tr>             <td><input id="submit" type="submit" value="登录"></td>         </tr>     </table></form></body></html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>测试FormatterRegistrar接口</title></head><body>登录名:${requestScope.user.loginname }<br>生日:<fmt:formatDate value="${requestScope.user.birthday}" pattern="yyyy年MM月dd日"/><br></body></html>

User.java

package com.bean;import java.io.Serializable;import java.util.Date;// 域对象,实现序列化接口public class User implements Serializable{private String loginname;private Date birthday;public User() {super();// TODO Auto-generated constructor stub}public String getLoginname() {return loginname;}public void setLoginname(String loginname) {this.loginname = loginname;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}@Overridepublic String toString() {return "User [loginname=" + loginname + ", birthday=" + birthday + "]";}}

DateFormatter.java

package com.formatter;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import org.springframework.format.Formatter;// 实现Converter<S,T>接口public class DateFormatter implements Formatter<Date>{// 日期类型模板:如yyyy-MM-ddprivate String datePattern;// 日期格式化对象private SimpleDateFormat dateFormat;// 构造器,通过依赖注入的日期类型创建日期格式化对象public DateFormatter(String datePattern) {this.datePattern = datePattern;this.dateFormat = new SimpleDateFormat(datePattern);}// 显示Formatter<T>的T类型对象@Overridepublic String print(Date date, Locale locale) {return dateFormat.format(date);}// 解析文本字符串返回一个Formatter<T>的T类型对象。@Overridepublic Date parse(String source, Locale locale) throws ParseException {try {return dateFormat.parse(source);} catch (Exception e) {throw new IllegalArgumentException();}}}

MyFormatterRegistrar.java

package com.formatter;import org.springframework.format.FormatterRegistrar;import org.springframework.format.FormatterRegistry;public class MyFormatterRegistrar implements FormatterRegistrar{private DateFormatter dateFormatter;public void setDateFormatter(DateFormatter dateFormatter) {this.dateFormatter = dateFormatter;}@Overridepublic void registerFormatters(FormatterRegistry registry) {registry.addFormatter(dateFormatter);}}

UserController.java

package com.control;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.bean.User;@Controllerpublic class UserController{private static final Log logger = LogFactory.getLog(UserController.class); @RequestMapping(value="/{formName}") public String loginForm(@PathVariable String formName){// 动态跳转页面return formName;}  @RequestMapping(value="/register",method=RequestMethod.POST) public String register(@ModelAttribute User user,Model model) { logger.info(user); model.addAttribute("user", user);     return "success"; }}

截图:




0 0
原创粉丝点击