SpringMVC参数传递
使用原生的ServletAPI获取(request.getParameter("id"))
页面:
<form action="login" method="post"> username:<input type="text" name="name"/><br> password:<input type="password" name="pwd"/><br> username:<input type="submit" value="login"/><br> </form> |
Controller:
@RequestMapping("login") public String login(HttpServletRequest request){ //取得到前台页面的值? System.out.println(request.getParameter("name")); System.out.println(request.getParameter("pwd")); return "success"; } |
Springmvc的核心配置文件
<!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 视图显示 --> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <!-- /就代表web的根目录,WebContent --> <property name="prefix" value="/"></property> <!-- 添加后缀:在每次请求完之后,加一个后缀名 --> <property name="suffix" value=".jsp"></property> </bean> |
使用@RequestParam注解取值解耦ServletAPI
@RequestParam 注解用来取得前台页面的值:
Value:是前台页面中input标签 name属性的值。
DefaultValue:默认值!
required=true 页面中的属性必须有值!
@RequestMapping("loginOne") public String loginOne(@RequestParam(value="name",defaultValue="amdin",required=true) String names){ //取得到前台页面的值? System.out.println(names); return "success"; } |
使用springmvc传值的时候:有一个特殊的地方!
如果前台页面name属性值,跟controller方法中传入的参数名称一致!则不需要写@RequestParam,直接能取得到!
@RequestMapping("loginOne") public String loginOne(String name){ //取得到前台页面的值? System.out.println(name); return "success"; } |
使用对象传值
当前台页面name属性的值,和实体类中属性名一致时!后台controller,可以直接使用实体类取值!
@RequestMapping("loginTwo") public String loginTwo(User user,HttpSession session){ //取得到前台页面的值? System.out.println(user); return "success"; } |
注意:如果输入中文的时候,可能会出现中文乱码,则需要添加一个过滤器,则这个过滤器配置如下: 过滤器配置的路径跟springmvc的核心配置文件的路径是有区别!
<filter> <filter-name>filter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
使用数组传值
页面:
<form action="loginThree" method="post"> username:<input type="text" name="name"/><br> password:<input type="password" name="pwd"/><br> 喜欢的游戏:<br> <input type="checkbox" name="games" value="lol"/>lol<br> <input type="checkbox" name="games" value="王者荣耀"/>王者荣耀<br> <input type="checkbox" name="games" value="dota"/>dota<br> <input type="checkbox" name="games" value="gyy"/>gyy<br> <input type="submit" value="login"/><br> </form> |
后天:
@RequestMapping("loginThree") public String loginThree(User user){ //取得到前台页面的值? System.out.println(user); return "success"; }
@RequestMapping("loginFour") public String loginFour(String[] games){ for (String string : games) { System.out.println(string); } return "success"; }
@RequestMapping("loginFive") public String loginFive(@RequestParam(value="games") List<String> games){ for (String string : games) { System.out.println(string); } return "success"; } |
使用restful风格传值
RESTFUL:风格其实本质就是一个跳转方式!
<body> <h1>欢迎来到王者农药,敌军还有五秒到达战场!请做好准备!</h1> <a href="user/gyy">TestA</a> <a href="user/ty">TestB</a> <a href="user/shq">TestC</a> </body> @Controller public class CommonsController {
@RequestMapping("/user/{path}") public void show(@PathVariable(value="path") String path){ System.out.println(path);
}
} 如果有精确匹配:则restful风格将会失效! @RequestMapping("user/gyy") public String gyy(){ System.out.println("我才是真的高圆圆!"); return "gyy"; } |
Springmvc的页面跳转方式:默认方式相当于转发
request.getRequestDispatcher("index.jsp").forward(request,response);
response.sendRedirect("index.jsp");
需要进行页面渲染!
mv.setViewName("index");
return "redirect:success.jsp";
return "forward:success.jsp";