spring mvc3(对象传参 Date 类型数据绑定)
2013-04-17 19:04
411 查看
User对象:
package login; import java.text.SimpleDateFormat; import java.util.Date; public class User { private String userName; private String password; private int age; private Date date; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return "userName:"+this.userName +",password:"+ this.password +",age:"+this.age+",date;"+ new SimpleDateFormat("yyyy-MM-dd").format(this.date); } }
controller
package login;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.stereotype.Controller;import org.springframework.validation.BindingResult;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.support.WebRequestDataBinder;@Controller@RequestMapping("/login")public class Login {@RequestMapping(value = "/login1",method = RequestMethod.GET)public String loginGet(){System.out.println("get");return "login";}@RequestMapping(value = "/login1",method = RequestMethod.POST)public String loginPost(User user ,BindingResult result){//提交form 时 自动注入 User 对象 但只能接收 String , int 类型的数据。//如果要接收 Date 类型的数据就得用下面的 数据绑定 。System.out.println(user);return "redirect:/login/succer.do";}@RequestMapping(value = "/succer")public String succer(){System.out.println("重定向了");return "login_succer";}//数据绑定@InitBinderpublic void initBinder(WebDataBinder binder) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");dateFormat.setLenient(false);binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//CustomDateEditor 可以换成自己定义的编辑器。//注册一个Date 类型的绑定器 。}}
相关文章推荐
- Spring mvc 数据绑定 String 绑定 Date
- spring mvc自动封装前端发送的json数据绑定为指定对象
- 自定义可绑定数据的业务对象实体和强类型-Part Four
- spring3mvc如何把checkbox的值绑定到model对象的int数据类型
- hibernate对象映射Date数据类型和input datetime使用注意事项
- hibernate对象映射Date数据类型和input datetime使用注意事项
- java 对象类型与基本数据类型传参的比较
- spring mvc各种常见类型参数绑定方式以及json字符串绑定对象
- 一起写框架-MVC框架-基础功能-Date类型数据绑定(七)
- springMVC中复杂嵌套对象、List等集合类型数据绑定
- springMVC对简单对象,Set,List,Map的数据绑定及类型转换
- 绑定用户控件赋值对象(非基本数据类型)
- SpringMVC数据绑定二(自定义数据类型与复杂数据类型对象)
- JavaEE_Mybatis_SpringMVC__Spring MVC中数据绑定之日期类型
- spring mvc绑定对象String转Date
- SpringMVC 自动绑定数据 - DATE多个类型格式 的数据绑定
- java 对象类型与基本数据类型传参的比较
- springMVC之对象中的基本类型数据绑定遇到的问题
- spring mvc表单复杂类型数据绑定
- Spring MVC中数据绑定之日期类型