Springmvc-xmlviewresolver

提供:Dev Guides
移動先:案内検索

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 Frameworkを使用して動的フォームベースのWebアプリケーションを開発します。

Step Description
1 Create a project with a name TestWeb under a package com.finddevguides as explained in the Spring MVC - Hello World chapter.
2 Create a Java class HelloController under the com.finddevguidespackage.
3 Create a view file hello.jsp under the jsp sub-folder.
4 Download JSTL library jstl.jar. Put it in your CLASSPATH.
5 The final step is to create the content of the source and configuration files and export the application as explained below.

HelloController.java

package com.finddevguides;

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.finddevguides"/>

   <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フォルダーから他のWebページにアクセスできることを確認します。 URL- http://localhost:8080/HelloWeb/hello にアクセスしてみてください。SpringWebアプリケーションで問題がなければ、次の画面が表示されます。

Spring Internal Resource View Resolver