基于一组简单变量请求参数:
使用场景:传递复选框checkbox
ParamController:param6
package com.baizhiedu;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/param")
public class ParamController {
//通过request获取求情参数
@RequestMapping("/param1")
public String param1(HttpServletRequest request){
String name=request.getParameter("name");
String password=request.getParameter("password");
System.out.println("name="+name);
System.out.println("password="+password);
return "param1";
}
//通过简单变量,获取请求参数
@RequestMapping("/param2")
public String param2(String name,String password){
System.out.println("name="+name+" password="+password);
return "param1";
}
//通过简单变量,获取请求参数,把http协议 传过来的字符串年龄,自动类型转换为整型
@RequestMapping("/param3")
public String param3(String name,Integer age){ //基本数据类型尽量使用包装器,如果不使用基本数据的包装类,如果在地址栏传值时没有给年龄int类型传值,默认为null,int类型不能接收null导致报错
System.out.println("name="+name+"age="+age);
return "param1";
}
//基于POJO类型,获取请求参数
@RequestMapping("/param4")
public String param4(User user){
System.out.println("User="+user);
return "param1";
}
//基于简单变量接收简单类型参数和基于POJO类型获取请求参数
@RequestMapping("/param5")
public String param5(String name,User user){ //name形参和POJO里面的name属性都会获取内容
System.out.println("name="+name);
System.out.println("User="+user);
return "param1";
}
//接收一组简单类型的参数
@RequestMapping("/param6")
public String param6(int[] ids){
for(int id:ids){
System.out.println("id="+id);
}
return "param1";
}
}
checkbox.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/3
Time: 15:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/param/param6">
<input type="checkbox" name="ids" value="1"><br>
<input type="checkbox" name="ids" value="2"><br>
<input type="checkbox" name="ids" value="3"><br>
<input type="checkbox" name="ids" value="4"><br>
<input type="submit" value="reg">
</form>
</body>
</html>
接收一组POJO类型对象的请求参数:
使用场景:接收一组POJO类型的请求参数,用于批量操作,一次性注册多个用户
可以通过自定义一个类型UserDAO,里面声明相对应的属性users
UserDTO:
package com.baizhiedu;
import java.util.ArrayList;
import java.util.List;
public class UserDTO {
private List<User> users=new ArrayList<>();
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
ParamController:param7
package com.baizhiedu;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
@RequestMapping("/param")
public class ParamController {
//通过request获取求情参数
@RequestMapping("/param1")
public String param1(HttpServletRequest request){
String name=request.getParameter("name");
String password=request.getParameter("password");
System.out.println("name="+name);
System.out.println("password="+password);
return "param1";
}
//通过简单变量,获取请求参数
@RequestMapping("/param2")
public String param2(String name,String password){
System.out.println("name="+name+" password="+password);
return "param1";
}
//通过简单变量,获取请求参数,把http协议 传过来的字符串年龄,自动类型转换为整型
@RequestMapping("/param3")
public String param3(String name,Integer age){ //基本数据类型尽量使用包装器,如果不使用基本数据的包装类,如果在地址栏传值时没有给年龄int类型传值,默认为null,int类型不能接收null导致报错
System.out.println("name="+name+"age="+age);
return "param1";
}
//基于POJO类型,获取请求参数
@RequestMapping("/param4")
public String param4(User user){
System.out.println("User="+user);
return "param1";
}
//基于简单变量接收简单类型参数和基于POJO类型获取请求参数
@RequestMapping("/param5")
public String param5(String name,User user){ //name形参和POJO里面的name属性都会获取内容
System.out.println("name="+name);
System.out.println("User="+user);
return "param1";
}
//接收一组简单类型的参数
@RequestMapping("/param6")
public String param6(int[] ids){
for(int id:ids){
System.out.println("id="+id);
}
return "param1";
}
//用于获取一组POJO类型的请求参数
@RequestMapping("/param7")
public String param7(UserDTO userDTO){
List<User> users=userDTO.getUsers();
for(User user:users){
System.out.println("user="+user);
}
return "param1";
}
}
users.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/3
Time: 17:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/param/param7">
UserName1:<input type="text" name="users[0].name"/><br>
UserPassword1:<input type="text" name="users[0].password"/><br>
UserAge1:<input type="text" name="users[0].age"/><br>
<hr>
UserName2:<input type="text" name="users[1].name"/><br>
UserPassword2:<input type="text" name="users[1].password"/><br>
UserAge2:<input type="text" name="users[1].age"/><br>
<input type="submit" value="submit">
</form>
</body>
</html>
总结: