Spring MVC - 內部資源檢視解析器示例



InternalResourceViewResolver 用於將提供的 URI 解析為實際的 URI。以下示例演示瞭如何使用 Spring Web MVC 框架使用 InternalResourceViewResolver。InternalResourceViewResolver 允許將網頁與請求進行對映。

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";
   }
}
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name = "prefix" value = "/WEB-INF/jsp/"/>
   <property name = "suffix" value = ".jsp"/>
</bean>

例如,使用上述配置,如果請求 URI

  • /hello,DispatcherServlet 將把請求轉發到字首 + 檢視名稱 + 字尾 = /WEB-INF/jsp/hello.jsp。

首先,讓我們準備好一個可用的 Eclipse IDE,然後考慮以下步驟來使用 Spring Web 框架開發基於動態表單的 Web 應用程式。

步驟 描述
1 建立一個名為 TestWeb 的專案,位於 com.tutorialspoint 包下,如 Spring MVC - Hello World 示例章節中所述。
2 在 com.tutorialspoint 包下建立一個名為 HelloController 的 Java 類。
3 在 jsp 子資料夾下建立一個名為 hello.jsp 的檢視檔案。
4 最後一步是建立原始檔和配置檔案的內容,並匯出應用程式,如下所述。

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.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".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 檔案選項,並將TestWeb.war檔案儲存到 Tomcat 的 webapps 資料夾中。

現在,啟動您的 Tomcat 伺服器,並確保您可以使用標準瀏覽器從 webapps 資料夾訪問其他網頁。嘗試訪問 URL – https://:8080/TestWeb/hello,如果 Spring Web 應用程式一切正常,我們將看到以下螢幕。

Spring Internal Resource View Resolver
廣告

© . All rights reserved.