开发环境:jdk1.7 + tomcat7 + Eclipse Juno
首先下载spring 相关jar包
我用的是 spring-framework-3.2.1.RELEASE
下载地址:http://www.springsource.org/download/community?project=Spring%2520Framework
这里需要注意的是jar都需要手动拷入到lib下面,否则tomcat部署会出错,用buildpath 加入jar只能确保开发的时候不会出编译错误。放入lib下面,是一劳永逸的好办法。
下载相关日志包
jcl-over-slf4j-1.6.1.jar
logback-classic-0.9.24.jar
logback-core-0.9.24.jar
slf4j-api-1.6.1.jar
下载地址:http://download.csdn.net/detail/jiujie395/5991615
在eclipse工程中建Dynamic Web project,向导式的开发,一路next,最后得到一个web工程。
这里需要注意的是:如果是Dynamic web module version 选择3.0 那么web.xml不会自动生成,需要自己创建。
在WebContent下生成一个index.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Spring 3.0 MVC demo</title>
</head>
<body>
<a href="hello.html">Say Hello World!</a>
</body>
</html>
在WebContent\WEB-INF目录下生成一个jsp文件夹和两个配置文件:spring-servlet.xml、web.xml
web.xml内容为
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Spring3MVC</display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这里需要注意的是:servlet-name的值就是 XX-servlet.xml的命名方式。如果需要改变则需要更改
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<?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.spring.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>
然后生成java文件HelloWorldController.java包路径为net.spring.controller
package net.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
System.out.println(message);
return new ModelAndView("hello", "message", message);
}
}
然后WEB-INF下新建jsp文件夹,然后在jsp文件夹下新建hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Spring 3.0 MVC demo</title>
</head>
<body>
${message }
</body>
</html>
在index.jsp上点击Say Hello,就会转向
http://localhost:8080/mvc_test/hello.html页面显示Hello World, Spring 3.0!,表示工作正常。
参考:http://www.open-open.com/lib/view/open1330670747561.html