Step 1:配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>springapp</display-name>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/page/*</url-pattern>
</servlet-mapping>
</web-app>
servlet-mapping定义所有以/page/开头的url请求都会被spring 的DispatcherServlet处理转发。默认情况下DispatcherServlet会读取<servlet-name>-servlet.xml文件的配置信息初始化,该文件中urlMapping的定义决定当前请求转发给哪个controller来处理。
Step2:
定义/WEB-INF/springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 方法名解析器 -->
<bean id="InternalPathMethodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
</bean>
<bean id="controller" class="com.controller.IndexController">
<property name="methodNameResolver">
<ref bean="InternalPathMethodNameResolver" />
</property>
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/controller/*">controller</prop>
</props>
</property>
</bean>
</beans>
urlMapping定义/controller/开头的url请求由名字为controller的控制器来处理,因为是多动作处理器,所以要定义MethodNameResolver来告诉springMVC应该调用controller的哪个方法,这里用的是InternalPathMethodNameResolver,该方法名解释器会把整个URL中最后一个"/"和最后一个"."之间的部分作为要调用的方法名。
Step3:
定义controller类并继承MultiActionController
package com.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class IndexController extends MultiActionController {
public ModelAndView method1(HttpServletRequest request,
HttpServletResponse respnose) throws ServletException, IOException {
Map model = new HashMap();
model.put("message", "你调用的是方法1");
return new ModelAndView("/index.jsp", "model", model);
}
public ModelAndView method2(HttpServletRequest request,
HttpServletResponse respnose) throws ServletException, IOException {
Map model = new HashMap();
model.put("message", "你调用的是方法2");
return new ModelAndView("/index.jsp", "model", model);
}
}
通过配置文件,访问上面这个controller中某个方法的url即为:localhost:8080/page/controller/方法名
Step4:/index.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<body>
<c:out value="${model.message}"/>
</body>
</html>
因为InternalPathMethodNameResolver方法名解释器会把整个URL中最后一个"/"和最后一个"."之间的部分作为要调用的方法名,所以你可以在这个URL后面加任意文件格式的后缀,比如:
localhost:8080/page/controller/method1.jsp
或
localhost:8080/page/controller/method2.html
很爽吧,和真实的URL地址一样。另:开发环境:MyEclipse5.0M2+tomcat5.5
需要用到spring.jar和jstl.jar两个包。
附:源代码下载