Java Spring框架的圆形视图路径异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35966441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Circular view path exception with Spring framework
提问by kenpeter
Basically, access "/" and "/a" are working on browser. Acess "/testme" is not working. Error is
基本上,访问“/”和“/a”正在浏览器上工作。访问“/testme”不起作用。错误是
2016-03-13 15:04:37.709 ERROR 1933 --- [io-8080-exec-57] o.s.boot.context.web.ErrorPageFilter : Forwarding to error page from request [/testmenull] due to exception [Circular view path [testme.html]: would dispatch back to the current handler URL [/XXXX/testme.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)]
2016-03-13 15:04:37.709 错误 1933 --- [io-8080-exec-57] osboot.context.web.ErrorPageFilter:由于异常 [循环视图路径 [ testme.html]:将再次调度回当前处理程序 URL [/XXXX/testme.html]。检查您的 ViewResolver 设置!(提示:由于默认视图名称生成,这可能是未指定视图的结果。)]
I do a string grep, but cannot found "ViewResolver". I have no idea why "/" and "/a" work. Any idea?
我做了一个字符串 grep,但找不到“ViewResolver”。我不知道为什么“/”和“/a”有效。任何的想法?
pom.xml
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxxxx</groupId>
<artifactId>Monitor</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<!--<name>Monitor</name>-->
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
<scope>provided</scope> <!--for web socket-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>Monitor</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>1.7</compilerVersion>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-source-plugin</artifactId>-->
<!--<executions>-->
<!--<execution>-->
<!--<id>attach-sources</id>-->
<!--<goals>-->
<!--<goal>jar</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
</plugins>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>${basedir}/src/main/java</directory>
</resource>
</resources>
</build>
</project>
web.xml
网页.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
回答by Boli-CS
the value of @RequestMapping is "testme" and the value of return is also "testme" so that when you access "/testme", the web will redirect to "/testme" again. This will lead to endless redirection to "/testme".
@RequestMapping 的值是“testme”,return 的值也是“testme”,这样当你访问“/testme”时,网页会再次重定向到“/testme”。这将导致无休止地重定向到“/testme”。
回答by MACCXpace
If you are using gradle you have to add:
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
, or maven find this dependency.
如果您使用的是 gradle,则必须添加:
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
,或者 maven 找到此依赖项。
回答by MACCXpace
I think that if all of the HTML names are correct, maybe you need to declare something like this in your MvcConfig
class:
我认为如果所有的 HTML 名称都是正确的,也许您需要在MvcConfig
类中声明如下内容:
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/usuario/perfil").setViewName("usuario/perfil");
registry.addViewController("/").setViewName("home");
registry.addViewController("/testme").setViewName("testme");
registry.addViewController("/test").setViewName("test");
registry.addViewController("/login").setViewName("login");
}
}
回答by User-8017771
If you are not returning a view. But only a JSON, adding @ResponseBody
to the response helps returning a JSON value rather than a view.
如果您没有返回视图。但只有 JSON,添加@ResponseBody
到响应有助于返回 JSON 值而不是视图。
Eg:
@RequestMapping(value="/getList",method = RequestMethod.GET)
public @ResponseBody List<LOne> getLOne(){
例如:
@RequestMapping(value="/getList",method = RequestMethod.GET)
public @ResponseBody List<LOne> getLOne(){
回答by Deepak Gupta
This issue comes due to circular dependencies mentioned in pom.xml. If you are using spring-boot-starter-freemaker then avoid using spring-boot-starter-web. spring-boot-starter-web is already part of spring-boot-starter-freemaker.
这个问题是由 pom.xml 中提到的循环依赖引起的。如果您使用的是 spring-boot-starter-freemaker,请避免使用 spring-boot-starter-web。spring-boot-starter-web 已经是 spring-boot-starter-freemaker 的一部分。
回答by ECM ECM
Tried all the above suggestions and it didn't work for me. But the issue was resolved when I changed the annotation on my controller class from @Controller to @RestController (which is a combination of @Controller and @ResponseBody).
尝试了上述所有建议,但对我不起作用。但是当我将控制器类上的注释从 @Controller 更改为 @RestController(@Controller 和 @ResponseBody 的组合)时,问题得到了解决。