Spring boot 配置 webapp 下资源文件

作者 : 开心源码 本文共3422个字,预计阅读时间需要9分钟 发布时间: 2022-05-11 共63人阅读

spring boot 在 1.5.x以后已经不支持 jar 打包用 jsp 了,必需用 war 打包,引入 static 下静态资源文件能用 classpath:/static/,假如指定到 webapp 下静态资源文件不可以用 classpath:,直接以 / 开头即可以。

import java.util.List;

import org.springframework.boot.web.servlet.FilterRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.Ordered;

import org.springframework.web.filter.HiddenHttpMethodFilter;

import org.springframework.web.filter.HttpPutFormContentFilter;

import org.springframework.web.servlet.HandlerExceptionResolver;

import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.znv.framework.common.error.ServiceExceptionHandler;

import com.znv.framework.interceptors.CommonInterceptor;

import com.znv.framework.spring.SpringContextUtil;

/**

* @author Chenfei

*/

@EnableWebMvc

@Configuration

public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Override

public void addViewControllers(ViewControllerRegistry registry) {

registry.addViewController(“/”).setViewName(“forward:/views/default”);

registry.setOrder(Ordered.HIGHEST_PRECEDENCE);

}

@Override

public void addInterceptors(InterceptorRegistry registry) {

/**

* 自己设置阻拦器

*/

registry.addInterceptor(new CommonInterceptor()).addPathPatterns(“/**”).excludePathPatterns(“/resources/**”,

“/main/uscc/**”, “/lang/**”, “/**/js/**”, “/**/css/**”, “/**/*.xml”, “/**/bootstrap/**”, “/**/plugins/**”,

“/**/404”, “/**/500”, “/**/error”, “/webcontent/**”, “/files/**”, “/main/mobile/**”, “/mobile/**”);

}

/**

* 关于PUT方法获取不到参数的解决

*/

@Bean

public FilterRegistrationBean hiddenHttpMethodFilter() {

FilterRegistrationBean registration = new FilterRegistrationBean();

//注入过滤器

registration.setFilter(new HiddenHttpMethodFilter());

//阻拦规则

registration.addUrlPatterns(“/”);

//过滤器名称

registration.setName(“HiddenHttpMethodFilter”);

//过滤器顺序

registration.setOrder(1);

return registration;

}

/**

* 关于PUT方法获取不到参数的解决

*/

@Bean

public HttpPutFormContentFilter httpPutFormContentFilter() {

return new HttpPutFormContentFilter();

}

/**

* 自己设置静态资源映射

*/

@Override

public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

configurer.enable();

}

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler(“/**”).addResourceLocations(“classpath:/META-INF/resources/”,

“classpath:/resources/”, “classpath:/static/”, “classpath:/public/”);

registry.addResourceHandler(“/views/**”).addResourceLocations(“classpath:/static/main/”);

// 指到 webapp 目录下

registry.addResourceHandler(“/webcontent/**”).addResourceLocations(“/webcontent/”);

}

/**

* 视图配置

* @return

*/

@Bean

public InternalResourceViewResolver resourceViewResolver() {

InternalResourceViewResolver resolver = new InternalResourceViewResolver();

resolver.setPrefix(“/main/”);

resolver.setSuffix(“”);

return resolver;

}

@Override

public void configureViewResolvers(ViewResolverRegistry registry) {

registry.viewResolver(resourceViewResolver());

// registry.jsp(“”, “.jsp”);

}

代码尽管复杂,但我相信你们会看懂的对不对!求关注啊,精彩内容稍后奉上!需要更多详细内容请理解尚学堂哦!

Spring boot 配置 webapp 下资源文件

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Spring boot 配置 webapp 下资源文件

发表回复