解决springboot 无法找到html页面报404以及静态文件找不到问题

对于html文件的处理在启动类增加InternalResourceViewResolver 视图解析
@SpringBootApplication
@EnableWebMvc
@EnableRabbit
public class MallPortalApplication {

    public static void main(String[] args) {
        SpringApplication.run(MallPortalApplication.class, args);
    }

    @Bean
    public InternalResourceViewResolver setupViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/templates/");
        resolver.setSuffix(".html");
        return resolver;

    }

}
对于静态文件的处理是增加如下配置实现WebMvcConfigurer 类

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new PageArgumentResolver());
    }

    /**
     *  视图跳转控制器
     * 无业务逻辑的跳转 均可以以这种方法写在这里
     * @param registry
     */
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index");
    }

//最主要的是在这里
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        //告知系统static 当成 静态资源访问
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/preview/static/**").addResourceLocations("classpath:/static/");
    }
}

猜你喜欢

转载自blog.csdn.net/saygood999/article/details/106490266