SpringMVC4-SpringMVC获取请求参数

news/2024/9/29 17:38:00/

目录

通过ServletAPI获取(不常用)

通过控制器方法的形参获取请求参数

@RequestParam

@RequestHeader

@CookieValue

通过POJO获取请求参数

解决获取请求参数的乱码问题


test_param.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>测试请求参数</title>
</head>
<body>
<h1>测试请求参数</h1>
</body>
</html>
package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class TestController {@RequestMapping("/param")public String param(){return "test_param";}}

启动:

通过ServletAPI获取(不常用)

将HttpServletRequest作为控制器方法的形参,此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class ParamController {@RequestMapping("/testServletAPI")//形参位置的request表示当前请求public String testServletAPI(HttpServletRequest request){String username = request.getParameter("username");String password = request.getParameter("password");System.out.println("username:"+username+",password:"+password);return "success";}}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>测试请求参数</title>
</head>
<body>
<h1>测试请求参数</h1>
<a th:href="@{/testServletAPI(username='admin',password=123456)}">测试使用ServletAPI获取请求参数</a>
</body>
</html>

通过控制器方法的形参获取请求参数

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class ParamController {@RequestMapping("/testParam")public String testParam(String username,String password){System.out.println("username:"+username+",password:"+password);return "success";}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>测试请求参数</title>
</head>
<body>
<h1>测试请求参数</h1>
<a th:href="@{/testParam(username='admin',password=123456)}">测试使用控制器的形参获取请求参数</a>
</body>
</html>

注:

若请求所传输的请求参数中有多个同名的请求参数,此时可以在控制器方法的形参中设置字符串数组或者字符串类型的形参接收此请求参数

  • 若使用字符串数组类型的形参,此参数的数组中包含了每一个数据
  • 若使用字符串类型的形参,此参数的值为每个数据中间使用逗号拼接的结果

字符串数组:

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class ParamController {@RequestMapping("/testParam")public String testParam(String username,String password,String[] hobby){System.out.println("username:"+username+",password:"+password+",hobby:"+ Arrays.toString(hobby));return "success";}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>测试请求参数</title>
</head>
<body>
<h1>测试请求参数</h1>
<form th:action="@{/testParam}" method="post">用户名:<input type="text" name="username"><br>密码:<input type="password" name="password"><br>爱好:<input type="checkbox" name="hobby" value="a">a<input type="checkbox" name="hobby" value="b">b<input type="checkbox" name="hobby" value="c">c<br><input type="submit" value="测试使用控制器的形参获取请求参数">
</form>
</body>
</html>

 

字符串:

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class ParamController {@RequestMapping("/testParam")public String testParam(String username,String password,String hobby){System.out.println("username:"+username+",password:"+password+",hobby:"+hobby);return "success";}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>测试请求参数</title>
</head>
<body>
<h1>测试请求参数</h1>
<form th:action="@{/testParam}" method="post">用户名:<input type="text" name="username"><br>密码:<input type="password" name="password"><br>爱好:<input type="checkbox" name="hobby" value="a">a<input type="checkbox" name="hobby" value="b">b<input type="checkbox" name="hobby" value="c">c<br><input type="submit" value="测试使用控制器的形参获取请求参数">
</form>
</body>
</html>

@RequestParam

@RequestParam是将请求参数和控制器方法的形参创建映射关系

@RequestParam注解一共有三个属性:

value:指定为形参赋值的请求参数的参数名

required:设置是否必须传输此请求参数,默认值为true

若设置为true时,则当前请求必须传输value所指定的请求参数,若没有传输该请求参数,且没有设置defaultValue属性,则页面报错400:Required String parameter 'xxx' is not present;若设置为false,则当前请求不是必须传输value所指定的请求参数;若没有传输,则注解所标识的形参的值为null

defaultValue:不管required属性值为true或false,当value所指定的请求参数没有传输或传输的值为""时,则使用默认值为形参赋值

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>测试请求参数</title>
</head>
<body>
<h1>测试请求参数</h1>
<form th:action="@{/testParam}" method="post">用户名:<input type="text" name="user_name"><br>密码:<input type="password" name="password"><br>爱好:<input type="checkbox" name="hobby" value="a">a<input type="checkbox" name="hobby" value="b">b<input type="checkbox" name="hobby" value="c">c<br><input type="submit" value="测试使用控制器的形参获取请求参数">
</form>
</body>
</html>
package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;@Controller
public class ParamController {@RequestMapping("/testParam")public String testParam(@RequestParam("user_name") String username, String password, String[] hobby){System.out.println("username:"+username+",password:"+password+",hobby:"+ Arrays.toString(hobby));return "success";}
}

@RequestHeader

@RequestHeader是将请求头信息和控制器方法的形参创建映射关系

@RequestHeader注解一共有三个属性:value、required、defaultValue,用法同@RequestParam

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;@Controller
public class ParamController {@RequestMapping("/testParam")public String testParam(@RequestParam("user_name") 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";}
}

 

@CookieValue

@CookieValue是将cookie数据和控制器方法的形参创建映射关系

@CookieValue注解一共有三个属性:value、required、defaultValue,用法同@RequestParam

创建session:

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;@Controller
public class ParamController {@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("user_name") String username, String password, String[] hobby,@RequestHeader("Host") 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";}
}

通过POJO获取请求参数

可以在控制器方法的形参位置设置一个实体类类型的形参,此时若浏览器传输的请求参数的参数名和实体类中的属性名一致,那么请求参数就会为此属性赋值

package com.qcby.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;}@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", age=" + age +", sex='" + sex + '\'' +", email='" + email + '\'' +'}';}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>测试请求参数</title>
</head>
<body>
<h1>测试请求参数</h1>
<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" value="使用实体类接收请求参数">
</form>
</body>
</html>
package com.qcby.mvc.controller;import com.qcby.mvc.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;@Controller
public class ParamController {@RequestMapping("/testpojo")public String testpojo(User user){System.out.println(user);return "success";}
}

解决获取请求参数的乱码问题

解决获取请求参数的乱码问题,可以使用SpringMVC提供的编码过滤器CharacterEncodingFilter,但是必须在web.xml中进行注册

<!--配置springMVC的编码过滤器-->
<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中处理编码的过滤器一定要配置到其他过滤器之前,否则无效


http://www.ppmy.cn/news/1532019.html

相关文章

性能调优知识点(mysql)三

SQL底层执行原理 MySQL的内部组件结构&#xff1a;大体来说&#xff0c;MySQL 可以分为 Server 层和存储引擎层store两部分 Server层:主要包括连接器、查询缓存、分析器、优化器、执行器等&#xff0c;涵盖 MySQL 的大多数核心服务功能&#xff0c;以及所有的内置函数&#xf…

R包:VennDiagram韦恩图

加载R包 library(VennDiagram)数据 # Prepare character vectors v1 <- c("DKK1", "NPC1", "NAPG", "ERG", "VHL", "BTD", "MALL", "HAUS1") v2 <- c("SMAD4", "DKK1…

一、Spring Boot集成Spring Security之自动装配

Spring Boot集成Spring Security之自动装配介绍 一、实现功能及软件版本说明二、创建Spring Boot项目三、查看自动装配配置类四、自动装配配置类之SecurityAutoConfiguration1、SecurityAutoConfiguration部分源码2、主要作用3、SpringBootWebSecurityConfiguration3.1、Spring…

深入探讨AI 神经网络:类型、特点与创新应用

一、引言 1.1 背景 随着科技的飞速发展,人工智能已经成为当今社会的热门领域。在人工智能的发展过程中,神经网络扮演着至关重要的角色。神经网络是一种模拟人类大脑神经元结构的计算模型,它通过大量的神经元相互连接并进行信息处理,从而实现对数据的分析和预测。不同类型…

网络空间搜索引擎- FOFA的使用技巧总结

网络空间搜索引擎- FOFA的使用技巧总结 简介 FOFA是一款网络空间测绘的搜索引擎&#xff0c;旨在帮助用户以搜索的方式查找公网上的互联网资产。 FOFA的查询方式类似于谷歌或百度&#xff0c;用户可以输入关键词来匹配包含该关键词的数据。不同的是&#xff0c;这些数据不仅包…

CNN-LSTM预测 | MATLAB实现CNN-LSTM卷积长短期记忆神经网络时间序列预测

CNN-LSTM预测 | MATLAB实现CNN-LSTM卷积长短期记忆神经网络时间序列预测 目录 CNN-LSTM预测 | MATLAB实现CNN-LSTM卷积长短期记忆神经网络时间序列预测预测效果基本介绍模型描述程序设计参考资料预测效果 基本介绍 本次运行测试环境MATLAB2020b 提出一种包含卷积神经网络和长短…

C语言 | 第四章 | 常量 运算符

P 31 课后练习评讲 2022/8/24 一、题目 代码演示:第一题 #include<stdio.h>void main(){// 分析&#xff1a;使用不同的变量来保存对应的数据char name[10] "张三"; // 字符数组&#xff0c;存放字符串short age 23;float score 78.5f;char gender M; …

Java项目实战II基于Java+Spring Boot+MySQL的美容院管理系统设计与实现(源码+数据库+文档)

目录 一、前言 二、技术介绍 三、系统实现 四、文档参考 五、核心代码 六、源码获取 全栈码农以及毕业设计实战开发&#xff0c;CSDN平台Java领域新星创作者&#xff0c;专注于大学生项目实战开发、讲解和毕业答疑辅导。获取源码联系方式请查看文末 一、前言 在快速发展…