Java SpringMVC显示HTTP状态404
这是错误消息:- 这是我的项目结构:- 以下是我的代码:- HelloController.javaJava SpringMVC显示HTTP状态404,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,这是错误消息:- 这是我的项目结构:- 以下是我的代码:- HelloController.java package com.home.pack; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod
package com.home.pack;
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";
}
}
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>
<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.home.pack" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
SpringMVC应用程序
你好
org.springframework.web.servlet.DispatcherServlet
1.
你好
/
HelloWeb servlet.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>
<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.home.pack" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
hello.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
你好,世界
${message}
我正在遵循本教程->但我无法让它工作。似乎我在这里遗漏了一些东西,如果有人指出这一点,我将不胜感激。谢谢。你想显示“你好”页面?Spring可能将其视为控制器,因为您执行了
@RequestMapping(“/hello”)
。。。所以我猜这里你试图将控制器显示为一个页面,这肯定会显示404错误
尝试将您的方法更改为“休耕”:
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
并删除控制器上的请求映射
我想这样会更好
或者让请求映射在您的控制器上,并为其他内容更改方法的请求映射,然后通过添加…/hello/yourMethodValue来访问它我对spring mvc一无所知,但404表示您的url不正确(找不到资源)。您是否检查了web.xml和绑定/hello的逻辑。HelloWebServlet与您的HelloController有何关联?只需提供特定方法的url,就像您刚才在@RequestMapping(method=RequestMethod.GET)中提供的方法一样。。。在这里提供价值,并将其添加到您的URL@NiravChhatrola:但我已经给出了类级别的url。请详细说明。@ParagKadam转到服务器的选项卡>右键单击tomcat>添加和删除>确保添加了SpringTest>删除其他服务器。重新启动服务器。不,这也没用。问题是,为什么上面的代码不运行。如何以另一种方式运行是另一件事,我看到了,我的错误。