4

I'm trying to build a Spring MVC project and am having some troubles trying to resolve the follow error.

HTTP Status 500 - Circular view path [login]: would dispatch back to the current handler URL [/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

I entered the following commands via Terminal:

mvn clean package
java -jar target/sprint2-0.1.0.jar

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.teamvirus.src</groupId>
    <artifactId>sprint2</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>0.5.0.M6</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--><dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring3</artifactId>
        </dependency>-->

    </dependencies>

    <properties>
        <start-class>com.teamvirus.src.Application</start-class>
    </properties>

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

    <repositories>
        <repository>
            <id>spring-milestone</id>
            <url>http://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestone</id>
            <url>http://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

LoginController.java

package com.teamvirus.src;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/authenticate")
    public String authenticate(
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "password", required = true) String password) {
        if ((username.equals("admin") && password.equals("admin")))
            return "redirect:dashboard?username=" + username;
        else if ((username.equals("student1") && password.equals("student1"))) {
            return "redirect:dashboard?username=" + username;
        }
        return "wrongpassword";
    }
}

Appreciate any assistance rendered.

Edit:

Application.java

package com.teamvirus.src;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Kuan E.
  • 373
  • 1
  • 3
  • 15

2 Answers2

5

You are entirely relying on Spring Boot to configure everything, although this shouldn't be a problem for your current view it is.

By default Spring boot registers an InternalResourceViewResolver however without any prefix/suffixes. (See the source). Assuming you have your views in /WEB-INF/views and they are jsp files do the following

  1. In src\main\resources add an application.properties file
  2. Add a property spring.view.prefix with the value /WEB-INF/views/
  3. Add a property spring.view.suffix with the value `.jsp'

Repackage and start your application.

If you don't have this additional configuration /login will lead back to /login which will lead back to /login which will lead back to /login which will... Well you get the picture I guess. (login is the name of the view you are referring to from your @Controller).

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Thanks. I've followed your advice and `application.properties` file with the following: `@Value("${spring.view.prefix:}") private String prefix = "/WEB-INF/views/"; @Value("${spring.view.suffix:}") private String prefix = ".jsp";` Repackaged, and restarted my application but still get the same error as earlier. Am I doing this correctly? – Kuan E. Dec 09 '13 at 09:54
  • Why did you add that to the properties file? That is java not plain properties. You should have something like `spring.view.prefix=/WEB-INF/views/` not the stuff you added. – M. Deinum Dec 09 '13 at 09:56
  • My apologies, I'm still learning. That appears to solve the Circular View Path error message. However, none of my JSP files are showing even after cleaning, packaging and running. – Kuan E. Dec 09 '13 at 10:06
  • See http://stackoverflow.com/questions/20199609/spring-boot-not-finding-jsp-pages-in-war-file. You also might want to check the sample application. To get some more insight in what is happening enable debug/trace logging and see what happens when your pages get resolved. – M. Deinum Dec 09 '13 at 10:10
  • Thank you for your help. I appreciate it. I will read and look into this. I've marked your post as the accepted answer. – Kuan E. Dec 09 '13 at 10:12
0

Do you have a view resolver? Try to add

<bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

so when you return "login" it will search and load the jsp inside /WEB-INF/pages/ with name login.jsp. Without view resolver it will call your controller method again.

Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
  • I've the following in servlet-context.xml. My pages are in 'views'. – Kuan E. Dec 09 '13 at 09:34
  • Your `servlet-context.xml` isn't going to be read, so basically all the configuration in there is ignored. Move it to either java configuration or see what is already provided for you by Spring Boot and configure accordingly. – M. Deinum Dec 09 '13 at 12:05