gpt4 book ai didi

spring-mvc - 尽管我在我的 hello-world Spring mvc 项目中没有发现任何错误,但总是得到 404

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

我已经设置了一个非常简单的 Spring MVC hello-world 类型的项目。但是当我在 tomcat 上运行它时,我总是得到 404。我花了几个小时在上面我不知道我哪里做错了。

我正在使用 Spring Tool Suite 4 和 tomcat v9。

我的 web.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>
<servlet>
<servlet-name>fitServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>fitServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

</web-app>

我的 servlet-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<mvc:annotation-driven/>
<context:component-scan base-package="com.xi.controller"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"/>

</beans>

我的 Controller :

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

@RequestMapping("/greeting")
public String sayHello(Model model) {
model.addAttribute("greeting", "Hello World!");
return "hello";
}

}

我的hello.jsp位于/WEB-INF/jsp/hello.jsp下

<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Fit tracking</title>
</head>
<%@ page import="java.util.Date" %>
<body>
<h1>${greeting}</h1><br>
<strong>Current Time is</strong>: <%=new Date() %>

</body>
</html>

WEB-INF的文件夹结构:

enter image description here

比如我部署到tomcat服务器后,在浏览器输入url

http://localhost:8080/FitTracker/greeting.html

我得到这个页面: enter image description here

我不明白为什么。我应该看到 JSP 页面...

最佳答案

你没有在任何地方定义 greeting.html

关于spring-mvc - 尽管我在我的 hello-world Spring mvc 项目中没有发现任何错误,但总是得到 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59229014/

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