Spring MVC hello world注釈の例
Spring 4 MVC Hello World]。リンク://spring3/spring-3-mvc-hello-world-example/[Maven Spring 3 MVC
こんにちは世界]
このチュートリアルでは、以前のリンク//spring-mvc/spring-mvc-hello-world-example/[Spring MVC hello world XMLベース]のリンクを取り、アノテーションベースの** プロジェクトに変換します。
使用される技術:
-
Spring 2.5.6
-
JDK 1.6
-
Maven 3
-
Eclipse 3.6
1.ディレクトリ構造

2. Maven
Springのアノテーションは、同じ `spring-webmvc.jar`にバンドルされています。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4__0__0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.common</groupId>
<artifactId>spring2-mvc-annotation-hello-world</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Spring 2 MVC</name>
<properties>
<jdk.version>1.6</jdk.version>
<spring.version>2.5.6</spring.version>
<jstl.version>1.2</jstl.version>
<servletapi.version>2.5</servletapi.version>
</properties>
<dependencies>
<!-- Spring MVC framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- for compile only, your container should have this -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servletapi.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/spring2</contextPath>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<wtpversion>2.0</wtpversion>
<wtpContextName>spring2</wtpContextName>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.コントローラー
これで、
@ Controller`と
@ RequestMapping`を使ってXML設定を置き換えることができます。
-
コントローラー
– コントローラークラスは、もはや-
AbstractController
または
SimpleFormController
** のようなベースコントローラは、クラスにhttp://static.springsource.org/spring/を注釈を付けるだけですdocs/2.5.x/api/org/springframework/stereotype/Controller.html[@Controller]アノテーション。
-
-
ハンドラマッピング
– 以下のようなハンドラマッピングの宣言はもうありません-
link://spring-mvc/spring-mvc-beannameurlhandlermapping-example/
、
link://spring-mvc/spring-mvc-controllerclassnamehandlermapping-example/
または
link://spring -mvc/spring-mvc-simpleurlhandlermapping-example/
、すべてが標準に置き換えられます
@RequestMapping
** アノテーション。
-
HelloWorldController.java
package com.mkyong.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/welcome")
public class HelloWorldController{
@RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld(){
ModelAndView model = new ModelAndView("HelloWorldPage");
model.addObject("msg", "hello world");
return model;
}
}
`@ RequestMapping`がクラスレベルで適用されている場合(メソッドレベルで
マルチアクションコントローラ
で適用できる)、
RequestMethod
は、マッピング要求を処理するメソッドを示します。
この場合、URIパターン `/welcome`が要求されると、このHelloWorldControllerにマップされ、
helloWorld()
メソッドで要求を処理します。
4.春のXML設定
ビューリゾルバとコンポーネントスキャンをXMLファイルで設定する必要があります。
/WEB-INF/spring-mvc-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<context:component-scan base-package="com.mkyong.common.controller"/>
</beans>
5. JSPページ
デモ用のシンプルなJSPページ。
HelloWorldPage.jsp。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<h1>Spring MVC Hello World Annotation Example</h1>
<h2>${msg}</h2>
</body>
</html>
6. web.xml
web.xml
<web-app id="WebApp__ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app__2__4.xsd">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>** .htm</url-pattern>
</servlet-mapping>
</web-app>
7.デモ
6.1埋め込みJettyを実行するには、次のように入力します。
$ mvn jetty:run
URL:http://localhost:8080/spring2/welcome.htm

6.2 Eclipse IDEにインポートする。
$ mvn eclipse:eclipse
-
Note ** このSpring MVCアノテーションベースのhello worldの例を、以前の
XMLベースの例
と比較すると、このアノテーションアプローチはコントローラークラスを明示的に宣言したり、特定のクラスを拡張したりする必要がないため、コントローラークラスとURLハンドラーのマッピングを簡単かつ柔軟に配線できます。
ソースコードをダウンロードする
ダウンロードする –
spring2-mvc-annotation.zip
(15 KB)
参考文献
Javadoc]。
http://docs.spring.io/spring/docs/2.5.x/api/org/springframework/web/bind/annotation/RequestMapping.html
[RequestMapping
Javadoc]。リンク://spring-mvc/spring-mvc-hello-world-example/[Spring MVC hello
世界のXMLベースの例]。リンク://spring/spring-auto-scanning-components/[Spring自動スキャン
コンポーネント]