获取前台参数:java
咱们以用户登陆为例,用户登陆涉及两个参数:spring
帐号:loginName 密码:password
这是前台登陆视图:mvc
相应的前台源码:app
<form action="login"> 账号:<input type="text" name="loginName" > <br/> 密码:<input type="text" name="password" > <br/> <input type="submit" value="登陆"> </form>
------------------------------------------------------jsp
介绍SpringMVC最经常使用的3种取值方法函数
------------------------------------------------------ui
方法1:参数直接获取this
ps: 函数参数名与请求参数名保持一致。url
@RequestMapping("/login") public String login(String loginName,String password){ System.out.println("方法1:参数直接获取"); System.out.println("loginName:"+loginName); System.out.println("password:"+password); return "loginSuccess"; }
运行结果:spa
方法2:对象获取
ps:创建一个对象,属性名对应请求参数名保持一致,并生成相应的getter()和setter()方法。
创建对象User:
package com.springdemo.entities; public class User { private String loginName; private String password; public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
开始接收:
@RequestMapping("/login") public String login(User u){ System.out.println("方法2:对象获取"); System.out.println("loginName:"+u.getLoginName()); System.out.println("password:"+u.getPassword()); return "loginSuccess"; }
运行结果:
方法3:@RequestParam参数绑定获取
ps:方法1的变种,但该接收参数名能够随意,经过注解@RequestParam指明便可。
请求URL为:
http://localhost:8080/springmvc/hello/101?loginName=wangwu&password=123
对于上面这个url,controller里面能够这么写:
@RequestMapping("/login") public String login(@RequestParam("loginName") String name,@RequestParam("password") String pwd){ System.out.println("方法3:参数绑定获取"); System.out.println("loginName:"+name); System.out.println("password:"+pwd); return "loginSuccess"; }
方法4:@PathVariable获取请求路径中的参数
这个注解可以识别URL里面的一个模板,咱们看下面的一个URL
http://localhost:8080/springmvc/hello/101?param1=10¶m2=20
上面的一个url你能够这样写:
@RequestMapping("/hello/{id}") public String getDetails(@PathVariable(value="id") String id, @RequestParam(value="param1", required=true) String param1, @RequestParam(value="param2", required=false) String param2){ ....... }
向前台传值(除了使用ModelAndView方式外。还可使用Map、Model和ModelMap的方式):
Java代码
1.使用Map、Model和ModelMap的方式
@RequestMapping("/test") public String test(Map<String,Object> map,Model model,ModelMap modelMap,HttpServletRequest request){ //1.放在map里 map.put("names", Arrays.asList("caoyc","zhh","cjx")); //2.放在model里 建议使用 model.addAttribute("time", new Date()); //3.放在request里 request.setAttribute("request", "requestValue"); //4.放在modelMap中 modelMap.addAttribute("city", "ChengDu"); modelMap.put("gender", "male"); return "hello"; }
JSP写法:
time:${requestScope.time} names:${requestScope.names } city:${requestScope.city } gender:${requestScope.gender } request:${requestScope.request}
2.使用ModelAndView的方式:
@RequestMapping(value="/test2.do",method = RequestMethod.POST) public ModelAndView checknameIsExist2(@RequestParam("sid") String sid,Model model,HttpServletRequest request) { ModelAndView mav = new ModelAndView(); mav.addObject("ModelAndView", "ModelAndViewValue"); //设置要跳转的页面,与返回值时String时返回success相似,如下跳转到/student/studentList.jsp mav.setViewName("/student/studentList"); return mav; }
说明:使用ModelAndView,能够直接使用AddObject来传递数据,而且数据㐓使用EL表达式在前台页面处理。使用setViewName("/student/studentList");来设置页面跳转路径。
借用一篇文章:
springMVC的@RequestParam注解和@PathVariable注解的区别
@RequestParam注解和@PathVariable注解的区别,从字面上能够看出前者是获取请求里边携带的参数;后者是获取请求路径里边的变量参数。
(例如:127.0.0.1/user/{userId}?userName=zhangshan,userId是路径上的变量,userName才是请求参数信息)
1.@RequestParam注解
@RequestParam有三个参数: value:参数名; required:是否必需,默认为true,表示请求参数中必须包含该参数,若是不包含抛出异常。 defaultValue:默认参数值,若是设置了该值自动将required设置为false,若是参数中没有包含该参数则使用默认值。 示例:@RequestParam(value = "userId", required = false, defaultValue = "1")
2.@PathVariable注解
当使用@RequestMapping URI占位符映射时,Url中能够经过一个或多个{xxxx}占位符映射,经过@PathVariable能够绑定占位符参数到方法参数中。 例如:@PathVariable("userId") Long userId,@PathVariable("userName") String userName (注:Long类型能够根据需求本身改变String或int,spring会自动作转换) @RequestMapping(“/user/{userId}/{userName}/query") 请求URL:http://localhost/user/8/张山/query
@PathVariable小误区:
在有些参考资料中说,若是定义的参数名和占位符中的名称是相同的,则能够将 @PathVariable(xxxx) 简写为:@PathVariable,这实际上是错误的!
由于在正常编译时,Java类反射对象是不包含方法的入参名称的,若是编译时将debug打开(javac –debug=no),方法的入参名称会记录到类中。
在Eclipse中能够这样设置(项目属性Java Compiler):