码迷,mamicode.com
首页 > 移动开发 > 详细

访问webapp目录报404错误

时间:2019-01-14 21:37:23      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:direct   body   source   view   void   ted   registry   add   handle   

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()); } }

 

访问webapp目录报404错误

标签:direct   body   source   view   void   ted   registry   add   handle   

原文地址:https://www.cnblogs.com/linhongwenBlog/p/10268823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!