1. 程式人生 > >SSM-SpringMVC-29:SpringMVC中InitBinder的初步

SSM-SpringMVC-29:SpringMVC中InitBinder的初步

ger 1.0 類型 body pub 執行順序 row servle class

------------吾亦無他,唯手熟爾,謙卑若愚,好學若饑-------------

之前博客的配置日期類型轉換器,他是全局的,如果只是一個處理器中使用怎麽辦?

引出@InitBinder註解

案例:

  1.在處理器中:

package cn.dawn.day22initbinder;

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;

import java.text.SimpleDateFormat;
import java.util.Date;

/** * Created by Dawn on 2018/3/31. */ @Controller public class FirstController { /*第一種*/ /*@InitBinder*/ /*第二種*/ @InitBinder("birthday") public void initBinder(WebDataBinder binder){ System.out.println("1111111111"); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"
); binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true)); } /*initbinder*/ @RequestMapping("/initbinderFirst") public String initbinderFirst(String username, Integer userage, Date birthday) throws Exception { System.out.println("2222222222"); System.
out.println(username); System.out.println(userage); System.out.println(birthday); return "success"; } }

  此處需要重點解釋一波,這樣就可以日期類型轉換器變成局部的了,倆個方法執行順序正如輸出的那樣一個是1,一個是2,@InitBinder註解它加參數是說只匹配birthday,如果不加,下面那個方法有三個參數,上面的方法就執行三次,System.out.print("11111111")三次

  2.自己的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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">

    <!--包掃描器-->
    <context:component-scan base-package="cn.dawn.day22initbinder"></context:component-scan>
    <!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/day22/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>


</beans>

  3.修改web.xml配置文件的中央調度器的上下問配置位置為上面的那個xml

  4.jsp頁面:

    4.1login.jsp

<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>登錄</h2>
<form action="${pageContext.request.contextPath}/initbinderFirst" method="post">

    用戶名:<input name="username" value="${username}">
    年齡:<input name="userage">
    生日:<input name="birthday">

    <input type="submit" value="登錄"/>
</form>
</body>
</html>

    4.2success.jsp頁面

<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
<html>
<body>
<%--<img src="image/1.jpg">--%>
<h2>Success!</h2>
</body>
</html>

  5.啟動tomcat,訪問login.jsp,發現只有yyyy-MM-dd可以轉換成功,說明它已經和springmvc默認的不同了,同樣的問題,兼容性不好,所以,下一篇博客為大家做進一步的優化

SSM-SpringMVC-29:SpringMVC中InitBinder的初步