我想在里面有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异常。
我做错了什么?
1条答案
按热度按时间5lwkijsr1#
在中测试
application.properties
文件