在Spring MVC中,前端JSP页面可以传递  基本类型(int,String)、实体类型、包装类型、数组类型、集合类型(List、map )等。

假如在传递的类型中有 Date类型的字段,需要在 Controller 通过  initBinder()  进行处理,代码如下:

@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));
    }
}

 

1、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:

/*
     * 添加用户
     * 通过 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;
    }

 

2、基本类型获取参数

JSP页面: 

<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: 

/*
     * 添加用户
     * 通过基本参数封装获取参数
     */
    @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;
    }

 

4、实体(javaBean)参数获取参数

实体:

 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

相关文章:

  • 2021-08-08
  • 2022-12-23
  • 2021-10-08
  • 2021-10-20
  • 2021-09-08
  • 2022-12-23
  • 2021-11-04
猜你喜欢
  • 2021-11-12
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-21
相关资源
相似解决方案