Initializing parameter Bindings in SPRINGMVC

Source: Internet
Author: User
Tags dateformat

Initialization parameter bindings are similar to type conversions, with initialization bindings, mostly parameter types

---single date

Configuring binding methods in a processor class using @initbinder annotations

Here first register a user editor parameter one for the target type PropertyEditor for the property editor, where we use the Customdateeditor property editor,

Parameter one is the date format that you want to convert, and parameter two indicates whether NULL is allowed

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 Jingpeppe * * */@Controllerpublic class mycontroller{     //Processor method    @RequestMapping (value= "/first.do") public    String Dofirst (Date birthday,int age) {        System.out.println ("3333");        return "/welcome.jsp";    }            Customizing a method    @InitBinder public    void Initbinder (Webdatabinder binder) {        DateFormat df=new SimpleDateFormat ("Yyyy-mm-dd");        Binder.registercustomeditor (Date.class, New Customdateeditor (DF, True));    }    }

Just configure a package scanner on the 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/bea NS 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 "> <!--Package Scanner--<context:component-scan base-package= "Cn.controller" ></context:component-scan> </bea Ns>

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" >

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" >

---multiple date formats

Configuration steps:

Using our custom property editor in the processor class

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; @Controllerpublic class mycontroller{     //Processor Method    @ Requestmapping (value= "/first.do") public    String Dofirst (Date birthday,int age) {        System.out.println ("3333") ;        return "/welcome.jsp";    }            Customize a method    @InitBinder public    void Initbinder (Webdatabinder binder) {        System.out.println ("==========");        Binder.registercustomeditor (Date.class, New Mydateeditor ());    }    }

Property Editor

At this point we need to consider which property editor we need to define our own property editor

The approximate configuration method is similar to the single date, only need to change the attribute edit

The custom property editor requires us to inherit propertieseditor, rewrite the Setastext method inside, and use the SetValue method to assign a value

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 Jingpeppe * */public class Mydateeditor extends Pro pertieseditor{/** * Source: Date of String type */@Override public void Setastext (string source) throws Illegalargum                Entexception {//Once error, automatically dispatched to the exception processor 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; }}

Initializing parameter Bindings in SPRINGMVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.