SpringMVC-リソースバンドルビューリゾルバーの例
ザ・ ResourceBundleViewResolverプロパティファイルで定義されたビューBeanを使用してビュー名を解決するために使用されます。次の例は、Spring WebMVCフレームワークを使用してResourceBundleViewResolverを使用する方法を示しています。
TestWeb-servlet.xml
<bean class = "org.springframework.web.servlet.view.ResourceBundleViewResolver">
   <property name = "basename" value = "views" />
</bean>ここでは、 basenameビューを運ぶリソースバンドルの名前を指します。リソースバンドルのデフォルト名はviews.properties、basenameプロパティを使用してオーバーライドできます。
views.properties
hello.(class) = org.springframework.web.servlet.view.JstlView
hello.url = /WEB-INF/jsp/hello.jspたとえば、上記の構成を使用すると、URI −
- / helloが要求されると、DispatcherServletはその要求をviews.propertiesのbeanhelloによって定義されたhello.jspに転送します。 
- ここで、「hello」は照合するビュー名です。一方、class ビュータイプを参照し、URLはビューの場所です。 
まず、動作するEclipse IDEを配置し、Spring WebFrameworkを使用して動的フォームベースのWebアプリケーションを開発するための次の手順を検討します。
| ステップ | 説明 | 
|---|---|
| 1 | Spring MVC-Hello Worldの章で説明されているように、パッケージcom.tutorialspointの下にTestWebという名前のプロジェクトを作成します。 | 
| 2 | com.tutorialspointpackageの下にJavaクラスHelloControllerを作成します。 | 
| 3 | jspサブフォルダの下にビューファイルhello.jspを作成します。 | 
| 4 | srcフォルダーの下にプロパティファイルviews.propertiesを作成します。 | 
| 5 | JSTLライブラリjstl.jarをダウンロードします。CLASSPATHに入れてください。 | 
| 6 | 最後のステップは、以下で説明するように、ソースファイルと構成ファイルのコンテンツを作成し、アプリケーションをエクスポートすることです。 | 
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" />
   </bean>
</beans>views.properties
hello.(class) = org.springframework.web.servlet.view.JstlView
hello.url = /WEB-INF/jsp/hello.jsphello.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フォルダーから他のWebページにアクセスできることを確認します。URLにアクセスしてみてください-http://localhost:8080/HelloWeb/hello Spring Webアプリケーションで問題がなければ、次の画面が表示されます。
