Spring MVC資源綁定視圖解析器

來源:互聯網
上載者:User

標籤: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應用程式:

  1. 建立一個名稱為 ResourceBundleViewResolver 的動態WEB項目。
  2. 在 com.yiibai.springmvc 包下建立一個Java類HelloController
  3. jsp子檔案夾下建立一個視圖檔案:hello.jsp
  4. src檔案夾下建立屬性檔案views.properties
  5. 下載JSTL庫jstl.jar。 把它放在 CLASSPATH 中。
  6. 最後一步是建立所有源和設定檔的內容並運行應用程式,詳細如下所述。

完整的專案檔目錄結構如下所示 -

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資源綁定視圖解析器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.