文章目录
- 前言
- 一、准备
- 1. 增加mavan配置
- 二、简单参数
- 1.JSP
- 2.Controller
- 三、复杂参数
- 1.JSP
- 2.Controller
- 三、扩展
- 1.JSP
- 2.header
- 3.cookie
- 4.session
- 总结
前言
在上一章的基础上,我们来学习对于请求参数的解析,前后端分离已经是大势所趋,JSP相关部分建议大家了解即可。
一、准备
1. 增加mavan配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <!-- 确保你使用的是支持-parameters标志的版本 -->
<configuration>
<source>17</source> <!-- 或者你的目标Java版本 -->
<target>17</target> <!-- 或者你的目标Java版本 -->
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
二、简单参数
1.JSP
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello RequestParam</title>
</head>
<body>
<h1><%= "简单类型参数演示" %></h1>
<br/>
<form action="/params01" method="post">
<span>姓名:</span><input name="name">
<span>年龄:</span><input name="age">
<button type="submit">params01</button>
</form>
<form action="/params02" method="post">
<span>姓名:</span><input name="userName">
<button type="submit">params02</button>
</form>
<a href="/params03">params03</a><br>
<a href="/params04">params04</a><br>
</body>
</html>
2.Controller
package org.example.springmvc.params.controller;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Create by zjg on 2024/4/27
*/
@Controller
public class ParamsController {
@RequestMapping("hello-params")
public String params(HttpServletRequest request){//重定向
System.out.println(request.getCharacterEncoding());
return "redirect:params/params.jsp";
}
@RequestMapping(value = "params01",method = RequestMethod.POST)
public String params01(String name, Integer age){//接收两种参数
System.out.println(name);
System.out.println(age);
return "forward:params/params.jsp";
}
@RequestMapping(value="params02",method = RequestMethod.POST)
public String params02(@RequestParam("userName") String name){//别名
System.out.println(name);
return "forward:params/params.jsp";
}
@RequestMapping("params03")
public String params03(@RequestParam(required = false) String name){//非必传
System.out.println(name);
return "forward:params/params.jsp";
}
@RequestMapping("params04")
public String params04(@RequestParam(defaultValue = "admin")String name){//默认值
System.out.println(name);
return "forward:params/params.jsp";
}
}
三、复杂参数
1.JSP
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello RequestParam</title>
</head>
<body>
<h1><%= "复杂类型参数演示" %></h1>
<form action="/params05" method="post">
<span>编号:</span><input name="id"><br>
<span>姓名:</span><input name="name"><br>
<span>外号:</span>
<input name="alias[0]" type="checkbox" value="狗蛋">狗蛋
<input name="alias[1]" type="checkbox" value="狗蛋">铁蛋<br>
<span>爱好:</span>
<input name="hobbies[0]" type="checkbox" value="读书">读书
<input name="hobbies[1]" type="checkbox" value="看报">看报<br>
<span>家人们:</span>
<input name="relatives[father]" type="checkbox" value="爸爸">爸爸
<input name="relatives[mother]" type="checkbox" value="妈妈">妈妈<br>
<span>角色:</span>
<span>编号:</span><input name="role.id">
<span>名称:</span><input name="role.name"><br>
<span>朋友们:</span><br>
<span>编号:</span><input name="friends[0].id">
<span>名称:</span><input name="friends[0].name"><br>
<span>编号:</span><input name="friends[1].id">
<span>名称:</span><input name="friends[1].name"><br>
<button type="submit">params05</button>
</form>
</body>
</html>
2.Controller
package org.example.springmvc.params.controller;
import jakarta.servlet.http.HttpServletRequest;
import org.example.springmvc.params.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Create by zjg on 2024/4/27
*/
@Controller
public class ParamsController {
@RequestMapping("hello-params")
public String params(HttpServletRequest request){//重定向
System.out.println(request.getCharacterEncoding());
return "redirect:params/params.jsp";
}
@RequestMapping("params05")
public String params05(User user){//复杂对象
System.out.println(user);
return "forward:params/params.jsp";
}
}
User(id=1, name=张三, alias=[狗蛋, 狗蛋], hobbies=[读书, 看报], relatives={father=爸爸, mother=妈妈}, role=Role(id=1, name=管理员), friends=[User(id=1, name=小李, alias=null, hobbies=null, relatives=null, role=null, friends=null), User(id=2, name=小王, alias=null, hobbies=null, relatives=null, role=null, friends=null)])
三、扩展
1.JSP
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello RequestParam</title>
</head>
<body>
<h1><%= "扩展类型参数演示" %></h1>
<form action="/header" method="post">
<button type="submit">header</button>
</form>
<form action="/cookie" method="post">
<button type="submit">cookie</button>
</form>
<form action="/session" method="post">
<button type="submit">session</button>
</form>
</body>
</html>
2.header
//header
@RequestMapping(value = "header")
public String header(@RequestHeader("Host") String host){
System.out.println("请求头中的Host变量"+host);
return path;
}
3.cookie
//cookie
@RequestMapping(value = "cookie")
public String cookie(@CookieValue("JSESSIONID") String id){
System.out.println("请求中的cookie变量"+id);
return path;
}
4.session
//session
@RequestMapping(value = "session")
public String session(@SessionAttribute("JSESSIONID") String id){
System.out.println("请求中的session变量"+id);
return path;
}
总结
回到顶部