Spring MVC參數封裝傳遞

在Spring MVC中,前端JSP頁面能夠傳遞  基本類型(int,String)、實體類型、包裝類型、數組類型、集合類型(List、map )等。前端

假如在傳遞的類型中有 Date類型的字段,須要在 Controller 經過  initBinder()  進行處理,代碼以下:java

@Controller
public class userController {
 
    /*
     * 添加用戶
     * 經過基本參數封裝獲取參數
     */
    @RequestMapping(value = "/user/addUser2", method = RequestMethod.POST)
    public ModelAndView addUser2(String username,String usercode,Date birthday,String address) {
        ModelAndView modelAndView = new ModelAndView();
        userModel model = new userModel();
        model.setUserName(username);
        model.setUserCode(usercode);
        model.setBirthday(birthday);
        model.setAddress(address);
        modelAndView.setViewName("/user/list");
        modelAndView.addObject("user", model);
        return modelAndView;
    }
      
    //處理日期類型參數
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}

 

一、HttpServletRequest 獲取參數

JSP頁面:數組

<div style="width: 28%; float: left;">
        <fieldset>
            <legend>普通提交-request.getParameter(args) 獲取參數</legend>
            <form action="${pageContext.request.contextPath }/user/addUser"
                method="post">
                <div style="width: 200px">
                    <label>名字:</label> <input name="username" id="username"
                        placeholder="請輸入名字" />
                </div>
                <div style="width: 200px">
                    <label>編號:</label> <input name="usercode" id="usercode"
                        placeholder="請輸入編號" />
                </div>
                <div style="width: 200px">
                    <label>生日:</label> <input name="birthday" id="birthday"
                        placeholder="請輸入生日" />
                </div>
                <div style="width: 200px">
                    <label>地址:</label> <input name="address" id="address"
                        placeholder="請輸入地址" />
                </div>
                <div style="width: 200px">
                    <button type="reset">重置</button>
                    <button type="submit">提交</button>
                </div>
            </form>
        </fieldset>
    </div>

Controller:app

/*
     * 添加用戶
     * 經過 request.getParameter(args) 獲取參數
     */
    @RequestMapping(value = "/user/addUser", method = RequestMethod.POST)
    public ModelAndView addUser(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView();
        userModel model = new userModel();
        model.setUserName(request.getParameter("username").toString());
        model.setUserCode(request.getParameter("usercode").toString());
        model.setAddress(request.getParameter("address").toString()); 
        modelAndView.setViewName("/user/list");
        modelAndView.addObject("user", model);
        return modelAndView;
    }

 

二、基本類型獲取參數

JSP頁面: ide

<div style="width: 28%; float: left;">
        <fieldset>
            <legend>普通提交-基本參數取值</legend>
            <form action="${pageContext.request.contextPath }/user/addUser2"
                method="post">
                <div style="width: 200px">
                    <label>名字:</label> <input name="username" id="username"
                        placeholder="請輸入名字" />
                </div>
                <div style="width: 200px">
                    <label>編號:</label> <input name="usercode" id="usercode"
                        placeholder="請輸入編號" />
                </div>
                <div style="width: 200px">
                    <label>生日:</label> <input name="birthday" id="birthday"
                        placeholder="請輸入生日" />
                </div>
                <div style="width: 200px">
                    <label>地址:</label> <input name="address" id="address"
                        placeholder="請輸入地址" />
                </div>
                <div style="width: 200px">
                    <button type="reset">重置</button>
                    <button type="submit">提交</button>
                </div>
            </form>
        </fieldset>
    </div>  

 Controller: post

/*
     * 添加用戶
     * 經過基本參數封裝獲取參數
     */
    @RequestMapping(value = "/user/addUser2", method = RequestMethod.POST)
    public ModelAndView addUser2(String username,String usercode,Date birthday,String address) {
        ModelAndView modelAndView = new ModelAndView();
        userModel model = new userModel();
        model.setUserName(username);
        model.setUserCode(usercode);
        model.setBirthday(birthday);
        model.setAddress(address);
        modelAndView.setViewName("/user/list");
        modelAndView.addObject("user", model);
        return modelAndView;
    }

 

四、實體(javaBean)參數獲取參數

