spring boot打包后,访问webapp目录报了404.并且java包中没有前端代码
解决办法:
1、 配置pom.xml
<build> <resources> <resource> <directory>src/main/webapp</directory> <!--注意此次必须要放在此目录下才能被访问到 --> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*</include> </includes> </resource> </resources> </build>
2、 重写WebMvcConfigurerAdapter适配器
addResourceHandlers方法
package com.iwhalecloud.visualization.config; import java.nio.charset.Charset; import java.util.List; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * Created by 61990 on 2019/1/14. */ @Configuration @EnableWebMvc @ComponentScan public class PortalWebConfig extends WebMvcConfigurerAdapter{ @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/"); } /** * 自定义静态资源映射 */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } //解决后端代码传到前端时,中文乱码问题 @Bean public HttpMessageConverter<String> responseBodyConverter(){ StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8")); return converter; } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); converters.add(responseBodyConverter()); } }
原文地址:https://www.cnblogs.com/linhongwenBlog/p/10268823.html
时间: 2024-11-09 02:14:50