- Spring MVC 基礎
- Spring MVC - 首頁
- Spring MVC - 概述
- Spring MVC - 環境搭建
- Spring MVC - Hello World 示例
- Spring MVC - 表單處理
- Spring MVC - 表單處理
- Spring MVC - 頁面重定向
- Spring MVC - 靜態頁面
- Spring MVC - 表單標籤庫
- Spring MVC - 文字框
- Spring MVC - 密碼框
- Spring MVC - 文字區域
- Spring MVC - 複選框
- Spring MVC - 複選框組
- Spring MVC - 單選按鈕
- Spring MVC - 單選按鈕組
- Spring MVC - 下拉列表
- Spring MVC - 列表框
- Spring MVC - 隱藏域
- Spring MVC - 錯誤處理
- Spring MVC - 檔案上傳
- Spring MVC - 處理器對映
- Bean 名稱 URL 處理器對映
- 控制器類名稱處理器對映
- 簡單 URL 處理器對映
- Spring MVC - 控制器
- Spring MVC - 多動作控制器
- 屬性方法名稱解析器
- 引數方法名稱解析器
- 可引數化檢視控制器
- Spring MVC - 檢視解析器
- 內部資源檢視解析器
- Spring MVC - XML 檢視解析器
- 資源束檢視解析器
- 多解析器對映
- Spring MVC - 整合
- Spring MVC - Hibernate 驗證器
- Spring MVC - 生成 RSS Feed
- Spring MVC - 生成 XML
- Spring MVC - 生成 JSON
- Spring MVC - 生成 Excel
- Spring MVC - 生成 PDF
- Spring MVC - 使用 log4j
- Spring 常見問題解答
- Spring - 常見問題解答
- Spring 有用資源
- Spring MVC - 快速指南
- Spring MVC - 有用資源
- Spring MVC - 討論
Spring MVC - 引數方法名稱解析器示例
以下示例演示瞭如何使用 Spring Web MVC 框架中多動作控制器的引數方法名稱解析器。MultiActionController 類有助於將多個 URL 分別對映到單個控制器中的其方法。
package com.tutorialspoint;
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;
}
}
<bean class = "com.tutorialspoint.UserController">
<property name = "methodNameResolver">
<bean class = "org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name = "paramName" value = "action"/>
</bean>
</property>
</bean>
例如,使用上述配置,如果請求 URI 為:
/user/*.htm?action=home,DispatcherServlet 將請求轉發到 UserController 的home() 方法。
/user/*.htm?action=add,DispatcherServlet 將請求轉發到 UserController 的add() 方法。
/user/*.htm?action=remove,DispatcherServlet 將請求轉發到 UserController 的remove() 方法。
首先,讓我們準備好一個可用的 Eclipse IDE,並按照以下步驟使用 Spring Web 框架開發基於動態表單的 Web 應用程式。
| 步驟 | 描述 |
|---|---|
| 1 | 在 Spring MVC - Hello World 章節中說明的基礎上,建立一個名為 TestWeb 的專案,其包名為 com.tutorialspoint。 |
| 2 | 在 com.tutorialspoint 包下建立一個名為 UserController 的 Java 類。 |
| 3 | 在 jsp 子資料夾下建立一個名為 user.jsp 的檢視檔案。 |
| 4 | 最後一步是建立原始檔和配置檔案的內容,並匯出應用程式,如下所述。 |
UserController.java
package com.tutorialspoint;
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;
}
}
TestWeb-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.tutorialspoint.UserController">
<property name = "methodNameResolver">
<bean class = "org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name = "paramName" value = "action"/>
</bean>
</property>
</bean>
</beans>
user.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
建立完原始檔和配置檔案後,匯出您的應用程式。右鍵單擊您的應用程式,使用匯出 → WAR 檔案選項並將TestWeb.war檔案儲存到 Tomcat 的 webapps 資料夾中。
現在,啟動您的 Tomcat 伺服器,並確保您可以使用標準瀏覽器從 webapps 資料夾訪問其他網頁。現在,嘗試 URL:https://:8080/TestWeb/user/test.htm?action=home,如果 Spring Web 應用程式一切正常,您將看到以下螢幕。
廣告