注:通過InitBinder方法注冊PropertyEditor,在@ResponseBody
注解的方法是無效的
1.自己定義一個類,這個類實現PropertyEditor接口,其實就是實現這個接口中的setAsText和getValue
1 springmvc會調用setAsText方法將request中對應的值傳遞進去
2 springmvc會調用getValue方法,將拿到的值賦值到controller中的form實體類中
下面實例顯示的是無論前臺傳遞什么參數,最終User的name都是"小剛"
public class MyStringPropertyEditor implements PropertyEditor{@Overridepublic Object getValue() {return "小剛";}@Overridepublic void setAsText(String text) throws IllegalArgumentException {System.out.println("request中傳遞過來的值:"+text);}//省略其他Override方法
}
controller
@Controller
@RequestMapping("initbinder")
public class Controller {@InitBinderpublic void aa(WebDataBinder binder) {MyStringPropertyEditor sdf=new MyStringPropertyEditor();//表示User121中如果是String類型,就用MyStringPropertyEditor屬性編輯器binder.registerCustomEditor(String.class, sdf);}@ResponseBody@RequestMapping("test")public String aa(User121 user) {//無論URL傳遞什么,此處都會打印"小剛"System.out.println("controller:"+user.getName());return "GG";}
}
jsonpropertyorder注解,URL中雖然寫的是周傳雄,但是進入到controller中,依然變成了小剛
http://localhost:8080/initbinder/test?name=周傳雄
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态