JSP file not showing up in Spring Boot web application

I have a Spring boot web application and it starts using the built-in Tomcat (by default). When it serves the JSP files as part of the rendering of the view specified in my controller, the JSPs are not displayed as such and instead print 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 displayed in browsers, the content above is displayed, not the expected content:

 Test 
+55
spring boot
Dec 16 '13 at 1:13
source share
17 answers

Make sure your pom.xml indicates the JSP dependency on Tomcat as follows:

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

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

As mentioned below, this JAR is sometimes also needed:

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

(I added, since this JAR should be included in the servlet container.

+95
Dec 16 '13 at 1:13
source share

For this you need not one, but two dependencies (jasper and jstl) in your pom.xml .

  <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> 
+21
Mar 21 '14 at 7:35
source share

I worked the same way Dan said. Removing the area provided .

 <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> 

Thank you man!

+14
Nov 08 '16 at 11:43
source share

Worked for me too, but I had to remove

 <scope>provided</scope> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> 
+12
Sep 23 '16 at 21:31
source share

Better you can use gradle (which catches up with Maven). Use this dependency in the build.gradle file.

// Required Dependency For JSP

 providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper' 
+10
Oct. 29 '14 at 8:48
source share

I think you missed some kind of configuration because it is easy to integrate JSP, just follow the steps below.

1 - dependency on tomcat-embad-jasper

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

2 - Add below configuration - application.properties

 spring.mvc.view.prefix: / spring.mvc.view.suffix: .jsp 

That he still has some doubts, then check him out below the link

Spring Download and JSP Integration

+6
Apr 29 '17 at 18:22
source share

I solved my problem when in addition to the previously described:

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

added ViewResolver:

 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @ComponentScan @EnableWebMvc public class SpringServletConfig { @Bean public InternalResourceViewResolver resolver() { InternalResourceViewResolver vr = new InternalResourceViewResolver(); vr.setPrefix("/WEB-INF/jsps/"); vr.setSuffix(".jsp"); return vr; } } 

from: Why does Spring MVC respond with 404 and say "No mapping was found for the HTTP request with the URI [...] in the DispatcherServlet"?

+3
Mar 14 '17 at 12:08
source share

Just change the dependency

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

to

  <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> 
+2
Apr 30 '17 at 10:18
source share

Full gradle configuration for Spring-Boot using Spring-MVC and with the integrated Tomcat server:

build.gradle

 buildscript { ext { springBootVersion = '1.5.8.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'org.springframework.boot' //WEB-MVC compile 'org.springframework.boot:spring-boot-starter-web:1.5.8.RELEASE' compile 'org.apache.tomcat.embed:tomcat-embed-jasper:9.0.1' compile 'javax.servlet:jstl:1.2' 

App.class

 @SpringBootApplication public final class App { public static void main(final String[] args) { new SpringApplicationBuilder(App.class) .build(args) .run(); } @Bean public ViewResolver viewResolver() { final InternalResourceViewResolver r = new InternalResourceViewResolver(); r.setPrefix("/WEB-INF/jsp/"); r.setSuffix(".jsp"); return r; } } 
+1
Nov 18 '17 at 22:48
source share

Sometimes tomcat-embed-jasper is not available, so you need to remove the tomcat-embed-jasper dependency provided from maven.

eg.

 <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <!--scope>provided</scope--> </dependency> 
+1
Nov 20 '17 at 7:39 on
source share

If you want to use 1.5.8.RELEASE or similar, then the above example and its explanation are here https://www.surasint.com/spring-boot-jsp/

You just need it in pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd "> 4.0.0 org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE

 <groupId>com.surasint.example</groupId> <artifactId>spring-boot-02</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- JSP --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- jstl for jsp --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> 

And this is in application.properties

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

Then you save your jsp in the folder / WEB -INF / jsp /.

This is the controller.

 package com.surasint.example.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import java.util.Date; import java.util.Map; @Controller public class TestController { @GetMapping("/testjsp") public String list(Map<String, Object> model) { model.put("this_time",new Date().toString()); return "testjsp-view"; } } 
+1
Dec 13 '17 at 20:26
source share

The reason is because you use the @RestController annotation instead of @Controller

When you annotate a class using RestController, all methods annotated with @RequestMapping accept the default semantics of @ResponseBody. In other words, your #index method serializes String / webapp / WEB-INF / index.jsp as JSON, rather than displaying its value in a view.

As mentioned in one of the answers, it should be

@Controller public class YourController {...}

+1
Jan 06 '19 at 11:38
source share

I ran into a problem, for example, typing the jsp file name in the browser instead of its contents.

By adding the snippet below to render the jsp page in pom.xml, it displays correctly.

 <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> 
0
Apr 21 '17 at 9:57 on
source share
 <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> I took the * off so it was just from web.xml <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 
0
Mar 28 '18 at 10:22
source share

I had this problem and finally it was solved!

My problem was that I had /WEB-INF/index.jsp JSP code on my page /WEB-INF/index.jsp . However, this page is served directly, without processing by any servlet or controller. Therefore, he had no chance of being compiled.

enter image description here

My decision:

  1. Move index.jsp to a subfolder named views .

    enter image description here

  2. Edit web.xml so that it transfers root control to the dispatcher servlet.

     <!-- WEB-INF/web.xml --> <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <display-name>18-655_lab_1</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> 
  3. Edit dispatcher-servlet.xml to make sure it looks in the views directory for files ending in .jsp .

     <!-- WEB-INF/dispatcher-servlet.xml --> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.app.controller" /> <mvc:default-servlet-handler /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/views/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> 
  4. Using the same base-package path that is specified in dispatcher-servlet.xml , create a controller that will return a ModelAndView.

    enter image description here

     package com.app.controller; @Controller @RequestMapping(value = "/") public class RootController { @RequestMapping(method = RequestMethod.GET) public ModelAndView homeGet() { return new ModelAndView("index", "message", "IT WORKS!!!"); } } 
  5. The syntax new ModelAndView("index", "message", "IT WORKS!!!") means that
    • dispatcher-servlet searches for a file named "/WEB-INF/views/" + "index" + ".jsp" .
    • It compiles the jsp file, replacing all instances of ${message} with IT WORKS!!! ,

So the last thing to do is put ${message} somewhere in our index.jsp file.

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <h1>${message}</h1> </body> </html> 
  1. Upload the project to the Tomcat server, run it and go to http://localhost:8080/[project name]/ .

    enter image description here

0
Jan 31 '19 at
source share

For me, with Spring Boot version 1.5.10.RELEASE, it worked on adding below maven dependencies.

Note: it worked only on not providing a <scope> for the two.

 <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <!--<scope>provided</scope>--> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <!--<scope>provided</scope>--> </dependency> 

And the below configuration in application.properties file

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

0
Jul 29 '19 at 10:48
source share

Please note that there are some limitations to the JSP. You cannot install packaging your pom on jar . Read This JSP Limitations

0
Aug 18 '19 at 11:33
source share



All Articles