SpringMVC-获取参数
环境
IDE:idea 2020.3
构建工具:maven-3.8.1
服务器:tomcat9
Spring版本:5.3.1
<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring5和Thymeleaf整合包 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
</dependencies>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- springMVC.xml -->
<!-- 自动扫描包 -->
<context:component-scan base-package="com.fly.controller"/>
<!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
</beans>
<!-- web.xml -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--
作为框架的核心组件,在启动过程中有大量的初始化操作要做
而这些操作放在第一次请求时才执行会严重影响访问速度
因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时
-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
通过ServletAPI获取
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<!--index.html-->
<form th:action="@{/testParam}" method="get">
用户名:<label>
<input type="text" name="userName">
</label>
<input type="submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
<!--hello.html-->
<h2>hello</h2>
</body>
</html>
/**
*HelloController
*/
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testParam")
public String testParam(HttpServletRequest request) {
String userName = request.getParameter("userName");
System.out.println("userName = " + userName);
return "hello";
}
通过控制器方法的形参获取请求参数
在控制器方法中设置与请求参数名一样的形参,DispatcherServlet就会将请求参数赋值给相应的形参
如果请求参数中有多个同名的参数,可以设置一个数组来接收
@RequestParam
@RequestParam将请求参数和控制器方法的形参进行绑定,有三个属性:
1:value:指定为形参赋值的请求参数的参数名
2:required:设置是否必须传输此请求参数,默认值为true
3:defaultValue:不管required属性值为true或false,当value所指定的请求参数没有传输或传输的值为""时,则使用默认值为形参赋值
@RequestHeader
@RequestHeader是将请求头信息和控制器方法的形参创建映射关系,用法同@RequestParam
@CookieValue
@CookieValue是将cookie数据和控制器方法的形参创建映射关系,用法同@RequestParam
通过POJO获取请求参数
可以在控制器方法中设置一个实体类类型的形参,如果请求参数的参数名和实体类中的属性名一致,那么请求参数就会为此属性赋值
package com.fly.entity;
/**
* @author 26414
*/
public class Person {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"userName='" + userName + '\'' +
'}';
}
public Person(String userName) {
this.userName = userName;
}
}
解决乱码
在web.xml中注册编码过滤器CharacterEncodingFilte
通过源码可以得出过滤器
<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>