springresourcetemplateresolver的setprefix方法未设置请求的路径

gev0vcfq  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(331)

我想在里面有html文件 webapp/WEB-INF/view 文件夹。因此,我创建了以下配置文件:

@Configuration
@EnableWebMvc
@ComponentScan({ "base.package.name" })
public class WebMvcConfig implements WebMvcConfigurer, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Bean
    public ViewResolver htmlViewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setContentType("text/html");
        resolver.setCharacterEncoding("UTF-8");
        resolver.setViewNames(new String[] {".html"});

        return resolver;
    }

    private ISpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(htmlTemplateResolver());

        return engine;
    }

    private ITemplateResolver htmlTemplateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setCacheable(false);
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setOrder(1);
        resolver.setCheckExistence(true);

        return resolver;
    }
}

用于测试的index.html位于 resources/templates/index.html 以及 webapp/WEB-INF/views/index.html .
当我拜访 localhost:8080 index.html是从 resources/templates 而不是 webapp/WEB-INF/** 我试图删除资源文件夹中的index.html,但收到filenotfound异常。
我做错了什么?

5lwkijsr

5lwkijsr1#

在中测试 application.properties 文件

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.html

相关问题