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"?>
04    xsi:schemaLocation="
05        http://maven.apache.org/POM/4.0.0
06        http://maven.apache.org/maven-v4_0_0.xsd">
07 
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>
13 
14    <dependencies>
15        <dependency>
16            <groupId>javax.servlet</groupId>
17            <artifactId>javax.servlet-api</artifactId>
18            <version>3.1.0</version>
19            <scope>provided</scope>
20        </dependency>
21        <dependency>
22            <groupId>org.springframework</groupId>
23            <artifactId>spring-webmvc</artifactId>
24            <version>3.2.4.RELEASE</version>
25        </dependency>
26    </dependencies>
27</project>

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

01package springweb;
02 
03import org.springframework.context.annotation.ComponentScan;
04import org.springframework.context.annotation.Configuration;
05import org.springframework.web.servlet.config.annotation.EnableWebMvc;
06importorg.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
07 
08public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
09    @Override
10    protected Class<?>[] getRootConfigClasses() {
11        return new Class<?>[0];
12    }
13 
14    @Override
15    protected Class<?>[] getServletConfigClasses() {
16        return new Class<?>[]{ WebAppConfig.class };
17    }
18 
19    @Override
20    protected String[] getServletMappings() {
21        return new String[]{ "/" };
22    }
23 
24    @Configuration
25    @EnableWebMvc
26    @ComponentScan("springweb.controller")
27    public static class WebAppConfig {
28    }
29}

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

01package springweb.controller;
02 
03import org.springframework.stereotype.Controller;
04import org.springframework.web.bind.annotation.RequestMapping;
05 
06@Controller
07public class IndexController {
08    @RequestMapping(value="/")
09    public String index() {
10        return "index";
11    }
12}

And now you would need the JSP view spring-web-annotation/src/main/webapp/index.jsp

1Hello World.

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


반응형