以下範例顯示如何使用Spring Web MVC框架來實現多動作控制器的屬性方法名稱解析器。 MultiActionController
類可在單個控制器中分別對映多個URL
到對應的方法。
所下所示組態 -
package com.yiibai.springmvc;
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 UserController extends MultiActionController{
public ModelAndView home(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "主頁面(Home方法)");
return model;
}
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "新增(Add方法)");
return model;
}
public ModelAndView remove(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "刪除(Remove方法)");
return model;
}
}
URL對映組態檔案如下 -
<bean class="com.yiibai.springmvc.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/user/home.html">home</prop>
<prop key="/user/add.html">add</prop>
<prop key="/user/update.html">update</prop>
</props>
</property>
</bean>
</property>
</bean>
例如,使用上面的組態,如果URI:
/user/home.html
請求,DispatcherServlet
將請求轉發到UserController
類的 home()
方法。user/add.html
,DispatcherServlet
將請求轉發到UserController
類的 add()
方法。user/remove.html
,DispatcherServlet
將請求轉發到UserController
類的 remove()
方法。首先,讓我們使用Eclipse IDE,並按照以下步驟使用Spring Web Framework開發基於動態表單的Web應用程式:
com.yiibai.springmvc
包下建立一個Java類UserController
。jsp
子檔案夾下建立一個檢視檔案:user.jsp
。完整的專案檔案目錄結構如下所示 -
UserController.java 的程式碼如下所示 -
package com.yiibai.springmvc;
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 UserController extends MultiActionController{
public ModelAndView home(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "主頁面(Home方法)");
return model;
}
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "新增(Add方法)");
return model;
}
public ModelAndView remove(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "刪除(Remove方法)");
return model;
}
}
PropertiesMethodNameResolver-servlet.xml 的程式碼如下所示 -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="caseSensitive" value="true" />
</bean>
<bean class="com.yiibai.springmvc.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/user/home.html">home</prop>
<prop key="/user/add.html">add</prop>
<prop key="/user/remove.html">remove</prop>
</props>
</property>
</bean>
</property>
</bean>
</beans>
user.jsp 的程式碼如下所示 -
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>User.jsp Page</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
完成建立源和組態檔案後,發布應用程式到Tomcat伺服器。
現在啟動Tomcat伺服器,當存取URL => http://localhost:8080/PropertiesMethodNameResolver/user/add.html , 如果Spring Web應用程式沒有問題,應該看到以下結果:
當存取URL => http://localhost:8080/PropertiesMethodNameResolver/user/remove.html , 如果Spring Web應用程式沒有問題,應該看到以下結果: