SpringMVC数据绑定四(自定义的类型转换器)

1、PropertyEditor

  继承PropertyEditorSupport

//controller
@Controller
public class TestController extends PropertyEditorSupport{
     //重写PropertyEditorSupport的setAsText方法
@Override
    public void setAsText(String text) throws IllegalArgumentException {
        User u = new User();
        String[] textArray = text.split(",");
        u.setName(textArray[0]);
        u.setAge(Integer.parseInt(textArray[1]));
        this.setValue(u);
    }
    
//TODO: http://127.0.0.1:8080/SpringMVC/propertyEditir?text=Tom,20 @RequestMapping(value
="propertyEditir") @ResponseBody public String propertyEditir(String text) { this.setAsText(text); return ((User)this.getValue()).toString(); } }

2、Formatter

posted @ 2017-10-20 12:09  watermelonBan  阅读(322)  评论(0编辑  收藏  举报