我在 2 個月前開始了我的第一份作業,自從我處理 Spring 以來已經有一段時間了。我正在嘗試運行 Tomcat 服務器,并顯示“/home”,但我得到了 404。當我將滑鼠懸停在“家”時,IntelliJ IDEA 正在顯示home.html
. 錯誤
HTTP Status 404 – Not Found
Type Status Report
Message The requested resource [/home] is not available
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.65
HTTP 錯誤
代碼檔案夾截圖
Tomcat 配置
Tomcat 部署配置
我已經用谷歌搜索了它,但它并沒有解決它。這是我的代碼。
檔案HomeController.java
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/home")
public String landingPage() {
return ("home");
}
}
檔案home.html
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Landing page</h1>
</body>
</html>
檔案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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
我也嘗試運行我的舊專案,但現在不行了。
先感謝您!
uj5u.com熱心網友回復:
嘗試使用ModelAndView
:
@RequestMapping("/")
public ModelAndView index () {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("home");
return modelAndView;
}
uj5u.com熱心網友回復:
您正在使用具有嵌入式 Tomcat 服務器的 Spring Boot。我相信你有一個獨立的 Tomcat 在你的機器上的 8080 埠上運行,防止 Spring Boot 使用它自己的嵌入式 Tomcat。
停止獨立的 Tomcat 并再次啟動 Spring Boot 應用程式。
uj5u.com熱心網友回復:
一切看起來都很好。Spring Boot 不需要您進行任何 tomcat 配置。
您的配置應該是問題所在。重新創建沒有湯姆貓配置和部署配置的專案。
觀看此視頻:https ://www.youtube.com/watch?v=HYGnVeCs0Yg&t=4235s
uj5u.com熱心網友回復:
正如之前提到的之前的答案,您使用的是 Spring Boot,它在后臺具有嵌入式 Tomcat 服務器。這是默認行為,但您可以將 Spring Boot 專案配置為使用 Netty 或 Undertow 服務器,請在此處查看更多詳細資訊 — Embedded Web Servers
因此,由于您已經擁有 Tomcat 服務器,因此無需在 IDE 中對其進行配置。此外,值得一提的是,從 IDE 運行專案并不是最好的主意。尤其是當您描述將問題重現給其他人的步驟時。相反,您可以使用以下命令從命令列運行您的 Spring Boot 專案:
mvn spring-boot:run
默認情況下,它將在 8080 埠上啟動您的應用程式,您將能夠通過以下 URL 訪問您的主頁:localhost:8080/home
有關運行 Spring Boot 應用程式的更多詳細資訊,您可以查看此檔案 —運行您的應用程式
要從 IntelliJ IDEA 運行 Spring Boot 專案,您需要打開 DemoApplication 類(在您的情況下),它具有 main 方法。在左側,應該有一個運行專案的播放按鈕。您還可以通過選擇“添加新配置”選單下的“Spring Boot”項在“編輯配置”選單中對其進行配置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/504082.html