SpringMVC如何处理请求
通过servletApi
但是使用占位符不能使用此方法进行获取
1 2
| String username = request.getParameter("username"); String password = request.getParameter("password");
|
通过控制器方法的形参获得参数
1 2 3 4 5 6
| @RequestMapping("/testParam")
public String testParam(String username, String password){ System.out.println("username:"+username+",password:"+password); return "success"; }
|
获取多个参数
1 2 3 4 5 6
| <form th:action="@{/target}" method="get"> <input type="checkbox" name="hobbies" value="a">a <input type="checkbox" name="hobbies" value="b">b <input type="checkbox" name="hobbies" value="c">c <input type="submit" value="以post方式提交表单"> </form>
|
1 2 3 4 5 6
| @RequestMapping(value = "/target") public String toTarget(String []hobbies){ System.out.println(Arrays.toString(hobbies)); System.out.println(hobbies[0]); return "target"; }
|
@RequestParam
在上面的使用方法中我们前端的name与controller中的参数名称必须相同,但是如果不相同的话,参数会返回空值,获取不到。而在这里我们可以使用@RequestParam的方法进行表示
1 2 3 4 5 6 7 8
| @RequestMapping(value = "/target") public String toTarget( @RequestParam("hobbies_1") String []hobbies ){ System.out.println(Arrays.toString(hobbies)); System.out.println(hobbies[0]); return "target"; }
|
1 2 3 4 5 6
| <form th:action="@{/target}" method="get"> <input type="checkbox" name="hobbies_1" value="a">a <input type="checkbox" name="hobbies_1" value="b">b <input type="checkbox" name="hobbies_1" value="c">c <input type="submit" value="以post方式提交表单"> </form>
|
1 2 3 4 5 6 7 8 9 10 11
| public @interface RequestParam { @AliasFor("name") String value() default "";
@AliasFor("value") String name() default "";
boolean required() default true;
String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n"; }
|
1 2 3 4 5 6 7 8 9
| public String toTarget( @RequestParam(value = "hobbies",required = false) String []hobbies, @RequestHeader(value="Host")String host ){ System.out.println(Arrays.toString(hobbies)); System.out.println(hobbies[0]); System.out.println(host); return "target"; }
|
@CookieValue
cookie时浏览器端的服务器会话技术,而session是服务器端的会话技术,session依赖于cookie
@CookieValue是将cookie数据和控制器方法的形参创建映射关系
当我们第一次访问浏览器的时候,我们会在请求报文中查找是否有cookie记录,如果没有则交给浏览器创建一个cookie,响应给服务器,服务器进行保存,下一次进行访问的时候会将session携带过去
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @RequestMapping( value = {"/","/hi"}) public String index(HttpServletRequest request){ HttpSession session= request.getSession(); return "index"; }
@RequestMapping(value = "/target") public String toTarget( @RequestParam(value = "hobbies",required = false) String []hobbies, @RequestHeader(value="Host")String host, @CookieValue("JSESSIONID:")String JSESSIONID ){ System.out.println(Arrays.toString(hobbies)); System.out.println(hobbies[0]); System.out.println(host); System.out.println(JSESSIONID); return "target"; }
|
通过POJO获取参数
注意在类中一定要定义set方法才可以赋值,至于为什么,现在不清楚,插眼!!!!后面看到源码解决
1 2 3 4 5 6 7 8
| <form th:action="@{/target}" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> 性别:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br> 年龄:<input type="text" name="age"><br> 邮箱:<input type="text" name="email"><br> <input type="submit"> </form>
|
1 2 3 4 5
| @RequestMapping(value = "/target") public String toTarget(User user){ System.out.println(user.toString()); return "target"; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package com.zss.mvc.pojo;
public class User { Integer id; String username; String password; String sex; Integer age; String email;
public void setId(Integer id) { this.id = id; }
public void setUsername(String username) { this.username = username; }
public void setPassword(String password) { this.password = password; }
public void setSex(String sex) { this.sex = sex; }
public void setAge(Integer age) { this.age = age; }
public void setEmail(String email) { this.email = email; }
public User(){
} public User(Integer id,String username, String password, String sex, Integer age, String email) { this.id=id; this.username = username; this.password = password; this.sex = sex; this.age = age; this.email = email; }
@Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", sex='" + sex + '\'' + ", age=" + age + ", email='" + email + '\'' + '}'; } }
|
解决乱码
注意我们改变请求的参数的乱码设置一定要在获得请求之前,如果在后面则不会发生作用
这里注意我们上面使用的是post方法,但是如果我们改到get方法,在其中配置相关编码,将get方法发送的url转换成utf-8编码,但是post方法还是存在乱码问题
所以我们应该在dispatcherservlet之前进行设置,所以我们在过滤器中进行配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <filter> <filter-name>CharacterEncodingFilter</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> //设置响应的编码是否也启用encoding <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> //对所有的页面进行响应 <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
|