Spring/3.x
[펌]Getting started with annotation based Spring MVC web application
EnterKey
2016. 1. 10. 12:22
Here is a minimal way to get a Spring 3 MVC project started with Maven.
First create spring-web-annotation/pom.xml
file and include the Spring dependency:
01 | <?xml version= "1.0" encoding= "UTF-8" ?> |
08 | <modelVersion> 4.0 . 0 </modelVersion> |
09 | <groupId>spring-web-annotation</groupId> |
10 | <artifactId>spring-web-annotation</artifactId> |
11 | <version> 1.0 -SNAPSHOT</version> |
12 | <packaging>war</packaging> |
16 | <groupId>javax.servlet</groupId> |
17 | <artifactId>javax.servlet-api</artifactId> |
18 | <version> 3.1 . 0 </version> |
19 | <scope>provided</scope> |
22 | <groupId>org.springframework</groupId> |
23 | <artifactId>spring-webmvc</artifactId> |
24 | <version> 3.2 . 4 .RELEASE</version> |
Now create the Servlet 3 web initializer and the Spring annotation config for the MVC parts in spring-web-annotation/src/main/java/springweb/WebApp.java
03 | import org.springframework.context.annotation.ComponentScan; |
04 | import org.springframework.context.annotation.Configuration; |
05 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
06 | import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; |
08 | public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer { |
10 | protected Class<?>[] getRootConfigClasses() { |
11 | return new Class<?>[ 0 ]; |
15 | protected Class<?>[] getServletConfigClasses() { |
16 | return new Class<?>[]{ WebAppConfig. class }; |
20 | protected String[] getServletMappings() { |
21 | return new String[]{ "/" }; |
26 | @ComponentScan ( "springweb.controller" ) |
27 | public static class WebAppConfig { |
The WebApp
class extends Spring’s built in Servlet3 web initializer code. It allows Servlet3 container such as Tomcat7 to auto detect this web application without the need of web.xml
configuration setup. Because of we do not use web.xml
, we need this class to allow Spring to hook into the Servlet container to bootstrap their dispatcher servlet. Also instead of typical Spring beans xml file configuration, we now can use all annotation based using WebAppConfig
.
Noticed that I have combined the WebAppConfig
as inner class, but you can easily move it out as top level class in a full scale application. This is the Spring annotation version of container configuration. You can easily customize the application by adding new @Bean
here.
Note: Do not forget to overwrite getServletMappings
method with "/"
, or else your URL request will not direct to the Spring dispatcher for processing! A step that can easily forgotten and you might find your self chasing why Spring controllers are not working.
Above are really the minmal setup you need to start a war project. Next you want to add at least one controller to have some output to verify. Create this controller file spring-web-annotation/src/main/java/springweb/controller/IndexController.java
01 | package springweb.controller; |
03 | import org.springframework.stereotype.Controller; |
04 | import org.springframework.web.bind.annotation.RequestMapping; |
07 | public class IndexController { |
08 | @RequestMapping (value= "/" ) |
09 | public String index() { |
And now you would need the JSP view spring-web-annotation/src/main/webapp/index.jsp
Now cd into spring-web-annotation
and execute mvn org.apache.tomcat.maven:tomcat7-maven-plugin:run
. You should see your Spring application starts and able to browse http://localhost:8080/spring-web-annotation
URL.
There are lot of cool stuff you can do with Spring MVC. Checkout their awesome docs for more details.
출처: http://www.javacodegeeks.com/2013/10/getting-started-with-annotation-based-spring-mvc-web-application.html?utm_content=bufferce4af&utm_medium=social&utm_source=facebook.com&utm_campaign=buffer