gpt4 book ai didi

java - 如何使用 viewResolvers 显示 html 和 jsp 文件

转载 作者:行者123 更新时间:2023-11-30 08:10:22 26 4
gpt4 key购买 nike

我正在尝试使我的项目可以显示 thymeleaf html 页面和 jsp,但是使用下面的代码似乎只显示我的 jspWebMvc配置:

@Bean
public TemplateResolver templateResolver() {
TemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setOrder(0);
templateResolver.setPrefix(VIEWS);
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCacheable(false);
return templateResolver;
}

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

@Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setTemplateEngine(templateEngine());
thymeleafViewResolver.setCharacterEncoding("UTF-8");
return thymeleafViewResolver;
}

@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setOrder(1);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}

Page1Controller(显示jsp):

@Controller
public class Page1Controller {
@RequestMapping("/page1")
public String Page1() {
return "page1/page1";
}
}

SigninController(不显示 html):

@Controller
public class SigninController {

@RequestMapping(value = "signin")
public String signin() {
return "signin/signin";
}
}

最佳答案

Use multiple view revolvers with order value..

/* Thymeleaf configuration */
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setPrefix("/WEB-INF/views/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode("HTML5");
templateResolver.setOrder(1);
return templateResolver;
}

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

@Bean
public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine templateEngine){
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver(); thymeleafViewResolver.setViewNames(new String[]{"thymeleaf/*"}); thymeleafViewResolver.setTemplateEngine(templateEngine);
return thymeleafViewResolver;
}

/* JSP view resolver */

@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;

}

关于java - 如何使用 viewResolvers 显示 html 和 jsp 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30479822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com