【发布时间】:2021-11-07 02:22:15
【问题描述】:
我已经开始编写简单的 Spring Boot 应用程序,但遇到了一些问题。我有控制器 home.html 页面,显示 Hello Home Page。
这是错误:
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.createHandlerMethod(AbstractHandlerMethodMapping.java:341)
The following method did not exist:
'void org.springframework.web.method.HandlerMethod.<init>(java.lang.String, org.springframework.beans.factory.BeanFactory, org.springframework.context.MessageSource, java.lang.reflect.Method)'
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.method.HandlerMethod
Process finished with exit code 0
Pom.xml
只有 pom.xml 中的依赖代码
<project>
<dependencies>
<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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
主控制器
控制器包中的MainController代码
@Controller
public class MainController {
@GetMapping("/")
public String greeting(Model model) {
model.addAttribute("title", "Home Page");
return "home";
}
}
home.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${title}"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${title} + '!'" />
</body>
</html>
百万应用
默认方法来自
@SpringBootApplication
public class MillionApplication {
public static void main(String[] args) {
SpringApplication.run(MillionApplication.class, args);
}
}
有人遇到过类似的问题吗? 有人可以帮助我吗,因为我尝试了很多东西但没有任何效果
【问题讨论】:
-
你使用哪个 spring-boot 版本?
-
移除 spring-web 依赖,spring-boot-starter-web 已经包含了正确的版本。
标签: java html spring spring-boot spring-mvc