Spring boot downloading jsp file

后端 未结 4 2009
我寻月下人不归
我寻月下人不归 2021-01-18 04:05

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?

相关标签:
4条回答
  • 2021-01-18 04:44

    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.

    0 讨论(0)
  • 2021-01-18 04:48

    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

    0 讨论(0)
  • 2021-01-18 04:49

    If you are using Gradle, add the below dependency

    compile('org.apache.tomcat.embed:tomcat-embed-jasper')
    
    0 讨论(0)
  • 2021-01-18 04:54

    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.

    0 讨论(0)
提交回复
热议问题