demo下载:http://download.csdn.net/detail/qyf_5445/4788204
1.事先准备好所需要的jar包,如下:(commons-logging.jar是必须的)
2.修改WEB-INF目录下的 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"> <servlet> <servlet-name>dispatch</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatch</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>3.在WEB-INF目录下新建一个spring-servlet.xml文件,这个文件的名字可以随便,比如:applicationContext-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 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"></context:component-scan> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> </beans>
5.在web包中新建一个java类,比如:HomeController
package com; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HomeController { @RequestMapping("home") public String home(){ return "home"; } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> </head> <body> hello ,this is a spring mvc demo!! </body> </html>
<host id="second.com" root-directory="."> <web-app id='/' document-directory="E:/myeclipse_workspace/Four/WebRoot"> <stdout-log path='E:/myeclipse_workspace/Four/logs/stdout.log' rollover-period='1W'/> <stderr-log path='E:/myeclipse_workspace/Four/logs/stderr.log' rollover-period='1W'/> <class-loader> <compiling-loader path='E:/myeclipse_workspace/Four/WebRoot/WEB-INF/classes'/> <library-loader path='E:/myeclipse_workspace/Four/WebRoot/WEB-INF/lib'/> </class-loader> <servlet-mapping url-pattern="/servlet/*" servlet-name="invoker"/> <session-config> <session-max>4096</session-max> <session-timeout>60</session-timeout> <enable-cookies>true</enable-cookies> <enable-url-rewriting>true</enable-url-rewriting> <always-save-session>true</always-save-session> </session-config> </web-app> </host>
127.0.0.1 second.com
9.测试一下:http://second.com/home.do