Spring boot downloading jsp file

后端 未结 4 2038
我寻月下人不归
我寻月下人不归 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:54

    You are missing the JEE dependency.

    
        javax
        javaee-web-api
        7.0
        provided
    
    

    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.

提交回复
热议问题