1、新建一web项目 springmvc,并在lib目录中加入jar包commons-logging.jar、log4j-1.2.13.jar、slf4j-api-1.5.6.jar\spring.jar\spring-webmvc.jar\ spring-web.jar

2、把web项目中的web.xml改成如下:

Xml代码
  1. xmlversion="1.0"encoding="UTF-8"?>

  2. <web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

  4. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  5. <welcome-file-list>

  6. <welcome-file>index.jspwelcome-file>

  7. welcome-file-list>

  8. <servlet>

  9. <servlet-name>dispatcherServletservlet-name>

  10. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

  11. servlet>

  12. <servlet-mapping>

  13. <servlet-name>dispatcherServletservlet-name>

  14. <url-pattern>*.dourl-pattern>

  15. servlet-mapping>

  16. web-app>




3、新建一测试用的action类,HelloWorldAction.java类
Java代码
  1. package cn.ehoo.springmvc.test;

  2. import java.io.IOException;

  3. import java.util.HashMap;

  4. import java.util.Map;

  5. import javax.servlet.ServletException;

  6. import javax.servlet.http.HttpServletRequest;

  7. import javax.servlet.http.HttpServletResponse;

  8. import org.springframework.web.servlet.ModelAndView;

  9. import org.springframework.web.servlet.mvc.Controller;

  10. publicclass HelloWorldAction implements Controller { // 控制器需要实现 Controller接口

  11. private String helloWorld; // 该属性用于获取配置文件中的helloWorld属性

  12. private String viewPage; // 用于获取配置文件中的viewPage属性

  13. public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res)

  14. throws ServletException, IOException { // 在该方法中处理用户请求

  15. Map model = new HashMap();

  16. model.put("helloWorld", getHelloWorld()); // 将helloWorld属性存入model中

  17. returnnew ModelAndView(getViewPage(), model); // 调用getViewPage获取要返回的页面

  18. }

  19. public String getHelloWorld() {

  20. return helloWorld;

  21. }

  22. publicvoid setHelloWorld(String helloWorld) {

  23. this.helloWorld = helloWorld;

  24. }

  25. public String getViewPage() {

  26. return viewPage;

  27. }

  28. publicvoid setViewPage(String viewPage) {

  29. this.viewPage = viewPage;

  30. }

  31. }



4、在WEB-INF目录中加入一个配置文件dispatcherServlet-servlet.xml,里面的内容为

Xml代码
  1. xmlversion="1.0"encoding="UTF-8"?>

  2. org/dtd/spring-beans.dtd">

  3. <beans>

  4. <beanid="localeResolver"class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">bean>

  5. <beanid="urlMapping"class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

  6. <propertyname="mappings">

  7. <props>

  8. <propkey="helloWorld.do">helloWorldActionprop>

  9. props>

  10. property>

  11. bean>

  12. <beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  13. <propertyname="viewClass">

  14. <value>org.springframework.web.servlet.view.InternalResourceViewvalue>

  15. property>

  16. bean>

  17. <beanid="helloWorldAction"class="cn.ehoo.springmvc.test.HelloWorldAction">

  18. <propertyname="helloWorld">

  19. <value>Hello Spring World!value>

  20. property>

  21. <propertyname="viewPage">

  22. <value>sayHello.jspvalue>

  23. property>

  24. bean>

  25. beans>




再在WebRoot目录下新建一个用于显示的jsp页面,sayHello.jsp
Html代码
  1. <%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

  2. <%

  3. String path = request.getContextPath();

  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

  5. %>

  6. >

  7. <html>

  8. <head><title>第一个Spring MVC实例title>head>

  9. <%

  10. String str = (String)request.getAttribute("helloWorld");

  11. %>

  12. <body>

  13. <h1>您输入的欢迎语是<%=str%>h1>

  14. body>

  15. html>

测试地址为:

http://127.0.0.1/springmvc/helloWorld.do