Spring-MVCåãã®ä¸æ©
Spring-MVCã§ç»é¢ã表示ãããã¨ããã¾ã§ãåå¼·ããã®ã§ã¾ã¨ãã¾ãã
Spring3å ¥éã®6ç« ã®ååãã¾ã¨ããæãã§ãã
Spring tool suiteã®ã¤ã³ã¹ãã¼ã«
http://www.springsource.org/downloads/sts-ggts
ããã¤ã³ã¹ãã¼ã©ããã¦ã³ãã¼ãããã
ä»å使ç¨ããã®ã¯linuxã®64bitç
spring-tool-suite-3.2.0.RELEASE-e3.8.2-linux-gtk-x86_64-installer.sh
Mavenããã¸ã§ã¯ããä½æãã
[File] > [New] > [Project]
[Maven] > [Maven Project] ãé¸æ
[Create a simple project(skip archetype selection)ã«ãã§ãã¯ãä»ãã
以ä¸ã®å
容ãå
¥åãã
Group Id: SampleGroup Artifact Id: SampleArtifact Packaging: war
ããã§ç©ºã®ããã¸ã§ã¯ããä½æãããã
web.xmlãä½æãã
ä½æããWebã¢ããªã±ã¼ã·ã§ã³ã®åºæ¬çãªè¨å®ãè¨è¿°ããããã®web.xmlãä½æããã
src/main/webapp/WEB-INF/ãã©ã«ããä½æããããã«æ°ãã«web.xmlã¨ãããã¡ã¤ã«ãä½æããã
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Webã³ã³ããä¸ã«DIã³ã³ãããé ç½®ããè¨å®...ã¨æ¸ãã¦ãã£ã --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/META-INF/spring/beans-biz.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- DispatcherServletã®å®ç¾© --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/META-INF/spring/beans-webmvc.xml <!-- ViewResolverãªã©ã®è¨å® --> </param-value> </init-param> </servlet> <!-- Mappingã®è¨å® ãã¹ã¦ã®ã¢ã¯ã»ã¹ãDispatcherServletã«æ¸¡ãã¦DispatcherServletå´ã§å¦çãã¦ããã --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
beans-webmvc.xmlãä½æãã
src/main/resources/META-INF/spring/beans-webmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- ãªã¯ã¨ã¹ããæ¥ãéããã®ããã±ã¼ã¸å ã®@Controllerã¯ã©ã¹ã®ä¸ããå¼ã³åºãå ãæ¢ã --> <context:component-scan base-package="sample.controller" /> <!-- @Controllerã¢ããã¼ã·ã§ã³ãæå¹ã«ããããã«å¿ è¦ --> <mvc:annotation-driven/> <!-- /WEB-INF/resources/以ä¸ã®ãã¡ã¤ã«ããããã¤ãã --> <mvc:resources location="/WEB-INF/resources/" mapping="/resources/**"/> <!-- ViewResolverã®è¨å® --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
beans-biz.xmlãä½æãã
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> </beans>
spring-webmvcã追å ãã
pom.xmlãéã
Dependenciesã¿ããéã
[Dependencies]ã®[Add...]ãã¿ã³ãã¯ãªãã¯(å·¦å´ã®ãã¤ã ã)
以ä¸ã®å 容ãå ¥åãã
Group Id: org.springframework Artifact Id: spring-webmvc Version: 3.1.1.RELEASE Scope: compile
ã³ã³ããã¼ã©ã¯ã©ã¹ãä½æãã
ã³ã³ããã¼ã©ã¯ããã©ã¦ã¶ããã®ãªã¯ã¨ã¹ããåãåããé©åãªå¦ç(DBãããã¼ã¿ãå¼ã£å¼µã£ã¦ãã¦å å·¥ããã¨ãããããå¦ç)ããã¦ãå¿ è¦ãªãã¼ã¿ãã¾ã¨ãã¦Viewã«æ¸¡ãå½¹å²
SpringMVCã®å ´åããããããã¢ããã¼ã·ã§ã³ãã¦ãããã¨ã§URIã¨HTTPã¡ã½ããããDispatcherServletãå¼ã³åºãã¹ãã¡ã½ãããæå®ãã
package sample.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class SampleController { @RequestMapping(value="/sample", method=RequestMethod.GET) public String sample() { return "sample-view"; } }
jspãä½æãã
src/main/webapp/WEB-INF/views/
å
ã«sample-view.jspãä½æããã
ä»åã¯JSPã®èª¬æãç®çã§ã¯ãªãã¦ãã¨ãããããã¼ã¸ã表示åºæ¥ãã°ããã§ããã®ã§å
容ã¯ä»¥ä¸ã«ãã¦ããã
Hello, Spring-MVC!
以ä¸ã§ä½æ¥ã¯å®äºã
[Run As...] > [Maven build]
ãã¦ãhttp://localhost:8080/SampleArtifact/sample ã«ã¢ã¯ã»ã¹ããã¨
"Hello, Spring-MVC!"ã£ã¦è¡¨ç¤ºãããã¯ãã
çµããã«
XMLã®è¨å®ãè²ã ãã£ã¦ãããã«ããã£ãã®ã§ãæä½éãªã«ãå¿ è¦ã§ãããã¯ãªã«ããã¦ããã®ããç¥ãããã«è¡ã£ãä½æ¥ã®ãã°ã§ãã