1. 程式人生 > >Spring MVC-視圖解析器(View Resolverr)-多重解析器(Multiple Resolver)示例(轉載實踐)

Spring MVC-視圖解析器(View Resolverr)-多重解析器(Multiple Resolver)示例(轉載實踐)

quest springmvc tab type elm utf servle err path

以下內容翻譯自:https://www.tutorialspoint.com/springmvc/springmvc_multiple_resolver_mapping.htm

說明:示例基於Spring MVC 4.1.6

如果要在spring mvc應用程序中使用多個視圖解析器,則可以使用order屬性設置優先級順序。以下示例顯示了如何在Spring Web MVC框架中使用ResourceBundleViewResolver和InternalResourceViewResolver。

TestWeb-servlet.xml

<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"
> <property name="basename" value="views" /> <property name="order" value="0" /> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"
/> <property name="order" value="1" /> </bean>
  1. 這裏order屬性定義視圖解析器的排名。0作為第一解析器,1作為下一個解算器等。

views.properties

hello.(class)=org.springframework.web.servlet.view.JstlView
hello.url=/WEB-INF/jsp/hello.jsp

例如,使用上面的配置,如果是URI

  1. /hello被請求,DispatcherServlet將請求轉發到由在您的view.properties中的bean hello定義的hello.jsp。

首先,讓我們使用Eclipse IDE,並按照以下步驟使用Spring Web Framework開發基於動態窗體的Web應用程序:

步驟描述
1 創建一個名為TestWeb的項目,在一個包com.tutorialspoint下,如Spring MVC - Hello World Example章節所述。
2 在com.tutorialspoint包下創建一個Java類HelloController。
3 在jsp子文件夾下創建一個視圖文件hello.jsp。
4 在src文件夾下創建一個屬性文件views.properties。
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.ResourceBundleViewResolver">
      <property name="basename" value="views" />
      <property name="order" value="0" />
   </bean>
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
      <property name="order" value="1" />
   </bean>
</beans>

views.properties

hello.(class)=org.springframework.web.servlet.view.JstlView
hello.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>

完成創建源文件和配置文件後,導出應用程序。右鍵單擊應用程序,然後使用Export > WAR File選項,並將您的HelloWeb.war文件保存在Tomcat的webapps文件夾中。

現在啟動您的Tomcat服務器,並確保您可以使用標準瀏覽器從webapps文件夾訪問其他網頁。現在嘗試訪問URL http://localhost:8080/HelloWeb/hello,如果您的Spring Web應用程序一切正常,您應該看到以下結果:

技術分享

Maven示例:

https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test25

Spring MVC-視圖解析器(View Resolverr)-多重解析器(Multiple Resolver)示例(轉載實踐)