18 Haziran 2015 Perşembe

Spring Framework Web Application Tutorials 1 - The Initial Setup & Hello World

You should have JRE, Maven, Tomcat, Eclipse (and preferable Git) installed. 


In Eclipse, in Package Explorer

  1. right click
  2. new
  3. other
  4. Maven
  5. Maven Project
  6. maven-archetype-webapp

In pom.xml include the dependency below


<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>


Let's include Spring Framework and Spring MVC dependencies (into pom.xml)

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>


Let's create the DispatcherServlet (which inherits from HttpServlet base class) and map the requests that we want our DispatcherServlet to handle. There are two ways to do this:

  1. web.xml
  2. A new class that implements WebApplicationInitializer (I choose this way)
While we create DispatcherServlet, we also create WebApplicationContext and tie to the servlet. While we are creating WebApplicationContext, we will tell it the Class (in my case: WebConfig.class) to look for the Bean definitions.

WebApplicationInitializer is an interface provided by Spring MVC that ensures your code-based configuration is detected and automatically used to initialize any Servlet 3 container. 

In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

The WebApplicationContext is an extension of the plain ApplicationContext (which is a Bean Factory with additional features) that has some extra features necessary for web applications. It differs from a normal ApplicationContext in that it is capable of resolving themes, and that it knows which Servlet it is associated with (by having a link to the ServletContext). The WebApplicationContext is bound in the ServletContext, and by using static methods on the RequestContextUtils class you can always look up the WebApplicationContext if you need access to it.

MyWebApplicationInitializer.java
package com.quizcv.web.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MyWebApplicationInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebConfig.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1); }
}

WebConfig.java
package com.quizcv.web.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.quizcv")
public class WebConfig extends WebMvcConfigurerAdapter {
}

HelloWorldController.java

package com.quizcv.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloWorldController {
    @ResponseBody
    @RequestMapping("/helloWorld")
    public String helloWorld(Model model) {
        model.addAttribute("message", "Hello World!");
        return "helloWorld";
    }
}

The final project structure:




The Result:




Hiç yorum yok:

Yorum Gönder