Chapter 8. Using Spring MVC 4.1

The Spring framework is based on the Inversion of Control (IoC) principle, in which the objects define their own dependencies. Dependency Injection is a form of IoC. The container injects the dependencies when the bean is created. The dependency injection is implemented using the BeanFactory and ApplicationContext interfaces. The BeanFactory interface is for managing beans and ApplicationContext (WebApplicationContext is used for a web application) is for configuring an application. BeanFactory applies the IoC pattern to separate configuration and dependency specifications. The ApplicationContext interface provides extended (extends) features to BeanFactory.

The Spring MVC framework is based on the Model-View-Controller design pattern and is implemented using DispatcherServlet. DispatcherServlet fields requests and delegates them to a request handler using web request URI mapping provided by the @RequestMapping annotation. The request handler or Controller returns Model and View using the ModelAndView holder. In this chapter, we will create a Spring MVC application in Eclipse, compile and package the application using the Maven build tool, and deploy the application to WildFly 8.1. Subsequently, we will run the Spring application in a browser. In this chapter, we will cover the following topics:

  • Setting up the environment
  • Creating a Spring MVC project
  • Creating a MySQL data source
  • Creating a JPA configuration file
  • Creating the Model
  • Creating the Data Access Object design pattern
  • Creating a web descriptor
  • Creating the request handler
  • Creating the View
  • Creating a Spring MVC context
  • Deploying the Spring project with Maven
  • Creating deployment structure and infrastructure deployment descriptors
  • Installing the Spring MVC Maven project
  • Running the Spring application

Setting up the environment

We need to install the following software:

Set the JAVA_HOME, JBOSS_HOME, MAVEN_HOME, and MYSQL_HOME environment variables. Add %JAVA_HOME%/bin, %MAVEN_HOME%/bin, %JBOSS_HOME%/bin, and %MYSQL_HOME/bin to the PATH environment variable.

Create a WildFly 8.1.0 runtime as discussed in Chapter 1, Getting Started with EJB 3.x. Create a MySQL data source with the JNDI name java:jboss/datasources/MySQLDS as explained in Chapter 1, Getting Started with EJB 3.x.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.233.245