gpt4 book ai didi

java - 在 Eclipse 中运行 spring MVC 应用程序时出现 HTTP-404 错误 - Tutorials point hello world example

转载 作者:行者123 更新时间:2023-11-30 10:59:08 24 4
gpt4 key购买 nike

我是 Spring 的初学者,我正在从教程点学习,下面的代码与教程点中的完全一样,但是当我尝试运行它时出现 404 错误,当我运行时代码是 hello world 程序使用 tomcat 服务器的 eclipse 我收到这样的错误

我在浏览器上使用的 url 是 http://localhost:8080/HelloWeb/hello

HelloController.java:

package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{

@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");

return "hello";
}

}

HelloWeb-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.tutorialspoint" />

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

</beans>

web.xml:

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

你好.jsp:

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>

这是我的目录结构

HelloWeb 项目:

|--HelloController.java
|-- WebContent
`-- WEB-INF
|-- jsp
|-- web.xml

最佳答案

http://howtodoinjava.com/2015/02/05/spring-mvc-hello-world-example/#hello_world

按照上面的 URL,它完全可以工作我已经尝试并确保你已经在 pom.xml 中添加了 spring 的依赖项。同样在您的 webapp 文件夹中,HelloWeb-servlet.xml 也应该存在。

关于java - 在 Eclipse 中运行 spring MVC 应用程序时出现 HTTP-404 错误 - Tutorials point hello world example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32003706/

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