最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC之@InitBinder注解詳解

 更新時(shí)間:2024年01月19日 10:10:19   作者:CUIYD_1989  
這篇文章主要介紹了SpringMVC之@InitBinder注解詳解,springmvc并不是能對所有類型的參數(shù)進(jìn)行綁定的,如果對日期Date類型參數(shù)進(jìn)行綁定,就會(huì)報(bào)錯(cuò)IllegalStateException錯(cuò)誤,需要的朋友可以參考下

@InitBinder注解的作用

springmvc并不是能對所有類型的參數(shù)進(jìn)行綁定的,如果對日期Date類型參數(shù)進(jìn)行綁定,就會(huì)報(bào)錯(cuò)IllegalStateException錯(cuò)誤。

所以需要注冊一些類型綁定器用于對參數(shù)進(jìn)行綁定。InitBinder注解就有這個(gè)作用。

程序代碼示例:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
@RequestMapping("/date")
public class InitBinderController {
    @RequestMapping(value = "/testInitBinder", method = RequestMethod.GET)
    private String testInitBinder(Date date) {
        System.out.println("date = " + date);
        return "RequsetInitBindDemo";
    }
}

postman測試:

在這里插入圖片描述

不能把String類型轉(zhuǎn)換為Date類型報(bào)錯(cuò)。

此時(shí)就需要一個(gè)日期類型轉(zhuǎn)換器。

import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
@RequestMapping("/date")
public class InitBinderController {
    @RequestMapping(value = "/testInitBinder", method = RequestMethod.GET)
    private String testInitBinder(Date date) {
        System.out.println("date = " + date);
        return "RequsetInitBindDemo";
    }
    @InitBinder
    public void dateTypeBinder(WebDataBinder webDataBinder) {
        //往數(shù)據(jù)綁定器中添加一個(gè)DateFormatter日期轉(zhuǎn)化器。
        webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));
    }
}

postman測試:

在這里插入圖片描述

打印結(jié)果:

date = Tue Jan 15 00:05:00 CST 2019

InitBinder注解源碼

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface InitBinder {

	//指定參數(shù)名,這個(gè)不知控制器方法上形參的參數(shù)名,而是請求參數(shù)名,
	//可以指定多個(gè)。指定后只有這些參數(shù)需要用到該轉(zhuǎn)換器。如果不指定,默認(rèn)所有。
	String[] value() default {};

}

注意:并且使用InitBinder 注冊的綁定器只有在當(dāng)前Controller中才有效,不會(huì)作用于其他Controller。

此時(shí),就需要用到@ControllerAdvice注解定義全局綁定器。使不同controller的方法都能作用到。

import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
@ControllerAdvice
public class InitConfig {
    @InitBinder
    public void dateTypeBinder(WebDataBinder webDataBinder) {
        //往數(shù)據(jù)綁定器中添加一個(gè)DateFormatter日期轉(zhuǎn)化器。
        webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));
    }
}

使用其他格式轉(zhuǎn)化器

我們可以自定義格式轉(zhuǎn)化器,實(shí)現(xiàn)Formatter接口就可。還可以添加驗(yàn)證器等等。

public class StringFormatter implements Formatter<String> {
    private static final String PREFIX = "convertString == ";

    @Override
    public String parse(String text, Locale locale) throws ParseException {
    	//所以String類型參數(shù)都加上一個(gè)前綴。
        String result = PREFIX + text;
        return result;
    }

    @Override
    public String print(String object, Locale locale) {
        return object;
    }
}

添加:

import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

@ControllerAdvice
public class InitConfig {

    @InitBinder
    public void dateTypeBinder(WebDataBinder webDataBinder) {
        //往數(shù)據(jù)綁定器中添加一個(gè)DateFormatter日期轉(zhuǎn)化器。
        webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));

        //添加一個(gè)string類型的數(shù)據(jù)綁定器,作用是加個(gè)前綴
        webDataBinder.addCustomFormatter(new StringFormatter());

    }
}

測試:

@RequestMapping(value = "/testInitBinder2", method = RequestMethod.GET)
    private String testInitBinder2(String name) {
        System.out.println("name = " + name);
        return "RequsetInitBindDemo";
    }

在這里插入圖片描述

打印結(jié)果:

name = convertString == 劉亦菲

到此這篇關(guān)于SpringMVC之@InitBinder注解詳解的文章就介紹到這了,更多相關(guān)@InitBinder注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

屯留县| 株洲市| 抚顺市| 信丰县| 渝北区| 宜宾市| 清河县| 宿松县| 高邑县| 天柱县| 延庆县| 通化县| 金山区| 龙岩市| 张家界市| 南木林县| 屯留县| 通州区| 苍溪县| 昂仁县| 河东区| 乌什县| 桂阳县| 江城| 饶河县| 隆回县| 宣化县| 新乡市| 双城市| 南靖县| 浏阳市| 临泽县| 六安市| 兰西县| 华坪县| 南江县| 乐至县| 柳河县| 宜昌市| 乌拉特后旗| 克山县|