【发布时间】:2016-05-24 06:20:28
【问题描述】:
运行以下代码时出现 404 错误,请帮助。这里一切正常,但我仍然收到 404 错误。请帮我解决这个问题。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Hello Spring</display-name>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app><br/>
你好-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 basepackage="com.hello"/>
<bean class="org.springframework.web.servlet.view.InternalResouceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans></br>
你好控制器
@Controller
@RequestMapping
public class HelloController {
@RequestMapping("/hello")
public ModelAndView getHello()
{
return new ModelAndView("hellopage","hellomessage","Welcome Spring MVC!!!");
}
}</br>
你好页面
<html>
<body>
<h1>${hellomessage}</h1>
</body>
</html>
【问题讨论】:
-
你用的是哪个版本的spring jar??
-
你的项目名和控制器类包名是什么??
-
在 Web 上找到 tutorialsdesk.com/2016/01/… 示例,用于 Spring MVC Hello World 示例
-
HelloMVC 是我的项目名称 & HelloController 是控制器,com.hello 是我的包。4.0.6 是我的 jar 版本
-
所以你的 URL 应该是 HelloMVC/hello,你是不是也一样?
标签: java spring model-view-controller