ãããç§ã®æåã®èšäºã§ãããã³ããŒã§åŒ·ãå©ããªãã§ãã ããã
ä»æ¥ããã®æçš¿ã§ã¯ãSpringãã¬ãŒã ã¯ãŒã¯ã«é¢ããäžé£ã®èšäºãéå§ããŸãã
çè«ã®è€éãã«ã€ããŠã¯èª¬æããŸããããHabréã«ã¯ããã«é¢ããååãªèšäºããããŸãã ç·Žç¿ã奜ãã§ãã
ããã§ããxml-configurationãšã¢ãããŒã·ã§ã³ã®äž¡æ¹ã䜿çšããŠSpring Frameworkãæäœã§ããããšãæãåºããŠãã ããã ãŸããäž¡æ¹ã®ã¢ãããŒããçµã¿åãããããšã劚ãããã®ã¯äœããããŸããã
å人çã«ã¯ã泚éã䜿çšããããšããå§ãããŸãã ãããã£ãŠããããã説æãããã¹ãŠã®äŸã§ã¯æ³šéã䜿çšããŸãã
ä»æ¥ã¯äœãããããäŒãã䟡å€ããããšæããŸãã
ä»æ¥ã¯ã2ã€ã®æ°å€ã®åèšãèšç®ãããã¡ãããŠãŒã¶ãŒã«å¿çãéä¿¡ããåçŽãªWebã¢ããªã±ãŒã·ã§ã³ãäœæããŸãã
IDE Intellij IDEA 12ã§äœæ¥ããŸããå§ããŸãããã
æ°ãããããžã§ã¯ããäœæããŸãïŒFile-New ProjectïŒã
[ãããžã§ã¯ãã¿ã€ã]-Spring MVCãéžæããŸãã ãããžã§ã¯ãã«ååïŒãããžã§ã¯ãåïŒãä»ãã[å®äº]ãã¯ãªãã¯ããŸãã å¿ èŠã«å¿ããŠãå¥ã®ããŒã¹ããã±ãŒãžïŒããŒã¹ããã±ãŒãžïŒãæå®ã§ããããããcom.springapp.controllersãã«å€æŽããŸããã ãŸããã¢ããªã±ãŒã·ã§ã³ããããã€ãããJDKïŒãããžã§ã¯ãSDKïŒããã³ãµãŒããŒïŒã¢ããªã±ãŒã·ã§ã³ãµãŒããŒïŒãžã®ãã¹ãæå®ã§ããå Žåã ã芧ã®ãšãããããŒãžã§ã³7ã®JavaãšGlassfishãµãŒããŒã䜿çšããŠããŸãã
ã¹ã¯ãªãŒã³ã·ã§ãã
ãããžã§ã¯ããäœæãããŠããããã®ãããªæ§é ãæã€å¿ èŠããããŸãã
ã¹ã¯ãªãŒã³ã·ã§ãã
ããã¯ãxmlæ§æã䜿çšãããã³ãã¬ãŒããããžã§ã¯ãã§ãã ãããã泚éã䜿çšããããšæããŸãã äœåãªãã®ãåãé€ããŸãã
mvc-dispatcher-servlet.xmlãã¡ã€ã«ãšãã¹ããã©ã«ããŒãåé€ããŸãïŒãã®äžã®ãã¹ãã§ã¯mvc-dispatcher-servlet.xmlãã¡ã€ã«ã䜿çšããŸãïŒã
ããäžåºŠããã®ã¹ãããã§äœããã¹ããã«ã€ããŠã®ç»é¢ã瀺ããŸãã
ã¹ã¯ãªãŒã³ã·ã§ãã
ããã§ãéåžžã®Javaã¯ã©ã¹ã®ã¿ã䜿çšããŠãåããã£ã¹ãããã£ãµãŒãã¬ãããäœæããå¿ èŠããããŸãã ããã°ã©ã ã¯åžžã«é©åã«æ§æãããŠããªããã°ãªããªããšä¿¡ããŠãããå€ãã®äººãç§ããµããŒãããŠããããšæããŸãã æåã«ãSpringèšå®å šäœãä¿åããæ°ãããconfigãããã±ãŒãžãäœæããŸãã ãããè¡ãã«ã¯ãjavaãã©ã«ããŒãéžæãããALT-INSERTããæŒããŸãã ããã±ãŒãžåïŒcom.springapp.configïŒãå ¥åãã[OK]ãã¯ãªãã¯ããŸãã
ã¹ã¯ãªãŒã³ã·ã§ãã
ããã±ãŒãžãäœæãããŸããã æ°ããã¯ã©ã¹ãè¿œå ããŸãã ãããè¡ãã«ã¯ããã¹ãŠãALT-INSERTã§ãããããã¯ã©ã¹ããéžæããŸãã ãããMvcConfigãšåŒã³ãŸãããã
äœæãããã¯ã©ã¹ã¯æ¬¡ã®ããã«ãªããŸãã
package com.springapp.config; /** * Created with IntelliJ IDEA. * User: diversant * Date: 18.06.13 * Time: 13:05 * To change this template use File | Settings | File Templates. */ public class MvcConfig { }
æ§æã®äœæãå§ããŸãããã æ¢è£œã®æ§æã¯ã©ã¹ãããã«æã¡èŸŒã¿ã以äžã«å¿ èŠãªè©³çŽ°ããã¹ãŠèª¬æããŸãã
package com.springapp.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @ComponentScan(basePackages="com.springapp") @EnableWebMvc public class MvcConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver getViewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/pages/"); resolver.setSuffix(".jsp"); return resolver; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } }
æåããå§ããŸãããã
@Configuration
ãã®ã¯ã©ã¹ã¯Springã®æ§æãã€ãŸãBeanã®æ§æãå«ããšèšããŸãã
@ComponentScan
ãã³ãšå¯Ÿè©±ããããã«å¿ èŠãªãã£ã¬ã¯ãã£ããæ€çŽ¢ãããããã±ãŒãžãæå®ããŸãã
@EnableWebMvc
ãã®ã¯ã©ã¹ãSpring MVCã®æ§æã§ããããšã瀺ããŸãã
@Bean
泚éä»ãã¡ãœããã¯Beanã§ãããšèšããŸãã
Javaãå°ãªããšãå°ã䜿ã£ãããšããã人ãªã誰ã§ã
@Override
ã¢ãããŒã·ã§ã³ãç¥ã£ãŠãããšæããŸãã
次ã«ãã¯ã©ã¹ã¡ãœããèªäœã«ã€ããŠè©³ãã説æããŸãã
ç§ã®æèŠã§ã¯ãgetViewResolverïŒïŒã¡ãœããã§ãæãåçŽãªããŒãžãã¥ãŒã¢ãŒãæå®ããŸãããã®ãã¥ãŒã¢ãŒã®æ§æã¯ãããŒãžã«ãã¬ãã£ãã¯ã¹ãšãµãã£ãã¯ã¹ãè¿œå ããããšã§æ§æãããŸãã
addResourceHandlersïŒResourceHandlerRegistryã¬ãžã¹ããªïŒã¡ãœããã§ãããŒãžã«äœ¿çšããããã¹ãŠã®ãªãœãŒã¹ã®å Žæãæå®ããŸãã ã«ã¹ã±ãŒãã¹ã¿ã€ã«ã·ãŒããJavaã¹ã¯ãªãããã¡ã€ã«ãç»åãªã©ã䜿çšã§ããŸãã
以äžã«ãã®ãããªåçŽãªæ§æã瀺ããŸãã
ããã§ããã®ã¯ã©ã¹ããã£ã¹ãããã£ãšããŠäœ¿çšããããšãWebã³ã³ããã«èª¬æããå¿ èŠããããŸãã ãããè¡ãã«ã¯ãweb.xmlãã¡ã€ã«ãç·šéããŸãã ãã®ã¢ã€ãã¢ã«ããããã®ãããªãã³ãã¬ãŒãã³ãŒããäœæãããŸããã
<web-app 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 MVC Application</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
ããã«çœ®ãæããå¿ èŠããããŸãã
<web-app 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 MVC</display-name> <context-param> <param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>SpringDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></init-param> <init-param> <param-name>contextConfigLocation</param-name><param-value>com.springapp.config</param-value></init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringDispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
ç§ã¯è©³çŽ°ã«ãã€ã³ãããŸãããç§ã¯èšãã ãã§ã-ã³ã³ããã¹ããšãã£ã¹ãããã£ãŒãã¢ãããŒã·ã§ã³ã䜿çšããŠèšå®ãããããšã瀺ããŸãïŒcontextClassãã©ã¡ãŒã¿ãŒãšãã®org.springframework.web.context.support.AnnotationConfigWebApplicationContextå€ã䜿çšïŒã ãŸããæ§æããã±ãŒãžãæ€çŽ¢ããããã±ãŒãžã瀺ããŸãïŒcontextConfigLocationãã©ã¡ãŒã¿ãŒïŒã
次ã«ãã³ã³ãããŒã©ãŒãäœæããå¿ èŠããããŸãã Springã®ã³ã³ãããŒã©ãŒã¯ã¢ãã¬ã¹å€æïŒãã©ãŠã¶ãŒã§è¡šç€ºããããã®ïŒãå®è¡ããããŒãžïŒãã®å Žåã¯hello.jspïŒã«ãªãã€ã¬ã¯ãããŸãã
Ideaãåºæ¬çãªã³ã³ãããŒã©ãŒãäœæãããããæåã§äœæããå¿ èŠã¯ãããŸããã 念ã®ããã圌ã®ã³ãŒããæäŸããããã§äœãèµ·ãã£ãŠããã®ãã説æããŸãã
package com.springapp.controllers; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/") public class HelloController { @RequestMapping(method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("message", "Hello world!"); return "hello"; } }
泚éããå§ããŸãããã
@Controller
å®éãããã¯ã³ã³ãããŒã©ãŒã§ãããšèšããŸãã è¿œå ãããã®ã¯ãããŸãã
@RequestMapping("/")
-ãã®æ³šéã䜿çšããŠããã®ã³ã³ãããŒã©ãŒã®ã¹ã³ãŒãïŒãã®æ³šéãã¯ã©ã¹ã«é©çšãããå ŽåïŒãŸãã¯ç¹å®ã®å€ææžã¿ã¢ãã¬ã¹ïŒã¡ãœããã®å ŽåïŒãæå®ããŸãã ãŸãããã®ã¢ãã¬ã¹ãŸãã¯ãã®ã¢ãã¬ã¹ããããŒããã£ã¹ããããç¹å®ã®èŠæ±ãæå®ããããšãã§ããŸãïŒã¡ãœãããã©ã¡ãŒã¿ãŒïŒã ãã®å Žåããã®ã³ã³ãããŒã©ãŒã¯ãã¹ãŠã®ã¢ãã¬ã¹ãïŒã€ãŸãããã©ãŠã¶ãŒã§ïŒè¡šç€ºããããããç®çã®ããŒãžã«å€æããããšãããšããäºå®ã«ã€ããŠè©±ããŸãã æ確ã«ããããã«ãäŸãæããŸãã
2ã€ã®ã¢ãã¬ã¹mydomain.com/infoãmydomain.com/aboutããããšããŸãã "/"-"/ about"ã®ä»£ããã«RequestMappingã§æå®ãããšãã³ã³ãããŒã©ãŒã¯ã¢ãã¬ã¹mydomain.com/infoãå€æããããšããŸããã
printWelcomeïŒïŒã¡ãœããã®æ©èœã«ã€ããŠã ãã®ã¡ãœããã¯ãåã«ã¡ãã»ãŒãžãHello WorldããããŒãžã«éä¿¡ãããã¡ãããããŒãžèªäœã®ååã瀺ããŸãã ããŠããã£ã¹ãããã£ãŒã¯ã©ã¹ïŒMvcConfigïŒã§èª¬æãããã¥ãŒã¢ãŒã®å©ããåããŠããã®ååã¯ãã¬ãã£ãã¯ã¹ãšãµãã£ãã¯ã¹ãè¿œå ããããšã§ããŒãžãžã®ãã«ãã¹ã«å€æãããŸãã
hello.jspããŒãžèªäœã®ã³ãŒãã®ã¿ãæäŸããŸãã
<html> <body> <h1>${message}</h1> </body> </html>
å®éãã¢ããªã±ãŒã·ã§ã³ã®ãããã€ãéå§ã§ããŸãã ãããè¡ãã«ã¯ãã¹ã¿ãŒããã¿ã³ã䜿çšããŸãã
ãµãŒããŒãèµ·åããã¢ããªã±ãŒã·ã§ã³ãå±éãããŸã§åŸ æ©ããŸãã ãããŠåºæ¥äžãããç§ãã¡ã¯çã®æãããHello WorldããèŠãŸãã
ã¹ã¯ãªãŒã³ã·ã§ãã
ãã¹ãŠãæ£åžžã«æ©èœããããã«ãªã£ããã2ã€ã®æ°å€ã®åèšã®èšç®ãã¹ãå®è£ ãå§ããããšãã§ããŸãã
ãŸããjspããŒãžã«çªå·ãå ¥åããããã®ãã©ãŒã ãè¿œå ããŠãjspããŒãžãå€æŽããŸãã
<html> <body> <h1>${message}</h1> <form method="post"> <input name="value1" type="text"/> + <input name="value2" type="text"/> <input type="submit"/> </form> <h3>Result: ${result}</h3> </body> </html>
è€éãªããšã¯äœããããŸããã æ°åãå ¥åããããã®2ã€ã®ãã£ãŒã«ããéä¿¡ãã¿ã³ãšçµæãåºåããããã®ãã£ã¬ã¯ãã£ããè¿œå ããŸããã ç°¡åã«ããããã«ãåãããŒãžã䜿çšããŠçµæã衚瀺ããããšã«ããŸããã
æåŸã«ãPOSTèŠæ±ãåŠçããã¡ãœãããã³ã³ãããŒã©ãŒã«è¿œå ããŸãã
package com.springapp.controllers; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/") public class HelloController { @RequestMapping(method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("message", "Hello world!"); return "hello"; } @RequestMapping(method = RequestMethod.POST) public String calcSum(Model m, @RequestParam(value = "value1") Double value1, @RequestParam(value = "value2") Double value2 ){ m.addAttribute("result", (value1+value2)); return "hello"; } }
ãã®ã¡ãœããã§ã¯ã
@RequestMapping
ã¢ãããŒã·ã§ã³ã䜿çšããŠPOSTåŠçã¡ãœãã
@RequestMapping
æå®ãã
@RequestParam
ã¢ãããŒã·ã§ã³
@RequestParam
䜿çšããŠãã©ãŒã ãã£ãŒã«ãïŒvalue1ãvalue2ïŒããæ å ±
@RequestParam
ååŸããŸãã
éå§ããŸãïŒã¢ã€ãã¢ã«ã¯4ã€ã®ãªãã·ã§ã³ãããããåãããã€ããéžæããŸãïŒããã©ãŒã ã衚瀺ãããŸãã
ã¹ã¯ãªãŒã³ã·ã§ãã
ããŠãéãèæ ®ããŸãã
ã¹ã¯ãªãŒã³ã·ã§ãã
å®éãããã ãã§ãã
次åã¯ãSpring Data JPAã®æŠèŠãšãã®äœ¿çšæ¹æ³ã«ã€ããŠèª¬æããŸãã
ç§ãåŒçšãããã¹ãŠã®æ å ±ã¯ãçŽç²ã«ç§ã®çµéšãŸãã¯æèŠã§ãããæšæºãŸãã¯æšæºã§ãããšäž»åŒµãããã®ã§ã¯ãããŸããã å¿ èŠãªæ¹æ³ã§ã¯ãªããã©ã®ããã«è¡ããããã瀺ããŸãã
ãèšäºã®æ§ç¯ãã®åéã§ã®ç§ã®çµéšã¯ããã»ã©å€§ãããªãã®ã§ã建èšçãªæ¹å€ãåãã§ããŸãã
ã¿ãªãããããããªãããŸãåŸã§ã