Spring MVC初始化参数绑定
初始化参数绑定与类型转换很类似,初始化绑定时,主要是参数类型
---单日期
在处理器类中配置绑定方法 使用@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;
}
}

Spring MVC初始化参数绑定的更多相关文章
- Spring MVC温故而知新 – 参数绑定、转发与重定向、异常处理、拦截器
请求参数绑定 当用户发送请求时,根据Spring MVC的请求处理流程,前端控制器会请求处理器映射器返回一个处理器,然后请求处理器适配器之心相应的处理器,此时处理器映射器会调用Spring Mvc 提 ...
- Spring MVC请求参数绑定
所谓请求参数绑定,就是在控制器方法中,将请求参数绑定到方法参数上 @RequestParam 绑定单个请求参数到方法参数上 @RequestParam("id") Integer ...
- Spring MVC请求参数绑定 自定义类型转化 和获取原声带额servlet request response信息
首先还在我们的框架的基础上建立文件 在domian下建立Account实体类 import org.springframework.stereotype.Controller; import org. ...
- springmvc的初始化参数绑定
一.springmvc的初始化参数绑定 此种和我们之前说的类型转换非常相似,可以看作是一种类型转换 在初始化参数绑定时 重要的是参数类型 -------------------单日期的绑定 二. 配 ...
- SpringMvc中初始化参数绑定
初始化参数绑定与类型转换很类似,初始化绑定时,主要是参数类型 ---单日期 在处理器类中配置绑定方法 使用@InitBinder注解 在这里首先注册一个用户编辑器 参数一为目标类型 proper ...
- SpringMVC初始化参数绑定--日期格式
一.初始化参数绑定[一种日期格式] 配置步骤: ①:在applicationcontext.xml中只需要配置一个包扫描器即可 <!-- 包扫描器 --> <context:comp ...
- SpringMVC 的初始化参数绑定
初始化参数绑定:日期格式 一:首先我们先做一种日期格式的绑定,配置初始化参数绑定和自定义类型转换有着异曲同工之妙 配置步骤如下: 1.我们首先配置applicationContext.xml,进行扫描 ...
- Spring MVC接收参数(Map,List,JSON,Date,2个Bean)(记录一次面试惨状)
题目Spring MVC 接收参数 MapListDate2个BeanJSON Spring MVC接收参数 -Map Spring MVC接收参数 -List Spring MVC接收参数 -dat ...
- spring mvc 复杂参数注入
过了这么久,又重新把博客拾起来了 来上海工作也已经有将近两周的时间了, 今天在整理项目的时候,遇到了一个关于参数注入的问题 背景: 我的开发前台用的是extjs4,在对后台spring mvc提交表单 ...
随机推荐
- Database Replay和Consolidated Database replay
简介 在数据库的迁移和升级场景中,我们经常会遇到一个问题:在做压力测试时,如何模拟真实的业务压力,解决这个问题的方法有很多,比如:应用方开发模拟程序或者使用压力测试工具模拟,如load runner, ...
- javascript中的this与函数讲解
前言 javascript中没有块级作用域(es6以前),javascript中作用域分为函数作用域和全局作用域.并且,大家可以认为全局作用域其实就是Window函数的函数作用域,我们编写的js代码, ...
- [译]ZOOKEEPER RECIPES-Leader Election
选主 使用ZooKeeper选主的一个简单方法是,在创建znode时使用Sequence和Ephemeral标志.主要思想是,使用一个znode,比如"/election",每个客 ...
- 02.LoT.UI 前后台通用框架分解系列之——灵活的菜单栏
LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...
- document.documentElement.clientHeight 与 document.body.clientHeight(杜绝千篇一律的抄袭!!)
document.documentElement.clientHeight 与 document.body.clientHeight用来获取页面可视高度我觉得有点问题.这两个应该不是一个东西. 页面中 ...
- 使用Java原生代理实现AOP
### 本文由博主柒.原创,转载请注明出处 ### 完整源码下载地址 [https://github.com/MatrixSeven/JavaAOP](https://github.com/Matri ...
- iOS逆向工程之KeyChain与Snoop-it
今天博客的主题是Keychain, 在本篇博客中会通过一个登陆的Demo将用户名密码存入到KeyChain中,并且查看一下KeyChain中存的是什么东西,把这些内容给导出来.当然本篇博客的重点不是如 ...
- javascript 判断参数类型大全
js 判断类型的在开发中是很常用的,因为js 是弱类型的语言,var 可以接受任何形式的类型,但是在真正的开发中,我们需要根据不同类型做不同的处理,所以这个是必须的精通. 首先需要知道 typeof这 ...
- 记一次.NET代码重构
好久没写代码了,终于好不容易接到了开发任务,一看时间还挺充足的,我就慢慢整吧,若是遇上赶进度,基本上直接是功能优先,完全不考虑设计.你可以认为我完全没有追求,当身后有鞭子使劲赶的时候,神马设计都是浮云 ...
- 听H3絮叨:何以让天下没有难用的流程
最近朋友圈.网站新闻铺天盖地是"让天下没有难用的流程",有人就要问了,H3 BPM何德何能,为BPM站台,让天下没有难用的流程? 这是一个关于"办公室空想"的故 ...