4、@RequestHeader
@RequestHeader是将请求头信息和控制器方法的形参创建映射关系
@RequestHeader注解一共有三个属性:value. required. defaultValue,用法同@RequestParam
@RequestMapping("/testParam") public String testParam( @RequestParam(value = "user_name",required = false,defaultValue = "呵呵") String username, String password, String[] hobby, @RequestHeader("host") String host ){ //若请求参数中出现多个同名的请求参数,可以在控制器方法的形参位置设置字符串类型或字符串数组来接受此请求参数 //若使用字符串类型的形参,最终结果为请求参数每一个值之间使用逗号进行拼接的结果 System.out.println("username:"+username+",password:"+password+",hobby:"+ Arrays.toString(hobby)); System.out.println("host:"+host); return "success"; }
@RequestMapping("/testParam") public String testParam( @RequestParam(value = "user_name",required = false,defaultValue = "hehe") String username, String password, String[] hobby, @RequestHeader(value = "sayhaha",required = true,defaultValue = "haha") String host ){ //若请求参数中出现多个同名的请求参数,可以在控制器方法的形参位置设置字符串类型或字符串数组来接受此请求参数 //若使用字符串类型的形参,最终结果为请求参数每一个值之间使用逗号进行拼接的结果 System.out.println("username:"+username+",password:"+password+",hobby:"+ Arrays.toString(hobby)); System.out.println("host:"+host); return "success"; }
5、@CookieValue
@CookieValue是将cookie和控制器方法的形参创建映射关系
@CookieValue注解一共有三个属性:value. required. defaultValue,用法同@RequestParam
@RequestMapping("/testServletAPI") //形参位置的request表示当前请求 public String testServletAPI(HttpServletRequest request){ HttpSession session = request.getSession(); String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username:"+username+",password:"+password); return "success"; }
@RequestMapping("/testParam") public String testParam( @RequestParam(value = "user_name",required = false,defaultValue = "hehe") String username, String password, String[] hobby, @RequestHeader(value = "sayhaha",required = true,defaultValue = "haha") String host, @CookieValue("JSESSIONID")String JSESSIONID ){ //若请求参数中出现多个同名的请求参数,可以在控制器方法的形参位置设置字符串类型或字符串数组来接受此请求参数 //若使用字符串类型的形参,最终结果为请求参数每一个值之间使用逗号进行拼接的结果 System.out.println("username:"+username+",password:"+password+",hobby:"+ Arrays.toString(hobby)); System.out.println("host:"+host); System.out.println("JSESSIONID"+JSESSIONID); return "success"; }
6、通过POJO获取请求参数
可以在控制器方法的形参位置设置一个实体类类型的形参,此时若浏览器传输的请求参数的参数名和实体类中的属 性名一致,那么请求参数就会为此属性赋值
<form th:action="@{/testPOJO}" 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>
@RequestMapping("/testPOJO") public string testpoJo(user user){ System.out.println(user); return "success"; //最终结果-->User{id=null,username='张三',password='123',age=23,sex="男", email='123@qq.com'}
演示
mvc新建bean下新建User类
package com.atguigu.mvc.bean; public class User { private Integer id; private String username; private String password; private Integer age; private String sex; private String email; public User() { } public User(Integer id, String username, String password, Integer age, String sex, String email) { this.id = id; this.username = username; this.password = password; this.age = age; this.sex = sex; this.email = email; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } 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; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", age=" + age + ", sex='" + sex + '\'' + ", email='" + email + '\'' + '}'; } }
ParamController
@RequestMapping("/testBean") public String testBean(User user){ System.out.println(user); return "success"; }
sex=’??·’,post中文出现乱码
get乱码问题在Tomcat配置文件中解决
7、解决获取请求参数的乱码问题
get乱码问题在Tomcat配置文件中解决
E:\apache-tomcat-8.5.69\conf\server.xml中
解决获取请求参数的乱码问题,必须在获取请求参数之前类修改编码,可以使用SpringMVC提供的编码过滤器CharacterEncodingFilter,但是必须在web.xml中进行注册
源码
web.xml
<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> <!-- 设置响应编码 --> <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>
五、域对象共享数据
新建模块springMVC_demo3
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置编码过滤器 --> <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> <!-- 设置响应编码 --> <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> <!-- 注册前端控制器DispatcherServlet --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
**TestController **
package com.atguigu.mvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { @RequestMapping("/") public String index(){ return "index"; } }
index.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>首页</h1> </body> </html>
1、使用servletAPI向request域对象共享数据
@RequestMapping("/testServletAPI") public string testServletApI(HttpServletRequest request){ request.setAttribute("testscope", "hello,servletAPI"; return "success"; }
演示
controller下新建ScopeController
package com.atguigu.mvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; @Controller public class ScopeController { //使用ServletAPI向Request域对象共享数据 @RequestMapping("/testRequestByServletAPI") public String testRequestByServletAPI(HttpServletRequest request){ request.setAttribute("testRequestScope","hello,ServletAPI"); return "success"; } }
success.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> success<br> <p th:text="${testRequestScope}"></p> </body> </html>
"${testRequestScope}"报红可以按设置修改
index.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>首页</h1> <a th:href="@{/testRequestByServletAPI}">通过ServletAPI向request域共享数据</a> </body> </html>
2、使用ModelAndView向request域对象共享数据
@RequestMapping("/testModelAndview") public ModelAndview testmodelAndviewO{ /** ModelAndview有Model和view的功能 Model主要用于向请求域共享数据 *view主要用于设置视图,实现页面跳转 */ ModelAndview mav = new ModelAndviewO; //向请求域共享数据 mav.addobject("testscope", "hello,ModelAndview"); //设置视图,实现页面跳转 mav.setviewName("success"); return mav:
演示
ScopeController
@RequestMapping("/testModelAndView") public ModelAndView testModelAndView(){ ModelAndView mav=new ModelAndView(); //处理模型数据,即向请求与request共享数据 mav.addObject("testRequestScope","hello,ModelAndView"); //设置视图名称 mav.setViewName("success"); return mav; }
3、使用Model响request域对象共享数据
@RequestMapping("/testModel") public string testModel(Model model){ model.addAttribute("testscope", "hello,Model"); return "success"; }
演示
@RequestMapping("/testModel") public String testModel(Model model) { model.addAttribute("testRequestScope", "hello,model"); return "success"; }
4、使用map向request域对象共享数据
@RequestMapping ("/testMap") public String testMap(Map<String, Object> map){ map.put("testscope", "hello,Map"); return "success"; }
演示
@RequestMapping ("/testMap") public String testMap(Map<String, Object> map){ map.put("testRequestScope", "hello,Map"); return "success"; }