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應用程式一切正常,我們將看到以下螢幕。

Spring Internal Resource View Resolver
廣告
© . All rights reserved.