我遇到一个问题,我尝试将
WebController
与 Thymeleaf 模板连接,并在尝试访问页面 http://localhost:8080/home
时收到此错误:
循环视图路径 [home]:将再次分派回当前处理程序 URL [/home]
还有
@Controller
和@GetMapping("/home")
,它说没有用法。我有
~/../src/main/java/com/example/test/WebController.java
package com.example.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class WebController {
@GetMapping("/home")
public String home(){
return "home";
}
}
~/../src/main/resources/templates/home.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome!</h1>
</body>
</html>
~/../src/main/java/com/example/test/ApiController.java
package com.example.test;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@GetMapping("/")
public String index(){
return "Greetings!";
}
}
~/../src/main/java/com/example/test/Application.java
package com.example.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
~/../src/main/java/com/example/securingweb/WebMvcConfigurer.java
package com.example.securingweb;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class MvcConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/home").setViewName("home");
}
}
~/../pom.xml
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
...
~/../src/main/resources/application.properties
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
我尝试过的
WebController
与@SpringBootApplication
类或子包位于同一个包中。@ComponentScan
显式包含包含 WebController
的包。/home
路径是否正常工作。我所期待的
我希望成功访问
/home
路径并看到 WebController
渲染的 home.html 视图。
到底发生了什么
循环视图路径 [home]:将再次分派回当前处理程序 URL [/home]。
问题是
MvcConfig.java
和 WebController.java
类存在冲突,因为您只需要其中之一。这就是为什么出现这个错误的原因
循环视图路径[home]:将分派回当前处理程序 再次网址[/home]
你的结构也是这样的:
src/main/java
└── com
└── example
├── test
│ ├── Application.java //@SpringBootApplication
│ ├── ApiController.java
│ └── WebController.java
│
└── securingweb
└── MvcConfig.java
但实际上你应该有这样的东西:
src/main/java
└── com
└── example
├── Application.java //@SpringBootApplication
├── test
│ ├── ApiController.java
│ └── WebController.java
│
└── securingweb
└── MvcConfig.java
这是因为Spring Boot(Application.java)需要位于同一个包中或者可以访问子包。