文章目录
- 1.SpringMVC映射请求数据
- 1.获取请求头信息
- 1.VoterHandler.java
- 2.request_parameter.jsp
- 3.结果展示
- 2.自动封装javabean
-
- 3.调用servlet-api
-
- 2.模型数据(放入request)
- 1.需求分析
- 2.模型数据—默认机制
- 1.接口
- 2.getmaster.jsp
- 3.postman测试
- 3.模型数据—HttpServletRequest API
-
- 4.模型数据—Map<String, Object>
- 1.接口
- 2.getmaster.jsp
- 3.postman测试
- 4.注意事项
- 5.模型数据—ModelAndView
-
- 6.模型数据(放入session)
- 1.接口
- 2.getsession.jsp
- 3.postman测试
- 7.@ModelAttribute注解配置所有方法前置通知
-
1.SpringMVC映射请求数据
1.获取请求头信息
java_6">1.VoterHandler.java
java">package com.sun.web.requetstparam;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/request")
@Controller
public class VoterHandler {@RequestMapping("/getheader")public String getHeader(@RequestHeader("Accept-Encoding") String ae, @RequestHeader("Host") String host) {System.out.println("Accept-Encoding:" + ae);System.out.println("host:" + host);return "/success";}
}
2.request_parameter.jsp
<%--Date: 2024/2/25Time: 21:04User: 孙显圣Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h3><a href="request/getheader">点击获取请求头信息</a></h3></body>
</html>
3.结果展示
javabean_63">2.自动封装javabean
1.需求分析
2.应用实例
java">package com.sun.web.entity;
public class Master {private Integer id;private String name;private Pet pet; public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Pet getPet() {return pet;}public void setPet(Pet pet) {this.pet = pet;}@Overridepublic String toString() {return "Master{" +"id=" + id +", name='" + name + '\'' +", pet=" + pet +'}';}
}
java">package com.sun.web.entity;import javafx.geometry.Orientation;
public class Pet {private Integer id;private String name;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Pet{" +"id=" + id +", name='" + name + '\'' +'}';}
}
3.后端接口
java"> @RequestMapping("/setmaster")public String setMaster(Master master) {System.out.println(master);return "success";}
4.结果展示
3.底层机制
- Spring MVC直到要将参数封装到Master中,则反射创建Master对象
- 基本属性封装
- 读取请求信息,得到id和name两个属性的值
- 调用Master对象的setter方法为其赋值
- 嵌套属性封装
- 读取请求信息,得到pet.id和pet.name这两个值
- 首先检测刚才反射创建的Master实例中是否有Pet实例,如果没有,则反射创建一个Pet实例
- 调用Pet的setter方法先封装id
- 再次检测Master实例中是否有Pet实例,发现已经存在,则直接调用setName方法封装name
- 最后掉用setPet的方法封装到Master实例中
3.调用servlet-api
1.基本说明
2.代码实例
1.接口
java"> @RequestMapping("/servlet")public String servlet(HttpServletRequest request, HttpServletResponse response) {String username = request.getParameter("username");String password = request.getParameter("password");System.out.println("username:" + username + " password:" + password) ;return "success";}
2.结果展示
3.注意事项
案例演示
java"> @RequestMapping("/session")public String session(HttpServletRequest request, HttpSession session) {System.out.println(session);HttpSession session1 = request.getSession();System.out.println(session1);return "success";}
2.模型数据(放入request)
1.需求分析
2.模型数据—默认机制
1.接口
java"> @RequestMapping("/m01")public String master01(Master master) {return "getmaster";}
2.getmaster.jsp
<%@ page import="org.springframework.web.context.request.RequestScope" %><%--Date: 2024/2/26Time: 11:56User: 孙显圣Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>id:${requestScope.master.id}<br>name:${requestScope.master.name}<br>pet.id:${requestScope.master.pet.id}<br>pet.name:${requestScope.master.pet.name}
</body>
</html>
3.postman测试
3.模型数据—HttpServletRequest API
1.接口
java"> @RequestMapping("/m02")public String master02(Master master, HttpServletRequest request) {request.setAttribute("master", master);return "getmaster";}
2.postman测试
4.模型数据—Map<String, Object>
1.接口
java"> @RequestMapping("/m03")public String master03(Master master, Map<String, Object> map) {map.put("address", "127.0.0.1"); return "getmaster";}
2.getmaster.jsp
<%@ page import="org.springframework.web.context.request.RequestScope" %><%--Date: 2024/2/26Time: 11:56User: 孙显圣Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
address:${requestScope.address}
id:${requestScope.master.id}<br>
name:${requestScope.master.name}<br>
pet.id:${requestScope.master.pet.id}<br>
pet.name:${requestScope.master.pet.name}
</body>
</html>
3.postman测试
4.注意事项
5.模型数据—ModelAndView
1.接口
java"> @RequestMapping("/m04")public ModelAndView modelandview(Master master) {ModelAndView modelAndView = new ModelAndView();modelAndView.addObject("address", "127.0.0.1");modelAndView.setViewName("getmaster");return modelAndView;}
2.postman测试
3.注意事项
6.模型数据(放入session)
1.接口
java"> @RequestMapping("/m05")public String getsession(Master master, HttpSession session){session.setAttribute("address", "122222");session.setAttribute("name", "sun");return "getsession";}
2.getsession.jsp
<%--Date: 2024/2/26Time: 13:38User: 孙显圣Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
name:${sessionScope.name}<br>
address:${sessionScope.address}</body>
</html>
3.postman测试
7.@ModelAttribute注解配置所有方法前置通知
1.代码
java"> @ModelAttributepublic void prepareModel() {System.out.println("准备工作完毕");}
2.postman测试