Spring 3 MVCとXMLの例
Spring 3では、「http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-annotation-driven[mvc:annotation- JAXBがプロジェクトのクラスパスにある場合、XMLファイルとの変換オブジェクトのサポートです。
このチュートリアルでは、戻りオブジェクトをXML形式に変換し、Spring @ MVCフレームワーク経由でそれをユーザーに返す方法を説明します。
使用される技術:
-
Spring 3.0.5.RELEASE
-
JDK 1.6
-
Eclipse 3.6
-
Maven 3
プロジェクトの依存関係
特別な依存関係はなく、Mavenの `pom.xml`にのみSpring MVCを組み込む必要があります。
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
2. JAXBモデル
単純なPOJOモデルで、
JAXBアノテーション
でアノテートされた後、このオブジェクトをXML出力に変換します。
package com.mkyong.common.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "coffee")
public class Coffee {
String name;
int quanlity;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getQuanlity() {
return quanlity;
}
@XmlElement
public void setQuanlity(int quanlity) {
this.quanlity = quanlity;
}
public Coffee(String name, int quanlity) {
this.name = name;
this.quanlity = quanlity;
}
public Coffee() {
}
}
3.コントローラー
メソッドの戻り値に「
@ ResponseBody
」を追加します。http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/の詳細はありません。 ResponseBody.html[Springのドキュメント]
私が知っているように、春が見えるとき
-
JAXBで注釈が付けられたオブジェクト
-
クラスパスにJAXBライブラリが存在しました
-
“mvc:注釈駆動型”が有効になっています
-
@ResponseBodyで注釈を付けられた戻りメソッド
それは自動的に変換を処理します。
package com.mkyong.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mkyong.common.model.Coffee;
@Controller
@RequestMapping("/coffee")
public class XMLController {
@RequestMapping(value="{name}", method = RequestMethod.GET)
public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) {
Coffee coffee = new Coffee(name, 100);
return coffee;
}
}
4. mvc:アノテーション駆動型
Springの設定XMLファイルの1つで、「mvc:annotation-driven」を有効にしてください。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.mkyong.common.controller"/>
<mvc:annotation-driven/>
</beans>
-
Note
+また、 ”
spring-oxm.jar
“依存関係を宣言し、変換を処理するために `MarshallingView`の後に次のものを含めることができます。このメソッドでは、メソッドに注釈
@ ResponseBody ** は必要ありません。
<beans ...>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean id="xmlViewer"
class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.mkyong.common.model.Coffee</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</beans>
5.デモ
「spring-mvc-xml-demo」、width = 530、height = 300]
ソースコードをダウンロードする
ダウンロードする –
SpringMVC-XML-Example.zip
(7 KB)
参考文献
-
リンク://spring-mvc/spring-3-mvc-and-rss-feed-example/[Spring MVCと
Rssの例]。
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-annotation-driven
[mvc-annotation-driven
JavaDoc]。
http://static.springsource.org/spring-ws/sites/1.5/apidocs/org/springframework/oxm/jaxb/Jaxb2Marshaller.html
[Jaxb2Marshaller
JavaDoc]。
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html
[ResponseBody.html
JavaDoc]