Spring MVC 使用介绍(八)—— 注解式控制器(三):类型转换

一、概述

二、PropertyEditor

1、基本介绍

PropertyEditor用于 String<--->Object 之间相互转换,spring内建了一些常用的PropertyEditor,如:

ClassEditor:  String<——>Class
FileEditor:  String<——>File
PatternEditor:  String<——>Pattern
URLEditor:  String<——>URL
ResourceEditor:  String<——>Resource

自定义的PropertyEditor须继承自PropertyEditorSupport,示例如下:

实体类

public class UserInfo {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

转换类

public class UserEditor extends PropertyEditorSupport  {
    
    public UserEditor() {
        System.out.println("UserEditor constructed");
    }
    
    @Override
    public String getAsText() {
        UserInfo userInfo = (UserInfo) this.getValue();
        return JSON.toJSONString(userInfo);
    }

    @Override
    public void setAsText(String text) throws java.lang.IllegalArgumentException {
        if (StringUtils.isEmpty(text)) {
            return;
        }
        
        UserInfo userInfo = JSON.parseObject(text, UserInfo.class);
        this.setValue(userInfo);
    }
}
@Controller
public class TestController5 {

    @RequestMapping("/type1")
    @ResponseBody
    public String testType1(@RequestParam("user") UserInfo userInfo) {
        System.out.println(userInfo.getName() + " " + userInfo.getAge());
        return "testType1";
    }
    
    @RequestMapping("/type2")
    @ResponseBody
    public String testType2() {
     System.out.println("void");
return "testType2"; }
   // 注册UserEditor @InitBinder
public void initBinder(WebDataBinder binder) { UserEditor userEditor = new UserEditor(); binder.registerCustomEditor(UserInfo.class, userEditor); System.out.println("initBinder invoked"); } }

web.xml与spring-mvc.xml配置略

启动后,先后访问:

http://localhost:8080/myweb/type1?user={"name":"matt","age":30}
http://localhost:8080/myweb/type2
http://localhost:8080/myweb/type1?user={"name":"matt","age":30}

输出:

UserEditor constructed
initBinder invoked
matt 30
void
UserEditor constructed
initBinder invoked
matt 30

从输出结果可以看出,处理方法若包含需要类型转换的参数,每次请求都会调用注册方法;处理方法如不包含则不会调用

2、自定义PropertyEditor的注册

自定义PropertyEditor的注册有三种级别:

  • 控制器级别,使用@InitBinder
  • web级别,使用WebBindingInitializer
  • 应用级别,自定义PropertyEditor与实体类同包,且命名为“实体名 + Editor”

i)控制器级别

控制器级别是在控制器类中使用@InitBinder注解来实现,注册范围为单个控制器,如上例所示

ii)web级别

web级别使用WebBindingInitializer来实现,注册范围为应用的所有控制器(即整个web层),示例:

WebBindingInitializer实现类

public class MyWebBindingInitializer implements WebBindingInitializer {

	public void initBinder(WebDataBinder binder, WebRequest request) {
		binder.registerCustomEditor(UserInfo.class, new UserEditor());
		System.out.println("initBinder invoked!");
	}
}

注册

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">  
        <bean class="cn.matt.convertor.MyWebBindingInitializer"/>  
    </property> 
</bean> 

注释上例控制器中的注册方法,即可测试

该注册方法的调用方式与控制器级别相同,即处理方法若包含需要类型转换的参数,每次请求都会调用注册方法;处理方法如不包含则不会调用

iii)应用级别(推荐)

应用级别的注册范围为spring层,使用方式:自定义PropertyEditor与实体类同包,且命名为“实体名 + Editor”,示例:UserInfoEditor

该注册方法在每次类型转换时均创建新的实例

猜你喜欢

转载自www.cnblogs.com/MattCheng/p/9183501.html