接上一节:读 Spring Boot实战笔记–day003
其他配置
viewController 配置:
package com.example.springboot.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author: hyh
* @Date: 2021/11/15 16:27
**/
@Controller
public class ControllerH {
// 通过ModelAttribute 注解获取参数 msg
@GetMapping("index")
public void index(@ModelAttribute("msg") String msg){
}
// 跳转主页面 可以配置在是视图配置里
@RequestMapping("/index")
public String toIndex(){
return "index";
}
}
package com.example.springboot.mvc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
/**
* @Author: hyh
* @Date: 2021/11/15 15:12
**/
@Configuration
@EnableWebMvc // 开启 webmvc
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//addResourceLocations 指的是文件放置的目录,addResourceHandler指的是对外暴露的访问路径。
registry.addResourceHandler("/assets/**").addResourceLocations("class:/assets/");
}
@Bean
public MyInterceptor interceptor(){
return new MyInterceptor();
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/class/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
// 拦截所有请求
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor());
}
// 直接配置在视图配置里
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("index").setViewName("index");
}
}
路径匹配参数配置:
在 Spring MVC 中,路径参数如果带“.”的话,“.”后面的值将被忽略,例如,访问http://localhost:8080/highlight _springmvc4/anno/pathvar/xx.yy,此时“.”后面的yy被忽略,如图4-10所示。
通过重写configurePathMatch方法可不忽略“.”后面的参数,代码如下:
package com.example.springboot.mvc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
/**
* @Author: hyh
* @Date: 2021/11/15 15:12
**/
@Configuration
@EnableWebMvc // 开启 webmvc
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//addResourceLocations 指的是文件放置的目录,addResourceHandler指的是对外暴露的访问路径。
registry.addResourceHandler("/assets/**").addResourceLocations("class:/assets/");
}
@Bean
public MyInterceptor interceptor(){
return new MyInterceptor();
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/class/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
// 拦截所有请求
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor());
}
// 直接配置在视图配置里
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("index").setViewName("index");
}
// 重写方法 不过滤 .
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}
}
文件上传配置:
文件上传是一个项目里经常要用的功能,Spring MVC通过配置一个MultipartResolver来上传文件。在 Spring 的控制器中,通过 MultipartFile file来接收文件,通过MultipartFile[]files接收多个文件上传。
(1)添加文件上传依赖。
<!-- file upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!--非必需,可简化1/O操作-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
// 前台代码 post 请求 upload 路径
<div class-"upload">
<form action="upload" enctype="multipart/form-data" method="post">
<input type-"file" name="file"/><br/>
<input type=" submit" value="上传">
</form>
</div>
package com.example.springboot.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
/**
* @Author: hyh
* @Date: 2021/11/15 16:27
**/
@Controller
public class ControllerH {
@PostMapping("/upload")
@ResponseBody
public String upload(MultipartFile file){
// 编写上传逻辑
return "ok";
}
}
package com.example.springboot.mvc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
/**
* @Author: hyh
* @Date: 2021/11/15 15:12
**/
@Configuration
@EnableWebMvc // 开启 webmvc
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//addResourceLocations 指的是文件放置的目录,addResourceHandler指的是对外暴露的访问路径。
registry.addResourceHandler("/assets/**").addResourceLocations("class:/assets/");
}
@Bean
public MyInterceptor interceptor(){
return new MyInterceptor();
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/class/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
// 拦截所有请求
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor());
}
// 直接配置在视图配置里
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("index").setViewName("index");
// 调用 toUpload 会转到 upload 方法
registry.addViewController("toUpload").setViewName("upload");
}
// 重写方法
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}
// 配置 bean 对象
@Bean
public MultipartResolver multipartResolver(){
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
// 文件最大为 10M
resolver.setMaxUploadSize(10 * 1024 * 1024);
return resolver;
}
}