(1)页面请求定义
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%--测试--%>
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
main
这里是main页面
(2) 执行器方法
测试一
@Controller
@RequestMapping("/testController")
public class TestController {
@RequestMapping(value = "test01",method = {RequestMethod.GET})
public String test01(@RequestParam(name = "username",required = true) String username){
System.out.println("username = " + username);
return "main";
}
}
测试二
去掉index.jsp中的input标签,再测试,报400
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%--测试--%>
测试三
据测试二,添加属性defaultValue
@RequestMapping(value = "test01",method = {RequestMethod.GET})
public String test01(@RequestParam(name = "username",required = true,defaultValue = "小李") String username){
System.out.println("username = " + username);
return "main";
}
(1)页面定义请求
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%--测试--%>
(2) 执行器方法
@RequestMapping(value = "test02",method = {RequestMethod.POST})
public String test02(@RequestHeader(name = "Upgrade-Insecure-Requests") String data){
System.out.println("data = " + data);
return "main";
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%--测试--%>
执行器方法
@RequestMapping(value = "test03",method = {RequestMethod.POST})
public String test03(@RequestBody String data){
System.out.println("data = " + data);
return "main";
}
Student实体
public class Student {
private String id;
private String name;
private int age;
public Student() {
}
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
引入json的解析器,pom.xml中
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.9version>
dependency>
静态页面无法加载,在application.xml配置信息
<mvc:default-servlet-handler>mvc:default-servlet-handler>
页面定义请求index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
执行器方法
@RequestMapping(value = "test04",method = {RequestMethod.POST})
public String test04(@RequestBody Student student){
System.out.println("student = " + student);
return "main";
}
原生的Cookie的获取
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
String name = cookie.getName();
if(name.equals("JSESSIONID")){
//获得cookie的值:
String value = cookie.getValue();
}
}
@CookieValue注解
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
test01
test05
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
main
这里是main页面
执行器方法
@RequestMapping(value = "test05",method = {RequestMethod.GET})
public String test05(@CookieValue("JSESSIONID") String data){
System.out.println("data = " + data);
return "main";
}
@RequestMapping(value = "test06",method = {RequestMethod.GET})
public String test06(@CookieValue("JSESSIONID") Cookie cookie){
System.out.println("key = " + cookie.getName() + "," + "value = " + cookie.getValue());
return "main";
}
// 注解在方法上
@ModelAttribute("param")
public String test07(){
System.out.println("这是 ModelAttribute 注解的方法");
return "hello world";
}
// 注解在参数位置
@RequestMapping(value = "test08",method = {RequestMethod.GET})
public String test08(@ModelAttribute("param") String data){
System.out.println("ModelAttribute 注解的参数 data = " + data);
return "main";
}
页面请求
test01.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
test01
test01
test02
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
main
这里是main页面
${studnet}
执行器方法
@Controller
@RequestMapping("/sessionController")
@SessionAttributes("student")
public class SessionController {
@RequestMapping(value = "test01",method = {RequestMethod.GET})
public ModelAndView test01(ModelMap modelMap){
ModelAndView modelAndView = new ModelAndView();
modelMap.addAttribute("studnet",new Student("1","tom",22));
modelAndView.setViewName("main");
return modelAndView;
}
@RequestMapping(value = "test02",method = {RequestMethod.GET})
public String test02(){
return "main";
}
}
测试过程:先点test02,后点test01,才能看到session的Attribute值
传统请求url
功能 | 统一资源定位符 | 请求方式 |
---|---|---|
新增 | http://localhost:8888/user/add | POST |
修改 | http://localhost:8888/user/update | POST |
删除 | http://localhost:8888/user/deleteById?id=1 | GET |
查询一个 | http://localhost:8888/user/findById?id=1 | GET |
查询所有 | http://localhost:8888/user/findAll | GET |
Rest风格请求
功能 | 统一资源定位符 | 请求方式 |
---|---|---|
新增 | http://localhost:8888/user | POST |
修改 | http://localhost:8888/user | PUT |
删除 | http://localhost:8888/user/1 | DELETE |
查询一个 | http://localhost:8888/user/1 | GET |
查询所有 | http://localhost:8888/user | GET |
位置:方法参数
作用:用于绑定 url 中的占位符,url 支持占位符是 spring3.0 之后加入的,是springmvc 支持 rest 风格 URL 的一个重要标志。
属性:
使用:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
test01
<%--test05
test06
test08
test01
test02
--%>
新增
修改
删除
查询
获取学生信息
form表单针对 PUT 和 DELETE:实际填写 POST
hidden隐藏域: _method PUT | DELETE
执行器方法
@Controller
@RequestMapping("/studentController")
public class StudentController {
/*@RequestMapping(value = "addStudent",method = {RequestMethod.POST})*/
@PostMapping("addStudent")
public String addStudent(Student student){
System.out.println("add student = " + student);
return "main";
}
/*@RequestMapping(value = "getStudent/{sid}",method = {RequestMethod.GET})*/
@GetMapping("getStudent/{sid}")
public String getStudent(@PathVariable("sid") int id){
System.out.println("get id = " + id);
return "main";
}
/*@RequestMapping(value = "updateStudent",method = {RequestMethod.PUT})*/
@PutMapping("updateStudent")
public String updateStudent(Student student){
System.out.println("update student = " + student);
return "main";
}
/*@RequestMapping(value = "deleteStudent",method = {RequestMethod.DELETE})*/
@DeleteMapping("deleteStudent")
public String deleteStudent(int id){
System.out.println("dlelete id = " + id);
return "main";
}
}
过滤器web.xml文件中
<filter>
<filter-name>hiddenHttpMethodFilterfilter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
@GetMapping("resp01")
public String resp01() {
return "main";
}
@GetMapping("resp02")
public void resp02(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.sendRedirect("../main.jsp");
}
@GetMapping("resp03")
public ModelAndView resp03(ModelMap modelMap) {
ModelAndView modelAndView = new ModelAndView();
modelMap.addAttribute("username","小李");
modelAndView.setViewName("main");
return modelAndView;
}
(1) 引入依赖包
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.9version>
dependency>
(2)ResponseBody注解: 能够将bean List Map 转换成Json格式,异步响应会客户端浏览器
页面定义请求
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
test02
resp01
resp02
resp03
resp04
执行器方法
@Controller
@RequestMapping("/responseController")
public class ResponseController {
@GetMapping("resp01")
public void resp01(HttpServletRequest request, HttpServletResponse response) throws IOException {
List list = new ArrayList();
list.add(new Student("1","mary",18));
list.add(new Student("2","tom",18));
list.add(new Student("3","jack",18));
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(list);
PrintWriter writer = response.getWriter();
writer.print(string);
writer.close();
}
}
@GetMapping("resp02")
@ResponseBody
public Student resp02(){
return new Student("3","jack",18);
}
@GetMapping("resp03")
@ResponseBody
public List resp03(HttpServletRequest request, HttpServletResponse response) throws IOException {
List list = new ArrayList();
list.add(new Student("1","mary",18));
list.add(new Student("2","tom",18));
list.add(new Student("3","jack",18));
return list;
}
@GetMapping("resp04")
@ResponseBody
public Map resp04(HttpServletRequest request, HttpServletResponse response) throws IOException {
List list = new ArrayList();
list.add(new Student("1","mary",18));
list.add(new Student("2","tom",18));
list.add(new Student("3","jack",18));
Map map = new HashMap();
map.put("students",list);
return map;
}
@GetMapping("resp02")
public String resp02() throws IOException {
System.out.println("进来了");
return "forward:../main.jsp";
}
@GetMapping("resp02")
public String resp02() throws IOException {
System.out.println("进来了");
return "redirect:../main.jsp";
}
开发者 在开发或者调试网路或者是 B/S 模式的程序时,需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具。
Postman可以调试 简单的css、html、脚本等简单的网页基本信息。它还可发送几乎所有的 HTTP请求。Postman 在发送网络 HTTP 请求方面可以说是 Chrome插件类产品中的代表产品之一。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-irOEXjBR-1681302027341)(F:\Java语言\课程笔记\第六阶段\myself\SpringMVC\img\Postman工具使用1.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dQP45ocG-1681302027341)(F:\Java语言\课程笔记\第六阶段\myself\SpringMVC\img\Postman工具使用2.png)]
map;
}
### 二、转发和重定向
#### 1、forword 请求转发
```Java
@GetMapping("resp02")
public String resp02() throws IOException {
System.out.println("进来了");
return "forward:../main.jsp";
}
@GetMapping("resp02")
public String resp02() throws IOException {
System.out.println("进来了");
return "redirect:../main.jsp";
}
开发者 在开发或者调试网路或者是 B/S 模式的程序时,需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具。
Postman可以调试 简单的css、html、脚本等简单的网页基本信息。它还可发送几乎所有的 HTTP请求。Postman 在发送网络 HTTP 请求方面可以说是 Chrome插件类产品中的代表产品之一。