Spring MVC控制器配置为thymeleaf视图



我已经尝试配置控制器的thymeleaf视图解析器,但它不工作。我做了控制器测试,他们通过了,所以我认为这是servlet配置问题。

我的WebConfig是这样的:

@Configuration
@EnableWebMvc
@ComponentScan("springmvccommerce.web")
public class WebConfig implements WebMvcConfigurer{

@Bean
ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}

@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(true);
return templateResolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}


public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

}

编辑:

HomeController:

@Controller
public class HomeController {
@GetMapping("/")
public String home() {
return "home";
}
}

和productController:

@Controller
public class ProductController {
@Autowired
private ProductRepository productRepository;
@GetMapping("/products")
public String products(Model model) {
model.addAttribute("productList", productRepository.findProduct(Long.MAX_VALUE, 20));
return "products";
}
}

如果还不够的话,我已经在注释中添加了repo的链接。

由于jakarta命名空间的原因,spring boot似乎不支持tomcat 10,请切换到tomcat 9然后重试。

以下是一些相关链接:

  1. https://github.com/spring-projects/spring-framework/issues/25354
  2. 部署Spring在Tomcat 10上安装。X在雅加达。*包

相关内容

  • 没有找到相关文章

最新更新