初始化参数绑定与类型转换很类似,初始化绑定时,主要是参数类型

---单日期

在处理器类中配置绑定方法  使用@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;
    }
}

SpringMvc中初始化参数绑定的更多相关文章

  1. springmvc的初始化参数绑定

    一.springmvc的初始化参数绑定 此种和我们之前说的类型转换非常相似,可以看作是一种类型转换 在初始化参数绑定时  重要的是参数类型 -------------------单日期的绑定 二. 配 ...

  2. SpringMVC中的参数绑定总结

    众所周知,springmvc是用来处理页面的一些请求,然后将数据再通过视图返回给用户的,前面的几篇博文中使用的都是静态数据,为了能快速入门springmvc,在这一篇博文中,我将总结一下springm ...

  3. 【SpringMVC学习05】SpringMVC中的参数绑定总结——较乱后期准备加入 同一篇幅他人的参数绑定

    众所周知,springmvc是用来处理页面的一些请求,然后将数据再通过视图返回给用户的,前面的几篇博文中使用的都是静态数据,为了能快速入门springmvc,在这一篇博文中,我将总结一下springm ...

  4. SpringMVC中的参数绑定

    SpringMVC中的参数绑定 参数绑定的定义 所谓参数绑定,简单来说就是客户端发送请求,而请求中包含一些数据,那么这些数据怎么到达 Controller.从客户端请求key/value数据(比如ge ...

  5. SpringMVC 的初始化参数绑定

    初始化参数绑定:日期格式 一:首先我们先做一种日期格式的绑定,配置初始化参数绑定和自定义类型转换有着异曲同工之妙 配置步骤如下: 1.我们首先配置applicationContext.xml,进行扫描 ...

  6. SpringMVC初始化参数绑定--日期格式

    一.初始化参数绑定[一种日期格式] 配置步骤: ①:在applicationcontext.xml中只需要配置一个包扫描器即可 <!-- 包扫描器 --> <context:comp ...

  7. Spring MVC初始化参数绑定

    初始化参数绑定与类型转换很类似,初始化绑定时,主要是参数类型 ---单日期 在处理器类中配置绑定方法  使用@InitBinder注解 在这里首先注册一个用户编辑器 参数一为目标类型   proper ...

  8. [Spring MVC] - SpringMVC的各种参数绑定方式

    SpringMVC的各种参数绑定方式 1. 基本数据类型(以int为例,其他类似):Controller代码: @RequestMapping("saysth.do") publi ...

  9. SpringMVC的各种参数绑定方式

    1. 基本数据类型(以int为例,其他类似):2. 包装类型(以Integer为例,其他类似):3. 自定义对象类型:4. 自定义复合对象类型:5. List绑定:6. Set绑定:7. Map绑定: ...

随机推荐

  1. 一起来玩echarts系列(一)------箱线图的分析与绘制

    一.箱线图 Box-plot 箱线图一般被用作显示数据分散情况.具体是计算一组数据的中位数.25%分位数.75%分位数.上边界.下边界,来将数据从大到小排列,直观展示数据整体的分布情况. 大部分正常数 ...

  2. 百度推出新技术 MIP,网页加载更快,广告呢?

    我们在2016年年初推出了MIP,帮助移动页面加速(原理).内测数据表明,MIP页面在1s内加载完成.现在已经有十多家网站加入MIP项目,有更多的网站正在加入中.在我们收到的反馈中,大部分都提到了广告 ...

  3. 隐私泄露杀手锏 —— Flash 权限反射

    [简版:http://weibo.com/p/1001603881940380956046] 前言 一直以为该风险早已被重视,但最近无意中发现,仍有不少网站存在该缺陷,其中不乏一些常用的邮箱.社交网站 ...

  4. C# i=0;i=i++,i的值是多少?

    昨天看群里dalao们聊天,有一个人出来问这个问题 这个题应该是挺常见的 int i = 0, t;        for(t = 0;t <= 5;t++)        {          ...

  5. C# MVC 5 - 生命周期(应用程序生命周期&请求生命周期)

    本文是根据网上的文章总结的. 1.介绍 本文讨论ASP.Net MVC框架MVC的请求生命周期. MVC有两个生命周期,一为应用程序生命周期,二为请求生命周期. 2.应用程序生命周期 应用程序生命周期 ...

  6. ASP.NET Core project.json imports 是什么意思?

    示例代码: "frameworks": { "netcoreapp1.0.0": { "imports" : "portable- ...

  7. CSharpGL(33)使用uniform块来优化对uniform变量的读写

    CSharpGL(33)使用uniform块来优化对uniform变量的读写 +BIT祝威+悄悄在此留下版了个权的信息说: Uniform块 如果shader程序变得比较复杂,那么其中用到的unifo ...

  8. ASP.NET MVC5----常见的数据注解和验证

    只要一直走,慢点又何妨. 在使用MVC模式进行开发时,数据注解是经常使用的(模型之上操作),下面是我看书整理的一些常见的用法. 什么是验证,数据注解 验证 从全局来看,发现逻辑仅是整个验证的很小的一部 ...

  9. C#制作简易屏保

    前言:前段时间,有个网友问我C#制作屏保的问题,我瞬间懵逼了(C#还可以制作屏保!).于是我去查阅相关资料,下面把C#如何制作屏保的过程及我学习过程的心得也记录下来,希望对需要的人能有帮助. 基本思路 ...

  10. python安装BeautifulSoup注意事项

    好久没有写爬虫了,最近用Python的BeautifulSoup4.Scrapy分别对以前写的spider进行优化,发现python3.5后这些库变化了很多,遇到了许多问题,在这里做一下总结. 切换环 ...