實體:this

 1 public class userModel {
 2     
 3     @Override
 4     public String toString() {
 5         return "userModel [userName=" + userName + ", userCode=" + userCode + ", account=" + account + ", pwd=" + pwd
 6                 + ", address=" + address + ", birthday=" + birthday + ", Memo=" + Memo + "]";
 7     }
 8 
 9     private String userName;
10     private String userCode;
11     private String account;
12     private String pwd;
13     private String address; 
14     private Date birthday;
15     private String Memo;
16 
17     public String getAccount() {
18         return account;
19     }
20 
21     public void setAccount(String account) {
22         this.account = account;
23     }
24 
25     public String getPwd() {
26         return pwd;
27     }
28 
29     public void setPwd(String pwd) {
30         this.pwd = pwd;
31     }
32 
33     public String getMemo() {
34         return Memo;
35     }
36 
37     public void setMemo(String memo) {
38         Memo = memo;
39     }
40 
41     public Date getBirthday() {
42         return birthday;
43     }
44 
45     public void setBirthday(Date birthday) {
46         this.birthday = birthday;
47     }
48 
49     public String getUserName() {
50         return userName;
51     }
52 
53     public void setUserName(String userName) {
54         this.userName = userName;
55     }
56 
57     public String getUserCode() {
58         return userCode;
59     }
60 
61     public void setUserCode(String userCode) {
62         this.userCode = userCode;
63     }
64 
65     public String getAddress() {
66         return address;
67     }
68 
69     public void setAddress(String address) {
70         this.address = address;
71     }
72 
73 }
View Code

 

JSP頁面: spa

<div style="width: 28%; float: left;">
        <fieldset>
            <legend>經過javaBean獲取參數</legend>
            <form action="${pageContext.request.contextPath }/user/addUser3"
                method="post">
                <div style="width: 200px">
                    <label>名字:</label> <input name="userName" id="userName"
                        placeholder="請輸入名字" />
                </div>
                <div style="width: 200px">
                    <label>編號:</label> <input name="userCode" id="userCode"
                        placeholder="請輸入編號" />
                </div>
                <div style="width: 200px">
                    <label>生日:</label> <input name="birthday" id="birthday"
                        placeholder="請輸入生日" />
                </div> 
                <div style="width: 200px">
                    <label>地址:</label> <input name="address" id="address"
                        placeholder="請輸入地址" />
                </div>  
                <div style="width: 200px">
                    <button type="reset">重置</button>
                    <button type="submit">提交</button>
                </div>
            </form>
        </fieldset>
    </div>  

 

Controller: code

/*
     * 添加用戶
     * 經過javaBean獲取參數
     */
    @RequestMapping("/user/addUser3")
    public ModelAndView addUser3(userModel model) {
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("/user/list");
        modelAndView.addObject("user", model);
        return modelAndView;
    }

 

五、包裝類獲取參數

實體類:orm

 1 public class userModel {
 2     
 3     @Override
 4     public String toString() {
 5         return "userModel [userName=" + userName + ", userCode=" + userCode + ", account=" + account + ", pwd=" + pwd
 6                 + ", address=" + address + ", birthday=" + birthday + ", Memo=" + Memo + "]";
 7     }
 8 
 9     private String userName;
10     private String userCode;
11     private String account;
12     private String pwd;
13     private String address; 
14     private Date birthday;
15     private String Memo;
16 
17     public String getAccount() {
18         return account;
19     }
20 
21     public void setAccount(String account) {
22         this.account = account;
23     }
24 
25     public String getPwd() {
26         return pwd;
27     }
28 
29     public void setPwd(String pwd) {
30         this.pwd = pwd;
31     }
32 
33     public String getMemo() {
34         return Memo;
35     }
36 
37     public void setMemo(String memo) {
38         Memo = memo;
39     }
40 
41     public Date getBirthday() {
42         return birthday;
43     }
44 
45     public void setBirthday(Date birthday) {
46         this.birthday = birthday;
47     }
48 
49     public String getUserName() {
50         return userName;
51     }
52 
53     public void setUserName(String userName) {
54         this.userName = userName;
55     }
56 
57     public String getUserCode() {
58         return userCode;
59     }
60 
61     public void setUserCode(String userCode) {
62         this.userCode = userCode;
63     }
64 
65     public String getAddress() {
66         return address;
67     }
68 
69     public void setAddress(String address) {
70         this.address = address;
71     }
72 
73 }
View Code
 1 public class userModelArray {
 2     private userModel model;
 3 
 4     private String code;
 5     
 6     public userModel getModel() {
 7         return model;
 8     }
 9 
10     public void setModel(userModel model) {
11         this.model = model;
12     }
13 
14     public String getCode() {
15         return code;
16     }
17 
18     public void setCode(String code) {
19         this.code = code;
20     }
21 
22     @Override
23     public String toString() {
24         return "userModelArray [model=" + model + ", code=" + code + "]";
25     }
26 }
View Code

 

