Kumar Harsh
Kumar Harsh

Reputation: 453

JSP Page not rendering in SpringBoot Application

I have seen lot of same problem but My problem is not resolving with all those solutions. My JSP page is not rendering.

I have added all required jars like thymeleaf, tomcat, jstl to run the application but still getting error

Below is my controller

@RequestMapping("/rest/user")
public class BookResource {

@GetMapping("/recommend")
    public List<String> recommend() {
        return bookrepo.BookDetail(bookname);

    }
@RequestMapping(value = "/recommendation", method = RequestMethod.GET)
    public String recommend(Model model) {
        model.addAttribute("recommendations", bookrepo.BookDetail(bookname));
        model.addAttribute("name", "Harsh");
        return "recommendation";

    }
}

After i Hit the URL I am getiing below error:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue May 28 09:50:33 IST 2019
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template "recommendation", template might not exist or might not be accessible by any of the configured Template Resolvers

The JSP Page I have created not rendering, I put my jsp page inside resources/templates and also at WEB-INF/jsp/. And properties file is also updated

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springBootApp</groupId>
    <artifactId>FirstSpringBootApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</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-security</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.6</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap-datepicker</artifactId>
            <version>1.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>1.9.1</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

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

        <!-- Redis Dependencies -->

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

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

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

        <!-- <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency> -->

    </dependencies>

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

            <!-- GCP plugin -->
            <!-- <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>1.3.2</version>
            </plugin> -->
        </plugins>
    </build>

</project>

Structure .

Project Structure

Upvotes: 0

Views: 2904

Answers (4)

chandru
chandru

Reputation: 139

In 2021 version, You need to add only tomcat jasper dependency in pom.xml file to run the .jsp file in Spring boot application. Don't add jstl dependency, not highly required.

In application.properties file, you are required to add below code

spring.mvc.view.prefix=/folder name/
spring.mvc.view.suffix=.jsp

actually this is not required

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

because anyways tomcat server locate the jsp file.

It worked for me. 100% will work.

Upvotes: 0

Ananthapadmanabhan
Ananthapadmanabhan

Reputation: 6216

From your stacktrace it can be seen that springboot fails to resolve your template "recommendation", so it must have something to do with where you have kept your jsp files.

Keep your jsp templates under /WEB-INF/jsp/ since that's what you have provided in the application.properties. Also , ensure that the extension for your template is .jsp itself since you have specified that in property file as well.

Ensure that , in your pom you have the following dependencies added :

<!-- JSTL -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<!-- To compile JSP files -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

You can remove the thymeleaf dependency since you are using jsp as the templating engine here.

If that did not work, then you can try manually configuring your jsp view resolver like :

@Configuration
public class MvcConfiguration implements WebMvcConfigurer
{
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        registry.viewResolver(resolver);
    }
}

Upvotes: -1

Girish Kumar K
Girish Kumar K

Reputation: 135

As per spring boot there are Limitations when it comes to jsp's.

If packaging is not declared in pom.xml, spring boot by default considers it as jar.

To overcome these limitations we need to have the configuration made in the application to render jsp by placing the jsp's under src/main/resources/META-INF/resources/WEB-INF/jsp folder.

Sample Code: Click Here

References:

https://dzone.com/articles/spring-boot-with-jsps-in-executable-jars-1

https://github.com/hengyunabc/spring-boot-fat-jar-jsp-sample

Upvotes: 2

Andy Wilkinson
Andy Wilkinson

Reputation: 116231

Spring Boot does not support JSPs when using jar packaging. If you want to use JSPs then you must use war packaging. You can do that by adding the following to your pom.xml:

<packaging>war</packaging>

It's typically placed immediately after the line with the <version> tag.

Having made that change, you should tell Eclipse to update your Maven project by right-clicking FirstSpringBootApp and then selecting Maven -> Update Project….

Using war packaging will mean that src/main/webapp is recognised by Eclipse and your JSP files in src/main/webapp/WEB-INF/jsp should then be found. For further reference, Spring Boot has a sample project for JSPs that may be of interest.

Upvotes: 1

Related Questions