初始化参数绑定与类型转换很类似,初始化绑定时,主要是参数类型
---单日期
在处理器类中配置绑定方法 使用@InitBinder注解
在这里首先注册一个用户编辑器 参数一为目标类型 propertyEditor为属性编辑器,此处我们选用 CustomDateEditor属性编辑器,
参数一为想转换的日期格式,参数二表示是否允许为空
package cn.controller; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; /** * * @author 景佩佩 * */ @Controller public class MyController{ //处理器方法 @RequestMapping(value="/first.do") public String doFirst(Date birthday,int age) { System.out.println("3333"); return "/welcome.jsp"; } //自定义一个方法 @InitBinder public void initBinder(WebDataBinder binder){ DateFormat df=new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true)); } }
只需在applicationContext.xml配置一个包扫描器就行
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 包扫描器 --> <context:component-scan base-package="cn.controller"></context:component-scan> </beans>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <style type="text/css"> form{ background-color:pink; width:500px; } </style> <title></title> </head> <body> ${ex.message } <form action="${pageContext.request.contextPath }/first.do" method="post"> <h1>自定义类型转换器</h1> 出生日期:<input name="birthday" value="${birthday }"/>${birthdayerror }<br/><br/> 年龄:<input name="age" value="${age }"/>${ageerror }<br/><br/> <input type="submit" value="注册"/> </form> </body> </html>
welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>欢迎页面</title> </head> <body> <h1>欢迎访问${uname }${param.uage }</h1> </body> </html>
---多日期格式
配置步骤:
在处理器类中使用我们自定的属性编辑器
MyController.java
package cn.controller; import java.util.Date; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyController{ //处理器方法 @RequestMapping(value="/first.do") public String doFirst(Date birthday,int age) { System.out.println("3333"); return "/welcome.jsp"; } //自定义一个方法 @InitBinder public void initBinder(WebDataBinder binder){ System.out.println("=========="); binder.registerCustomEditor(Date.class, new MyDateEditor()); } }
属性编辑器
此时我们需要考虑使用哪个属性编辑器,需要定义自己的属性编辑器
大致的配置方式如单日期相似,只需要更换属性编辑即可
自定义的属性编辑器,需要我们继承PropertiesEditor,重写里面的setAsText方法,使用setValue方法赋值
MyDateEditor.java
package cn.controller; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Pattern; import org.springframework.beans.TypeMismatchException; import org.springframework.beans.propertyeditors.PropertiesEditor; /** * * @author 景佩佩 * */ public class MyDateEditor extends PropertiesEditor{ /** * source:字符串型的日期 */ @Override public void setAsText(String source) throws IllegalArgumentException { //一旦报错,自动调度到异常处理器 SimpleDateFormat sdf=getDateFormat(source); try { Date date = sdf.parse(source); setValue(date); } catch (ParseException e) { e.printStackTrace(); } } private SimpleDateFormat getDateFormat(String source){ SimpleDateFormat sdf=new SimpleDateFormat(); if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)) { sdf=new SimpleDateFormat("yyyy-MM-dd"); }else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)) { sdf=new SimpleDateFormat("yyyy/MM/dd"); }else if (Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)) { sdf=new SimpleDateFormat("yyyyMMdd"); }else { throw new TypeMismatchException("",Date.class); } return sdf; } }
时间: 2024-10-08 19:35:20