Spring MVC从页面向Controller传值的方式
从页面向Controller 传值的方式有很多,本文整理出常用的从页面向Controller传值的几种方式:
1、通过HttpServletRequest 向后台传递数据
页面程序如下:
<h2>利用HttpServletRequest传数据</h2> <form action="loginToJson"> 用户名:<input name="name" /> 密码:<input name="pass" /> <input type="submit" value="登陆"> </form>
后台程序段如下:
@ResponseBody @RequestMapping(value="/loginToJson",method=RequestMethod.POST) public String loginToJson(HttpServletRequest request) { User user = new User(); user.setUserName(request.getParameter("name")); user.setPassword(request.getParameter("pass")); return JSON.toJSONString(user); }
2、通过参数向Controller传递值
前台程序如下:
<h2>利用函数参数传数据</h2> <form action="loginByVariable" method="post"> 用户名:<input name="name" /> 密码:<input name="pass" /> <input type="submit" value="登陆"> </form>
后台程序如下:
@RequestMapping(value="/loginByVariable",method=RequestMethod.POST) public String loginToSession(HttpServletRequest request,ModelMap map,String name,String pass) { User user = new User(); user.setUserName(name); user.setPassword(pass); map.put("user", user); System.out.println(user); return "mySession"; }
注意:前台<input name="name" />中的name属性的值,必须和后台函数参数中的String name中的形参名一致,否则从后台取不到值。
3、利用函数参数,对象传递值到Controller
前置条件,对象的定义代码:
注意这里,User对象的属性名为userName和password,并有getter和setter属性
public class User{ @Override public String toString() { return "User [userName=" + userName + ", password=" + password + "]"; } 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; } private String userName; private String password; }
前台程序如下:
注意:userName和password必须和对象属性名称一致。
<h2>利用函数参数对象传数据</h2> <form action="loginByObject" method="post"> 用户名:<input name="userName" /> 密码:<input name="password" /> <input type="submit" value="登陆"> </form>
后台程序如下:
@RequestMapping(value="/loginByObject",method=RequestMethod.POST) public String loginToSession(ModelMap map,User user) { User user1 = new User(); user1.setUserName(user.getUserName()); user1.setPassword(user.getPassword()); System.out.println(user1+" Object"); map.put("user", user1); return "mySession"; }
4、利用函数参数@RequestParam参数传递据
前台代码如下:
<br> <h2>利用函数参数@RequestParam参数传递据</h2> <form action="loginByRequestParm" method="post"> 用户名:<input name="name" /> 密码:<input name="pass" /> <input type="submit" value="登陆"> </form>
后台程序如下:
@RequestMapping(value="/loginByRequestParm",method=RequestMethod.POST) public String loginToSessiona(HttpServletRequest request,ModelMap map, @RequestParam("name") String UserName,@RequestParam String pass) { User user = new User(); user.setUserName(UserName); user.setPassword(pass); map.put("user", user); System.out.println(user+"requestParam"); return "mySession"; }
注意:@RequestParam("name")中如果有参数,name必须为页面中属性的值,如果没有参数,则参数名,必须和页面中属性的值相同,如页面中pass,则在@RequestParam中,则可以不指定参数,但形参的名字必须为pass
5、 通过URL传递参数 @PathVariable
具体代码如下:
@Controller public class TestController { @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET) public String getLogin(@PathVariable("userId") String userId, @PathVariable("roleId") String roleId){ System.out.println("User Id : " + userId); System.out.println("Role Id : " + roleId); return "hello"; } @RequestMapping(value="/product/{productId}",method = RequestMethod.GET) public String getProduct(@PathVariable("productId") String productId){ System.out.println("Product Id : " + productId); return "hello"; } @RequestMapping(value="/javabeat/{regexp1:[a-z-]+}", method = RequestMethod.GET) public String getRegExp(@PathVariable("regexp1") String regexp1){ System.out.println("URI Part 1 : " + regexp1); return "hello"; } }