how to use both thymeleaf and jsp in same spring boot project
Asked Answered
T

1

8

I am able to access files under templates folder, But I am not able to access the jsp files. If I remove the thymeleaf dependency, I am able to access the jsp files, but I want access both thymeleaf html files and jsp files using spring boot.

Below are my configuration in application.properties and pom.xml

Spring view resolver set up

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

Below is the ERROR , When i try to access the jsp file

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "todo-form", template might not exist or might not be accessible by any of the configured Template Resolvers spring

My views folder structure

enter image description here

Tansy answered 26/12, 2021 at 4:48 Comment(2)
This could help: https://mcmap.net/q/540725/-use-jsp-and-thymeleaf-views-in-spring-boot-appHammerhead
This is for spring mvc and I need it for spring-bootTansy
T
8

Put below code in @configuration class. I can able to access both jsp and html files now.

@Autowired
WebApplicationContext webApplicationContext;

@Bean
public SpringResourceTemplateResolver thymeleafTemplateResolver(){
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setApplicationContext(webApplicationContext);
    templateResolver.setOrder(9);
    templateResolver.setPrefix("/WEB-INF/views/");
    templateResolver.setSuffix("");
    return templateResolver;
}

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

@Bean
public ThymeleafViewResolver thymeleafViewResolver(){
    final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setViewNames(new String[] {"*.html"});
    viewResolver.setExcludedViewNames(new String[] {"*.jsp"});
    viewResolver.setTemplateEngine(templateEngine());
    viewResolver.setCharacterEncoding("UTF-8");
    return viewResolver;
}

@Bean
public InternalResourceViewResolver jspViewResolver(){
    final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setOrder(10);
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix("");
    viewResolver.setViewNames("*.jsp");
    return viewResolver;
}

enter image description here

enter image description here

Tansy answered 26/12, 2021 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.