BaseController处理表单数据绑定问题

一、第一步写个基础控制类
 import java.text.SimpleDateFormat;
 2 import java.util.Date;
 3
 4 import org.springframework.beans.propertyeditors.CustomDateEditor;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.WebDataBinder;
 7 import org.springframework.web.bind.annotation.InitBinder;
 8
 9 import sun.beans.editors.DoubleEditor;
10 import sun.beans.editors.FloatEditor;
11 import sun.beans.editors.IntEditor;
12 import sun.beans.editors.LongEditor;
13
14 @Controller
15 public class BaseController {
16
17     @InitBinder    
18     public void initBinder(WebDataBinder binder) {    
19
20         binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
21         binder.registerCustomEditor(int.class, new IntEditor());
22         binder.registerCustomEditor(long.class, new LongEditor());  
23         binder.registerCustomEditor(double.class, new DoubleEditor());  
24         binder.registerCustomEditor(float.class, new FloatEditor());  
25     }
26     
27     
28 }

二、第二部继承过来
import org.springframework.stereotype.Controller;
 2 import org.springframework.web.bind.annotation.RequestMapping;
 3
 4 import com.kickstarter.entity.User;
 5
 6 @Controller("userController") 继承过来
 7 public class UserController extends BaseController{
 8
 9     @RequestMapping(value="regUser")
10     public String dateTest(User user , double dd){
11         
12         System.out.println( user.toString() );
13         System.out.println( dd );
14         return "index";
15     }
16 }

三、注解方式

import java.util.Date;
 2
 3 import org.springframework.format.annotation.DateTimeFormat;
 4
 5 public class User {
 6
 7     private int userId;
 8     private String userName;
 9     
10     @DateTimeFormat(pattern="yyyy-MM-dd")//这里加注解
11     private Date createDate;
12     
13     public User() {}
14
15     public User(int userId, String userName, Date createDate) {
16         super();
17         this.userId = userId;
18         this.userName = userName;
19         this.createDate = createDate;
20     }
21
22     public User(String userName, Date createDate) {
23         super();
24         this.userName = userName;
25         this.createDate = createDate;
26     }
27
28     public int getUserId() {
29         return userId;
30     }
31
32     public void setUserId(int userId) {
33         this.userId = userId;
34     }
35
36     public String getUserName() {
37         return userName;
38     }
39
40     public void setUserName(String userName) {
41         this.userName = userName;
42     }
43
44     public Date getCreateDate() {
45         return createDate;
46     }
47
48     public void setCreateDate(Date createDate) {
49         this.createDate = createDate;
50     }
51
52     @Override
53     public String toString() {
54         return "User [createDate=" + createDate + ", userId=" + userId
55                 + ", userName=" + userName + "]";
56     }
57 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值