gpt4 book ai didi

java - Tomcat 找不到 Spring Controller

转载 作者:行者123 更新时间:2023-11-28 23:25:30 31 4
gpt4 key购买 nike

我正在尝试使用 SpringMVC 建立一个非常小的测试项目。我已经设法让它运行在目标文件夹中创建的 jar 文件,如 this tutorial 中所述。 .但是,我无法通过 IntelliJ IDEA 配置部署 war 文件。

应用:

@SpringBootApplication
public class Application {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}

你好 Controller

@Controller
public class HelloController {
@RequestMapping("/greeting")
public String greeting(final Model model) {
model.addAttribute("name", "bla");
return "greeting";
}
}

问候语.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'"/>
</body>
</html>

运行配置: Run configuration

工件:

Artifact结构和目标输出

Structure and target output

我在转到“localhost:8080/greeting”时收到 404。

如果有人能指出我做错了什么,我会很高兴。

最佳答案

正如我所见,您将 Spring 作为独立应用程序启动

public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}

Tomcat 只能与 servlet 应用程序一起工作。像往常一样启动您的 spring 应用程序控制台应用程序(在 main() 方法的 Idea 中右键单击并选择“运行”菜单项)。然后在浏览器中请求http://localhost:8080/greeting

spring-boot example

您根本不需要 IDEA 中的工件来启动应用程序。

因此,如果您想在 Tomcat 等 servlet 容器中将 Spring boot 作为 war 运行,请阅读 this doc

关于java - Tomcat 找不到 Spring Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37836034/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com