接上一节:读 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; @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; @Configuration @EnableWebMvc // 开启 webmvc public class MyMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //addResourceLocations 指的是文件放置的目录,addResourceHandler指的是对外暴露的访问路径。 registry.addResourceHandler("/assets @Configuration @EnableWebMvc // 开启 webmvc public class MyMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //addResourceLocations 指的是文件放置的目录,addResourceHandler指的是对外暴露的访问路径。 registry.addResourceHandler("/assets @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; @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; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)