SSM系列——SpringMVC资源开放,乱码处理,数据请求(续)
开放资源的访问
我们需要在spring-mvc.xml中添加以下中任意一个
<!-- 设置springmvc开启资源的访问位置-->
<mvc:resources mapping="/static/**" location="/static/"></mvc:resources>
<!-- 让mvc去寻求tomcat,让tomcat开启访问位置-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
这里注意一点,资源的路径是从webapp下开始写的!
处理请求数据乱码问题
在tomcat10版本中post请求中文乱码已被解决,但是我们依然需要保证对中文乱码的处理,因此我们可以添加一个过滤器进行乱码的处理
我们需要在web.xml中进行如下配置
<!-- 配置全局处理乱码的filter-->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
数据请求注解
@RequestParam(参数绑定)
当请求的参数名称与Controller的业务方法参数名称不-致时, 就需要通过@RequestParam注解显示的绑定
内部参数
- value:与请求参数名称
- required:此在指定的请求参数是否必须包括,默认是true,提交时如果没有此参数则报错
- defaultValue:当没有指定请求参数时,则使用指定的默认值赋值
源码
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
}
实例
我们将@RequestParam("en")中的en设置为请求参数名,映射到enter上
RequestParamController
package com.example.mvc1.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestParamController {
@GetMapping("/param1")
public void test1(@RequestParam("en") String enter){
System.out.println(enter);
}
}
测试
SpringMVC获取Restful风格参数
Restful是一种软件架构风格、 设计风格,而不是标准,只是提供了一组设计原则和约束条件。主要用于客户端和服务器交互类的软件,基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存机制等。
请求方式
Restful风格的请求是使用url+ 请求方式表示次请求目的的,HTTP 协议里面四个表示操作方式如下:
- GET:用于获取资源
- POST:用于新建资源
- PUT:用于更新资源
- DELETE:用于删除资源
举例:
| 地址 | 说明 |
|---|---|
| /user/1 | GET : 得到id= 1的user |
| /user/1 | DELETE : 删除id= 1的user |
| /user/1 | PUT : 更新id=1的user |
| /user | POST : 新增user |
@PathVariable注解
@RequestParam 是从request里面拿取值,而 @PathVariable 是从一个URI模板里面来填充
从源码上看差别不大
这里我不讲源码,我会在以后的推出Spring源码系列在其中的注解源码讲解中对两者从源码的角度进行分析
改写为Restful风格
对应
RequestParamController
@GetMapping("/param2/{en}")
public void test2(@PathVariable("en") String enter){
System.out.println(enter);
}
自定义类型转换器
SpringMVC默认已经提供了一些常用的类型转换器,如String-->int,String-->list等
但是不是所有的数据类型都提供了转换器,没有提供的就需要自定义转换器,比如你接收了一个String其实是日期类型的,这时候就需要我们自己写类型转换
以下演示String --> Date 我个人不建议使用String --> Date 建议转为LocalDate
步骤
1. 定义转换器类实现Converter接口
注意是这个包:import org.springframework.core.convert.converter.Converter;
package com.example.mvc1.converter;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
2. 在配置文件中声明转换
进入spring-mvc.xml添加如下配置
<!-- 声明转换器-->
<bean id="myConvert" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.example.mvc1.converter.DateConverter"></bean>
</list>
</property>
</bean>
3. 在< annotation-driven >中引用转换器
在spring-mvc.xml中
<mvc:annotation-driven conversion-service="myConvert"></mvc:annotation-driven>
完整spring-mvc.xml
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- controller的组件扫描-->
<context:component-scan base-package="com.example.mvc1.controller"></context:component-scan>
<mvc:annotation-driven conversion-service="myConvert"></mvc:annotation-driven>
<!-- <context:component-scan base-package="com">-->
<!--<!– 包括的过滤器–>-->
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
<!--<!– 不包括的过滤器–>-->
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
<!-- </context:component-scan>-->
<!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!-- <!– 设置前缀–>-->
<!-- <property name="prefix" value="/WEB-INF/html/"></property>-->
<!-- <!– 设置后缀–>-->
<!-- <property name="suffix" value=".jsp"></property>-->
<!-- </bean>-->
<!-- 设置springmvc开启资源的访问位置-->
<mvc:resources mapping="/static/**" location="/static/"></mvc:resources>
<!-- 让mvc去寻求tomcat,让tomcat开启访问位置-->
<!-- <mvc:default-servlet-handler></mvc:default-servlet-handler>-->
<!-- 声明转换器-->
<bean id="myConvert" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.example.mvc1.converter.DateConverter"></bean>
</list>
</property>
</bean>
</beans>
测试
controller
@GetMapping("/date")
public void test3(Date date){
System.out.println(date);
}
获取Servlet相关API(原始Servlet)
SpringMVC支持使用原始ServletAPI对象作为控制器方法的参数进行注入,常用的对象如下:
- HttpServletRequest
- HttpServletResponse
- HttpSession
简单知道一下就行
@GetMapping("/servlet")
public void test1(HttpServletRequest request,
HttpServletResponse response,
HttpSession session){
System.out.println(request);
System.out.println(response);
System.out.println(session);
}
SpringMVC获取请求头
在真实业务场景中我们可能会遇到需要获取请求头中的某个参数,例如: 我们前端将用户的token令牌设置到请求头中,后端使用请求头中的token信息进行判断用户状态,用户信息等
@RequestHeader
该注解可以获取到请求头中的信息,相当于request.getHeader()方法 属性有:
- value:请求头名称
- required:是否必须携带
源码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestHeader {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
}
可获取的请求头(原始)
GET /header HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Cookie: splunkweb_csrf_token_8000=7230890568853798578; JSESSIONID=F5B7C57742E540679B414F3E88CF9011; token_key=7230890568853798578; experience_id=b857f012-afe4-7a2c-0df8-c9c9a8f18245
Upgrade-Insecure-Requests: 1
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
DNT: 1
Sec-GPC: 1
实例
package com.example.mvc1.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class headerController {
@GetMapping("/header")
public void test1(@RequestHeader("Host")String host,@RequestHeader("User-Agent") String agent){
System.out.println(host);
System.out.println(agent);
}
}
@CookieValue用于获取Cookie
文件上传
enctype= "application/x www-form-urlencoded"时, form表单的正文内容格式是:key= value&key= value&key= value
当form表单的enctype取值为Mutilpart/form-data时,请求正文内容就变成多部分形式:
实例代码
1. 导入依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
2.编写静态页面(jsp,html都行)
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %>
</h1>
<br/>
<a href="hello-servlet">Hello Servlet</a>
<form method="post" action="/up" enctype="multipart/form-data" >
<input type="text" name="upUser">
<input type="file" name="upFile">
<input type="submit" value="up">
</form>
</body>
</html>
3. 配置文件上传解析器
到spring-mvc.xml中,其单位为B
<!-- 配置文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置编码-->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 配置单个上传文件的大小-->
<property name="maxUploadSizePerFile" value="10485760"></property>
<property name="maxUploadSize" value="10485760"></property>
</bean>
4.controller代码
package com.example.mvc1.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@RestController
public class UploadController {
@PostMapping("/up")
public void test(String upUser, MultipartFile upFile) throws IOException {
System.out.println(upUser);
System.out.println(upFile);
String contentType = upFile.getContentType();
String filename = upFile.getOriginalFilename();
upFile.transferTo(new File("D:\\2020-2024大学学习\\大二第二学期\\JavaWeb\\mvc1\\src\\main\\webapp\\static\\"+filename));
System.out.println(contentType);
System.out.println(filename);
}
}
结果展示
多文件上传用MultipartFile[]数组
只需要在单文件的基础上改为
MultipartFile[]