JSP頁面: 

<div style="width: 28%; float: left;">
        <fieldset>
            <legend>經過包裝類獲取參數</legend>
            <form action="${pageContext.request.contextPath }/user/addUser4"
                method="post">
                <div style="width: 200px">
                    <label>名字:</label> <input name="model.userName" id="userName"
                        placeholder="請輸入名字" />
                </div>
                <div style="width: 200px">
                    <label>編號:</label> <input name="code" id="code"
                        placeholder="請輸入編號" />
                </div>
                <div style="width: 200px">
                    <label>生日:</label> <input name="model.birthday" id="birthday"
                        placeholder="請輸入生日" />
                </div> 
                <div style="width: 200px">
                    <label>地址:</label> <input name="model.address" id="address"
                        placeholder="請輸入地址" />
                </div>  
                <div style="width: 200px">
                    <button type="reset">重置</button>
                    <button type="submit">提交</button>
                </div>
            </form>
        </fieldset>
    </div>  

 

Controller: 

/*
     * 添加用戶
     * 經過包裝類獲取參數
     */
    @RequestMapping("/user/addUser4")
    public ModelAndView addUser4(userModelArray model) {
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("/user/list");
        modelAndView.addObject("user", model);
        return modelAndView;
    }

 

六、數組類型獲取參數

JSP頁面: 

<div style="width: 28%; float: left;">
        <fieldset>
            <legend>數組類型獲取參數</legend>
            <form action="${pageContext.request.contextPath }/user/addUser5"
                method="post">
                <div style="width: 200px">
                    <label>選擇:</label> 
                    張三<input name="id" id="id" type="checkbox" value="1" />
                    李四<input name="id" id="id" type="checkbox" value="2" />
                    王五<input name="id" id="id" type="checkbox" value="3" />
                    趙六<input name="id" id="id" type="checkbox" value="4" />
                </div>  
                <div style="width: 200px">
                    <button type="reset">重置</button>
                    <button type="submit">提交</button>
                </div>
            </form>
        </fieldset>
    </div>  

 

Controller:

/*
     * 添加用戶
     * 數組類型獲取參數
     */
    @RequestMapping("/user/addUser5")
    public ModelAndView addUser5(Integer[] id) {        
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("/user/list");
        modelAndView.addObject("user", new userModel());
        return modelAndView;
    }

 七、包裝類-List集合 獲取參數傳遞 

 

JSP頁面: 

<div style="width: 28%; float: left;">
        <fieldset>
            <legend>封裝類集合獲取參數</legend>
            <form action="${pageContext.request.contextPath }/user/addUser6" method="post">
                <div style="width: 200px">
                    <label>名字:</label> <input name="userList[0].userName" id="userList[0].userName"  placeholder="請輸入名字" />
                </div>
                <div style="width: 200px">
                    <label>編號:</label> <input name="userList[0].userCode" id="userList[0].userCode" placeholder="請輸入編號" />
                </div>
                <div style="width: 200px">
                    <label>生日:</label> <input name="userList[0].birthday" id="birthday" placeholder="請輸入生日" />
                </div> 
                <div style="width: 200px">
                    <label>地址:</label> <input name="userList[0].address" id="address" placeholder="請輸入地址" />
                </div>  
                <div style="width: 200px">
                    <label>名字:</label> <input name="userList[1].userName" id="userList[0].userName"  placeholder="請輸入名字" />
                </div>
                <div style="width: 200px">
                    <label>編號:</label> <input name="userList[1].userCode" id="userList[0].userCode" placeholder="請輸入編號" />
                </div>
                <div style="width: 200px">
                    <label>生日:</label> <input name="userList[1].birthday" id="birthday" placeholder="請輸入生日" />
                </div> 
                <div style="width: 200px">
                    <label>地址:</label> <input name="userList[1].address" id="address" placeholder="請輸入地址" />
                </div>  
                <div style="width: 200px">
                    <button type="reset">重置</button>
                    <button type="submit">提交</button>
                </div>
            </form>
        </fieldset>
    </div>  

