3

So I'm new to Spring and I've so far gotten a simple web API running connected to a MongoDB database, but I'm having trouble generating just plain old views using .jsp or .html files. I've tried a variety of different approaches: InternalResourceViewResolver, XmlViewResolver, returning Strings instead of ModelAndView objects, nothing seems to be working for me. I have the following code:

Edit: here is a git repo with my project: https://github.com/jwallp/Spring-Test

As the above project is, I am getting a white label error upon going to /index which says:

There was an unexpected error (type=Internal Server Error, status=500).
Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

Edit: So I managed to get the view to finally load by using spring.view.prefix and spring.view.suffix instead of spring.mvc.view.prefix and such, and by moving my WEB-INF directory from my project root to inside <project root>/src/main/webapp/. I just wanted to know, if my WEB-INF directory is contained within another directory, will it still function as intended (making its contents not directly visible)?

rawa
  • 315
  • 3
  • 8
  • 20
  • Using spring.view.prefix in application.properties with Eclipse STS , Spring boot 2.0.2, gives : "spring.view.prefix is an unkwown property" – DS. Oct 03 '18 at 12:52

10 Answers10

5

Spring Boot has limited support for JSP, because of its use of an embedded servlet container. From the Spring Boot reference documentation:

When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

With Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to a standard container (not limited to, but including Tomcat). An executable jar will not work because of a hard coded file pattern in Tomcat. Jetty does not currently work as an embedded container with JSPs.

Here is a basic example of using jsp in spring boot application.

mirmdasif
  • 6,014
  • 2
  • 22
  • 28
  • Oh well thank you for informing me of that, I didn't know. I am using war packaging now, but the problem still persists. I saw that example too, my project looks almost identical to it, yet the page still won't load. – rawa Aug 09 '15 at 18:45
  • 1
    Thanks a ton didn't know that, was struggling since yesterday, works just fine with war deployment – Sam2016 Aug 20 '17 at 11:20
3

Hope you have the JSP libraries in your classpath. If you are using maven, including the following dependencies in pom.xml will have those:

    <!-- For using JSP -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- If you want to use JSTL -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

Also, you may need to put this line at the top of the JSP file:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

Update

Doing the following changes to your project at GitHub worked in my environment:

  1. Move the WEB-INF folder into src/main/webapp. That's the place for it.
  2. In application.properties replace

    spring.mvc.view.prefix=/WEB-INF/pages/
    spring.mvc.view.suffix=.jsp
    

    with

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

    Seems the former will work with Spring Boot 1.3, but not not with the current stable release.

Sanjay
  • 8,755
  • 7
  • 46
  • 62
  • I had already included Jasper and JSTL, I hadn't used that line of code, and I tried including it on the first line of my `index.jsp`, still nothing. – rawa Aug 09 '15 at 03:56
  • Things look good to my eyes, then. Could be something silly - difficult to guess without looking at the complete source code. Did you try changing the packaging to war? – Sanjay Aug 09 '15 at 04:05
  • If you are still struggling with it, I can give it a shot if I you provide me the complete source code of a minimal sample project with the issue. You can upload it at GitHub or somewhere. – Sanjay Aug 09 '15 at 06:06
  • I have changed packaging to war and still no luck. Here is a link to my project: https://github.com/jwallp/Spring-Test – rawa Aug 09 '15 at 18:46
  • Oh! I now see your edited question that you had already fixed it. I should have seen that before giving my time into downloading and testing it in my env. Or, you could have put a comment here as soon as you did it. I don't think putting your WEB-INF directory elsewhere would work. There may be some way to configure it, thought. – Sanjay Aug 10 '15 at 00:55
0

Try the Following:

package hello;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.bind.annotation.RequestMapping;

    @Controller
    public class IndexController
    {
        @RequestMapping("/")
        public String index()
        {
            return "index";
        }
    }
  • I have done something similar to this (I simply used "/index" where you used "/") and that didn't work either. – rawa Aug 09 '15 at 03:55
0

We ran into this problem at work while upgrading an older application to Spring Boot. What we did:

  1. The prefix and suffix mappings as given above for application.properties (though our prefix was just /WEB-INF/).

  2. Moved our CSS, JavaScript, HTML files to a resources\static folder. We had subdirectories under that for each type.

  3. Places where window.open("somefile.jsp") was used, was changed to window.open("somevalue.do") where somevalue mapped to a @RequestMapping value and the setViewName for the ModelAndView of that method mapped to the previous jsp. Where there was a window.open("somefile.html") we changed it to map to window.open("includes/somefile.html") where includes is a subdirectory in our resources/static tree.

Murmel
  • 5,402
  • 47
  • 53
lilabnersgal
  • 198
  • 1
  • 4
  • 8
0

I ran into this problem few times. It was because I put /WEB-INF/ in /src/main/java folder. Latest I created separate path for the INF file in /src/main/webapp and I was able to run my application correctly and displayed the text in the browser.

0

Following jsp files relocation can solve the problem. I solved my problem this way:

Move the .jsp files to:

"src/main/resources/META-INF/resources/WEB-INF/jsp".

Make sure the application.properties file contains:

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

Your controller class should be like:

   @Controller
    public class IndexController
     {
         @RequestMapping("/")
         public String index()
          {
             return "index";
         }
      }

If you are using maven, include the following dependencies in pom.xml

<!-- For using JSP -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

<!-- If you want to use JSTL -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

It should then work.

Abdur Rahman
  • 1,420
  • 1
  • 21
  • 32
0

I added one line to my application.properties file.

server.tomcat.additional-tld-skip-patterns=hk2-utils.jar,javax.annotation-api.jar,javax.inject.jar,hk2-api.jar,config-types.jar,hk2-core.jar,hk2-config.jar,tiger-types.jar,validation-api.jar,jboss-logging.jar,classmate.jar,hk2-locator.jar,javassist.jar,hk2-runlevel.jar,class-model.jar,asm-all-repackaged.jar

It should then run.

Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43
0

I have used mvn spring-boot:run

It's worked for me

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Subha Chandra
  • 751
  • 9
  • 23
0

Eventhough I have tried many ways to fix this issue, couldn't find perfect solution.

If you are using Intellij IDEA : do not try to run Spring Boot application(with dynamic .jsp views) with the IDE's run ▶︎ button.

$ cd {PROJECT_FOLDER}

$ ls //Make sure that you are in the same folder where pom.xml resides
and then run below command

$ mvn spring-boot:run

Now your application should be served at localhost:8080.

Harsha
  • 1
  • 1
0

i have faced the same problem and my solution was replacing the

spring.view.prefix: /WEB-INF/pages/

spring.view.suffix: .jsp

with a configuration class like this. it worked for me!

package com.rbc.sample.user.login.sample;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;


@Configuration
public class WebConfig {


    public static final String RESOLVER_PREFIX= "/WEB_INF/jsp/";
    public static final String RESOLVER_SUFIX=".jsp";


    @Bean
    public ViewResolver viewResolver(){
        UrlBasedViewResolver viewResolver=new InternalResourceViewResolver();
        viewResolver.setPrefix(RESOLVER_PREFIX);
        viewResolver.setSuffix(RESOLVER_SUFIX);

        return viewResolver;
    }
}