- 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 - Xml檢視解析器示例
XmlViewResolver 用於使用在xml檔案中定義的檢視bean解析檢視名稱。以下示例演示瞭如何在Spring Web MVC框架中使用XmlViewResolver。
TestWeb-servlet.xml
<bean class = "org.springframework.web.servlet.view.XmlViewResolver">
<property name = "location">
<value>/WEB-INF/views.xml</value>
</property>
</bean>
views.xml
<bean id = "hello" class = "org.springframework.web.servlet.view.JstlView"> <property name = "url" value = "/WEB-INF/jsp/hello.jsp" /> </bean>
例如,使用上述配置,如果請求URI -
/hello,DispatcherServlet 將請求轉發到view.xml中bean hello定義的hello.jsp。
首先,讓我們準備好一個可用的Eclipse IDE,並按照以下步驟使用Spring Web框架開發基於動態表單的Web應用程式。
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為TestWeb的專案,位於com.tutorialspoint包下,如Spring MVC - Hello World章節中所述。 |
| 2 | 在com.tutorialspoint包下建立一個Java類HelloController。 |
| 3 | 在jsp子資料夾下建立一個檢視檔案hello.jsp。 |
| 4 | 下載JSTL庫 jstl.jar。將其放入你的CLASSPATH。 |
| 5 | 最後一步是建立原始檔和配置檔案的內容,並匯出應用程式,如下所述。 |
HelloController.java
package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
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">
<context:component-scan base-package = "com.tutorialspoint" />
<bean class = "org.springframework.web.servlet.view.XmlViewResolver">
<property name = "location">
<value>/WEB-INF/views.xml</value>
</property>
</bean>
</beans>
views.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 id = "hello"
class = "org.springframework.web.servlet.view.JstlView">
<property name = "url" value = "/WEB-INF/jsp/hello.jsp" />
</bean>
</beans>
hello.jsp
<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
建立完原始檔和配置檔案後,匯出你的應用程式。右鍵單擊你的應用程式,使用**匯出→WAR檔案**選項,並將**HelloWeb.war**檔案儲存到Tomcat的webapps資料夾中。
現在,啟動你的Tomcat伺服器,並確保你可以使用標準瀏覽器訪問webapps資料夾中的其他網頁。嘗試訪問URL - **https://:8080/HelloWeb/hello**,如果Spring Web應用程式一切正常,我們將看到以下螢幕。
廣告