模型類:

 1 /*
 2  * 包裝類 - 包裝 List<userModel>
 3  */
 4 public class UserModelCustom {
 5     // 集合
 6     private List<userModel> userList; 
 7     
 8     public List<userModel> getUserList() {
 9         return userList;
10     }
11     public void setUserList(List<userModel> userList) {
12         this.userList = userList;
13     } 
14 } 
15 /*
16  * userModel 類
17  */
18 public class userModel {     
19     private String userName;
20     private String userCode;
21     private String account;
22     private String pwd;
23     private String address; 
24     private Date birthday;
25     private String Memo;
26 
27     public String getAccount() {
28         return account;
29     }
30 
31     public void setAccount(String account) {
32         this.account = account;
33     }
34 
35     public String getPwd() {
36         return pwd;
37     }
38 
39     public void setPwd(String pwd) {
40         this.pwd = pwd;
41     }
42 
43     public String getMemo() {
44         return Memo;
45     }
46 
47     public void setMemo(String memo) {
48         Memo = memo;
49     }
50 
51     public Date getBirthday() {
52         return birthday;
53     }
54 
55     public void setBirthday(Date birthday) {
56         this.birthday = birthday;
57     }
58 
59     public String getUserName() {
60         return userName;
61     }
62 
63     public void setUserName(String userName) {
64         this.userName = userName;
65     }
66 
67     public String getUserCode() {
68         return userCode;
69     }
70 
71     public void setUserCode(String userCode) {
72         this.userCode = userCode;
73     }
74 
75     public String getAddress() {
76         return address;
77     }
78 
79     public void setAddress(String address) {
80         this.address = address;
81     } 
82 }
View Code

Controller:

/*
     * 添加用戶
     * 封裝類集合獲取參數
     */
    @RequestMapping("/user/addUser6")
    public String addUser6(UserModelCustom customModel) {        
        
           return "/user/list";
    }

八、包裝類-Map集合 獲取參數傳遞

JSP頁面:

<div style="width: 28%; float: left;">
        <fieldset>
            <legend>封裝類Map獲取參數</legend>
            <form action="${pageContext.request.contextPath }/user/addUser7" method="post">
                <div style="width: 200px">
                    <label>名字:</label> <input name="maps['userName']" id="map['userName']"  placeholder="請輸入名字" />
                </div>
                <div style="width: 200px">
                    <label>編號:</label> <input name="maps['userCode']" id="map['userCode']" placeholder="請輸入編號" />
                </div>
                <div style="width: 200px">
                    <label>生日:</label> <input name="maps['birthday']" id="map['birthday']" placeholder="請輸入生日" />
                </div> 
                <div style="width: 200px">
                    <label>地址:</label> <input name="maps['address']" id="map['address']" placeholder="請輸入地址" />
                </div>   
                <div style="width: 200px">
                    <button type="reset">重置</button>
                    <button type="submit">提交</button>
                </div>
            </form>
        </fieldset>
    </div>  

模型類: 

 1 /*
 2  * 包裝類 - 包裝 List<userModel>
 3  */
 4 public class UserModelCustom {
 5     // map
 6     private Map<String, Object> maps;
 7     
 8     public Map<String, Object> getMaps() {
 9         return maps;
10     }
11     public void setMaps(Map<String, Object> maps) {
12         this.maps = maps;
13     } 
14 } 
15 /*
16  * userModel 類
17  */
18 public class userModel {     
19     private String userName;
20     private String userCode;
21     private String account;
22     private String pwd;
23     private String address; 
24     private Date birthday;
25     private String Memo;
26 
27     public String getAccount() {
28         return account;
29     }
30 
31     public void setAccount(String account) {
32         this.account = account;
33     }
34 
35     public String getPwd() {
36         return pwd;
37     }
38 
39     public void setPwd(String pwd) {
40         this.pwd = pwd;
41     }
42 
43     public String getMemo() {
44         return Memo;
45     }
46 
47     public void setMemo(String memo) {
48         Memo = memo;
49     }
50 
51     public Date getBirthday() {
52         return birthday;
53     }
54 
55     public void setBirthday(Date birthday) {
56         this.birthday = birthday;
57     }
58 
59     public String getUserName() {
60         return userName;
61     } 
62 }
View Code

 

Controller:

/*
     * 添加用戶
     * 封裝類Map獲取參數
     */
    @RequestMapping("/user/addUser7")
    public String addUser7(UserModelCustom customModel) {        
        
           return "/user/list";
    }
相關文章
相關標籤/搜索