0

I know that this question duplicates these questions.

https://techhelpnotes.com/java-how-to-use-both-thymeleaf-and-jsp-in-same-spring-boot-project/

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

https://stackoom.com/en/question/2hMVq

But none of these sources helped me.

Following one of the previous guides, I added a configuration class. After that, the controller began to see the files, but about calling a file with the extension from the browser .jsp error appears.

here is the project code:

ViewConfiguration.java

@EnableWebMvc
public class ViewConfiguration implements WebMvcConfigurer {
 @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;
 }
}

MyController.java

@Controller
public class MyController {
 

    @GetMapping("/1")
    public String jsp() {
        return "jj.jsp";
    }

    @GetMapping("/2")
    public String html() {
        return "hh.html";
    }
}

JspAndThymeleafApplication.java

@SpringBootApplication
public class JspAndThymeleafApplication {

    public static void main(String[] args) {
        SpringApplication.run(JspAndThymeleafApplication.class, args);
    }

}

ServletInitializer.java

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(JspAndThymeleafApplication.class);
    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>jspAndThymeleaf</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>jspAndThymeleaf</name>
    <description>jspAndThymeleaf</description>
    <properties>
        <java.version>11</java.version>
    </properties>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jakarta.servlet.jsp.jstl</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

project structure:

enter image description here

When calling http://localhost:8090/1

In the files.jj.jsp and hh.html simple hello world projects

Alex
  • 29
  • 4

0 Answers0