我想配置一个简单的控制器:
@Configuration
public class Config implements WebMvcConfigurer {
@Autowired
private final ApplicationContext applicationContext;
public Config(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
public SpringResourceTemplateResolver templateResolver(){
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine(){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
registry.viewResolver(resolver);
}
@Controller
@RequestMapping("/people")
public class ControllerPeople {
private final PersonDAO personDAO;
@Autowired
public ControllerPeople(PersonDAO personDAO) {
this.personDAO = personDAO;
}
@GetMapping("/test")
public String test(){
return "people/test";
}
@GetMapping()
public String index(Model model){
model.addAttribute("people", personDAO.index());
return "people/index";
}
@GetMapping("/{id}")
public String show(@PathVariable("id") int id, Model model){
model.addAttribute("person", personDAO.getPeople());
return "people/show";
}
}
当应用程序启动时,我收到此消息:
2024-03-03T15:40:59.935+03:00 WARN 9616 --- [ main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)
当我尝试移动到本地主机 URL 时,我收到此异常
org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“ServletContext资源[/WEB-INF/views/people/index.html]”) 在 org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:235) ~[thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE] 在 org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) ~[thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE] 在 org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:649) ~[thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE] 在 org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1103) ~[thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE] 在 org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1077) ~[thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE] 在 org.thymeleaf.spring6.view.ThymeleafView.renderFragment(ThymeleafView.java:372) ~[thymeleaf-spring6-3.1.2.RELEASE.jar:3.1.2.RELEASE] 在 org.thymeleaf.spring6.view.ThymeleafView.render(ThymeleafView.java:192) ~[thymeleaf-spring6-3.1.2.RELEASE.jar:3.1.2.RELEASE]
如果我在配置中的这行中理解正确,我会在发现我的观点或问题不在这里时显示
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
我的观点是为了以防万一。
我试图创建另一个测试视图并将视图移动到
resources/templates
。