標籤:xsd set mes tomcat 完整 ack uri 9.png tool
ResourceBundleViewResolver使用屬性檔案中定義的視圖bean來解析視圖名稱。 以下樣本顯示如何使用Spring Web MVC架構中的ResourceBundleViewResolver。
ResourceBundleViewResolver-servlet.xml 配置如下所示 -
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="views" /></bean>
XML
這裡basename是指攜帶視圖的資源束的名稱。資源束的預設名稱是views.properties,可以使用basename屬性重寫(也就是將views修改為其它名稱,對應的views.properties檔案名稱也要修改)。
views.properties 配置如下所示 -
hello.(class)=org.springframework.web.servlet.view.JstlViewhello.url=/WEB-INF/jsp/hello.jsp
Shell
例如,使用上面的配置,如果URI:
- 對於
/hello請求,DispatcherServlet會將請求轉寄到由views.properties中定義的hello對應的 hello.jsp 。
- 這裡“
hello”是要匹配的視圖名稱。class指定檢視類型,url是視圖的位置。
首先,讓我們使用Eclipse IDE,並按照以下步驟使用Spring Web Framework開發基於動態表單的Web應用程式:
- 建立一個名稱為 ResourceBundleViewResolver 的動態WEB項目。
- 在
com.yiibai.springmvc 包下建立一個Java類HelloController。
- 在
jsp子檔案夾下建立一個視圖檔案:hello.jsp。
- 在
src檔案夾下建立屬性檔案views.properties。
- 下載JSTL庫jstl.jar。 把它放在
CLASSPATH 中。
- 最後一步是建立所有源和設定檔的內容並運行應用程式,詳細如下所述。
完整的專案檔目錄結構如下所示 -
HelloController.java 的代碼如下所示 -
package com.yiibai.springmvc;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資源綁定視圖解析器!"); return "hello"; }}
Java
ResourceBundleViewResolver-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.yiibai" /> <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="views" /> </bean></beans>
XML
views.properties 配置如下所示 -
hello.(class)=org.springframework.web.servlet.view.JstlViewhello.url=/WEB-INF/jsp/hello.jsp
hello.jsp 的代碼如下所示 -
<%@ page contentType="text/html; charset=UTF-8" %><html><head><title>Hello World</title></head><body> <h2>${message}</h2></body></html>
HTML
完成建立源和設定檔後,發布應用程式到Tomcat伺服器。
現在啟動Tomcat伺服器,當訪問URL => http://localhost:8080/ResourceBundleViewResolver/hello , 如果Spring Web應用程式沒有問題,應該看到以下結果:
Spring MVC資源綁定視圖解析器