【发布时间】:2014-08-31 16:47:40
【问题描述】:
我刚刚开始使用 Spring MVC 框架。我正在尝试做helloworld 示例,但我检查的每个教程都有一些我不知道这意味着什么的问题!
例如,当我单击 sayhello 时,该项目出现 404 错误:
我使用 eclipse Kepler 和 tomcat 7.0 和 Spring 4.0 ;
在构建路径中添加的所有 Spring .jar 文件。
控制器:
package net.spring3.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping(value = "/hello")
public ModelAndView helloWorld() {
String message = "Hello World";
return new ModelAndView("hello", "message", message);
}
}
spring-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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="net.spring3.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
WEB-INF/jsp/hello.jsp:
<html>
<head>
<title>Spring</title>
</head>
<body>${message}
</body>
</html>
index.jsp:
<html>
<head>
<title></title>
</head>
<body>
<a href="hello.html">Say Hello</a>
</body>
</html>
【问题讨论】:
标签: java eclipse spring jsp spring-mvc