rawa
rawa

Reputation: 333

Spring MVC with Boot: "There was an unexpected error (type=Not Found, status=404)"

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)?

Upvotes: 3

Views: 31076

Answers (10)

zahra souri
zahra souri

Reputation: 46

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;
    }
}

Upvotes: 0

Harsha
Harsha

Reputation: 1

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.

Upvotes: 0

Subha Chandra
Subha Chandra

Reputation: 781

I have used mvn spring-boot:run

It's worked for me

Upvotes: 0

Nekoray
Nekoray

Reputation: 67

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.

Upvotes: 0

Abdur Rahman
Abdur Rahman

Reputation: 1666

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.

Upvotes: 0

keshab pandey
keshab pandey

Reputation: 1

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.

Upvotes: 0

lilabnersgal
lilabnersgal

Reputation: 306

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.

Upvotes: 0

Sanjay
Sanjay

Reputation: 8975

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.

Upvotes: 3

mirmdasif
mirmdasif

Reputation: 6354

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.

Upvotes: 5

Danny Varela
Danny Varela

Reputation: 74

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";
        }
    }

Upvotes: 0

Related Questions