Step 1:配置web.xml <?xml version="1.0" encoding="UTF-8"?> servlet-mapping定義所有以/page/開頭的url請求都會被spring 的DispatcherServlet處理轉發(fā),。默認情況下DispatcherServlet會讀取<servlet-name>-servlet.xml文件的配置信息初始化,,該文件中urlMapping的定義決定當前請求轉發(fā)給哪個controller來處理,。<web-app version="2.4" xmlns="http://java./xml/ns/j2ee" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://java./xml/ns/j2ee http://java./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> Step2:定義/WEB-INF/springMVC-servlet.xml <?xml version="1.0" encoding="UTF-8"?> urlMapping定義/controller/開頭的url請求由名字為controller的控制器來處理,因為是多動作處理器,,所以要定義MethodNameResolver來告訴springMVC應該調用controller的哪個方法,,這里用的是InternalPathMethodNameResolver,該方法名解釋器會把整個URL中最后一個"/"和最后一個"."之間的部分作為要調用的方法名,。<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www./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> 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"%> 因為InternalPathMethodNameResolver方法名解釋器會把整個URL中最后一個"/"和最后一個"."之間的部分作為要調用的方法名,所以你可以在這個URL后面加任意文件格式的后綴,,比如:<%@ taglib prefix="c" uri="http://java./jstl/core_rt" %> <%@ taglib prefix="fmt" uri="http://java./jstl/fmt"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head></head> <body> <c:out value="${model.message}"/> </body> </html> localhost:8080/page/controller/method1.jsp 或 localhost:8080/page/controller/method2.html 很爽吧,,和真實的URL地址一樣,。 另:開發(fā)環(huán)境:MyEclipse5.0M2+tomcat5.5 需要用到spring.jar和jstl.jar兩個包。 附:源代碼下載 |
|