Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP file not rendering in Spring Boot web application

Tags:

spring-boot

I have a Spring Boot web application up and running using embedded Tomcat (the default). When it serves up JSP files as part of rendering the view I specified in my controller, the JSPs are not being rendered as such, and instead print out the contents. For example:

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html lang="en">     <head></head>     <body>Test</body> </html> 

When the view is rendered in the browsers, the contents above are displayed, instead of the expected contents:

Test 
like image 790
Nick Spacek Avatar asked Dec 16 '13 01:12

Nick Spacek


People also ask

Where do JSP files go in spring boot project?

As per convention, we place our JSP files in the ${project. basedir}/main/webapp/WEB-INF/jsp/ directory.

Can we use JSP with spring boot?

Project Dependencies We want to use JSP as the view. Default embedded servlet container for Spring Boot Starter Web is tomcat. To enable support for JSP's, we would need to add a dependency on tomcat-embed-jasper.

Where does JSP file go in Web application?

In order to deploy Java Server Pages (JSP) files, you must place them in the root (or in a subdirectory below the root) of a Web application.

Can JSP deployed in web server?

Because JSPs are part of the J2EE standard, you can deploy JSPs on a variety of platforms, including WebLogic Server.


2 Answers

Make sure that your pom.xml specifies the Tomcat JSP dependency as follows:

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

It seems that embedded Tomcat treats the JSP rendering as optional.

As mentioned below, this JAR is sometimes necessary as well:

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

(I added provided since this JAR should be included by the servlet container.

like image 144
Nick Spacek Avatar answered Sep 28 '22 05:09

Nick Spacek


You will need not one but two dependencies (jasper and jstl) in your pom.xml for this to work.

   <dependencies>     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-web</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> </dependencies> 
like image 45
Sezin Karli Avatar answered Sep 28 '22 04:09

Sezin Karli