Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot downloading jsp file

I am using spring boot simple application for displaying a JSP. However instead of rendering the JSP the page gets downloaded in the browser. Please suggest?

application.properties

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tcadmin?zeroDateTimeBehavior=convertToNull
spring.datasource.username=tcuser
spring.datasource.password=tcuserpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=9090
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
server.context-path=/internal
spring.mvc.view.prefix:/
spring.mvc.view.suffix:.jsp

I have made the folder src/main/webapp and put printReciept.jsp in that.

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.a2b</groupId>
    <artifactId>A2BInternalModules</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>A2BInternalModules</name>
    <description>Sitemap Generator for A2B</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.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>
    <!-- Tomcat Embed -->

        <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>required</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
       </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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


</project>

PrintRecieptController.java

@RequestMapping(value = "/PrinReciept", method = RequestMethod.GET)
    public String welcome(Model model) {
        model.addAttribute("message", "Hello World !!!");
        return "printReciept";
    }

printReciept.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Print Receipt
</body>
</html>

The controller is getting hit but instead of rendering the JSP it is downloading that page in browser.Please suggest.

like image 598
Jeets Avatar asked Nov 06 '17 13:11

Jeets


3 Answers

Within Maven dependencies check the tomcat-embed version and find the relevant tomcat-jasper version dependency in the maven central. This jar is required for JSP compilation. Since it is not compiled it comes as a downloadable.

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>9.0.19</version>
    </dependency>

My embed tomcat version was 9.0.19, and I got the jasper relevant for that version.

like image 193
Akalanka Avatar answered Sep 19 '22 15:09

Akalanka


I Got same error but i am not putting tomcat jasper who is responsible for converting our jsp files,

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>9.0.16</version>
    </dependency>

Just add tomcat-jasper according to you spring boot using tomcat

like image 43
Bhargav Suthar Avatar answered Sep 21 '22 15:09

Bhargav Suthar


You are missing the JEE dependency.

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

Also, I highly suggest that you put all your JSP into your WEB-INF folder (this is true for any templating engine) and choose a prefix other than root. It's just more secure and more flexible if you also want to have some RESTlike endpoints served from the same application.

You can also extend the WebMvcConfigurerAdapter and override applicable methods.

// Add the JSP view resolver.
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
    registry.jsp();
    // OR
    registry.jsp("/",".jsp");
}
//... snip
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry
        .addViewController("/yourpath")
        .setViewName("yourtemplate");
}

"addViewControllers" is nice to use so you don't have to create a controller for each generic JSP and partial. Notice that I did not add ".jsp" to the view name.

You can use the root context as the prefix and still use the above configuration.

like image 25
Pytry Avatar answered Sep 21 '22 15:09

Pytry