
- 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 - 基於Bean名稱的URL處理器對映示例
以下示例演示瞭如何使用Spring Web MVC框架中的Bean名稱URL處理器對映。BeanNameUrlHandlerMapping類是預設的處理器對映類,它將URL請求對映到配置中提到的Bean名稱。
<beans> <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.handler.BeanNameUrlHandlerMapping"/> <bean name = "/helloWorld.htm" class = "com.tutorialspoint.HelloController" /> <bean name = "/hello*" class = "com.tutorialspoint.HelloController" /> <bean name = "/welcome.htm" class = "com.tutorialspoint.WelcomeController"/> </beans>
例如,使用上述配置,如果請求URI
/helloWorld.htm 或 /hello{任意字母}.htm,DispatcherServlet將請求轉發到HelloController。
/welcome.htm,DispatcherServlet將請求轉發到WelcomeController。
/welcome1.htm,DispatcherServlet將找不到任何控制器,伺服器將丟擲404狀態錯誤。
首先,讓我們準備好一個可用的Eclipse IDE,並考慮以下步驟來使用Spring Web框架開發基於動態表單的Web應用程式。
步驟 | 描述 |
---|---|
1 | 建立一個名為TestWeb的專案,位於com.tutorialspoint包下,如Spring MVC - Hello World章節中所述。 |
2 | 在com.tutorialspoint包下建立Java類HelloController和WelcomeController。 |
3 | 在jsp子資料夾下建立檢視檔案hello.jsp和welcome.jsp。 |
4 | 最後一步是建立所有原始檔和配置檔案的內容,並匯出應用程式,如下所述。 |
HelloController.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.AbstractController; public class HelloController extends AbstractController{ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView model = new ModelAndView("hello"); model.addObject("message", "Hello World!"); return model; } }
WelcomeController.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.AbstractController; public class WelcomeController extends AbstractController{ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView model = new ModelAndView("welcome"); model.addObject("message", "Welcome!"); 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.handler.BeanNameUrlHandlerMapping"/> <bean name = "/helloWorld.htm" class = "com.tutorialspoint.HelloController" /> <bean name = "/hello*" class = "com.tutorialspoint.HelloController" /> <bean name = "/welcome.htm" class = "com.tutorialspoint.WelcomeController"/> </beans>
hello.jsp
<%@ page contentType = "text/html; charset = UTF-8" %> <html> <head> <title>Hello World</title> </head> <body> <h2>${message}</h2> </body> </html>
welcome.jsp
<%@ page contentType = "text/html; charset = UTF-8" %> <html> <head> <title>Welcome</title> </head> <body> <h2>${message}</h2> </body> </html>
建立完原始檔和配置檔案後,匯出您的應用程式。右鍵單擊您的應用程式,使用匯出→WAR檔案選項,並將TestWeb.war檔案儲存到Tomcat的webapps資料夾中。
現在,啟動您的Tomcat伺服器,並確保您可以使用標準瀏覽器從webapps資料夾訪問其他網頁。嘗試使用URL - https://:8080/TestWeb/helloWorld.htm,如果Spring Web應用程式一切正常,我們將看到以下螢幕。

嘗試使用URL - https://:8080/TestWeb/hello.htm,如果Spring Web應用程式一切正常,我們將看到以下螢幕。

嘗試使用URL https://:8080/TestWeb/welcome.htm,如果Spring Web應用程式一切正常,我們將看到以下螢幕。

嘗試使用URL https://:8080/TestWeb/welcome1.htm,如果Spring Web應用程式一切正常,我們將看到以下螢幕。
