In this recipe, we will build a simple Spring web application with Eclipse. We will:
Create a new Maven project
Add Spring to it
Add two Java classes to configure Spring
Create a "Hello World" web page
In the next recipe, we will compile and run this web application.
In this section, we will create a Spring web application in Eclipse.
Under Maven, select Maven Project and click on Next >.
Select the Create a simple project (skip archetype selection) checkbox and click on Next >.
For the Group Id field, enter
com.springcookbook
. For the Artifact Id field, enterspringwebapp
. For Packaging, selectwar
and click on Finish.
Open Maven's pom.xml
configuration file at the root of the project. Select the pom.xml tab to edit the XML source code directly. Under the project
XML node, define the versions for Java and Spring. Also add the Servlet API, Spring Core, and Spring MVC dependencies:
<properties> <java.version>1.8</java.version> <spring.version>4.1.5.RELEASE</spring.version> </properties> <dependencies> <!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- Spring Core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
Create the Java packages
com.springcookbook.config
andcom.springcookbook.controller
; in the left-hand side pane Package Explorer, right-click on the project folder and select New | Package….In the
com.springcookbook.config
package, create theAppConfig
class. In the Source menu, select Organize Imports to add the needed import declarations:package com.springcookbook.config; @Configuration @EnableWebMvc @ComponentScan (basePackages = {"com.springcookbook.controller"}) public class AppConfig { }
Still in the
com.springcookbook.config
package, create theServletInitializer
class. Add the needed import declarations similarly:package com.springcookbook.config; public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[0]; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{AppConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
This section will give more you details of what happened at every step.
The generated Maven project is a pom.xml
configuration file along with a hierarchy of empty directories:
pom.xml src |- main |- java |- resources |- webapp |- test |- java |- resources
The declared Maven libraries and their dependencies are automatically downloaded in the background by Eclipse. They are listed under Maven Dependencies in the left-hand side pane Package Explorer.
Tomcat provides the Servlet API
dependency, but we still declared it because our code needs it to compile. Maven will not include it in the generated .war
file because of the <scope>provided</scope>
declaration.
AppConfig
is a Spring configuration class. It is a standard Java class annotated with:
@Configuration
: This declares it as a Spring configuration class@EnableWebMvc
: This enables Spring's ability to receive and process web requests@ComponentScan(basePackages = {"com.springcookbook.controller"})
: This scans thecom.springcookbook.controller
package for Spring components
ServletInitializer
is a configuration class for Spring's servlet; it replaces the standard web.xml
file. It will be detected automatically by SpringServletContainerInitializer
, which is automatically called by any Servlet 3. ServletInitializer
extends the AbstractAnnotationConfigDispatcherServletInitializer
abstract class and implements the required methods:
getServletMappings()
: This declares the servlet root URI.getServletConfigClasses()
: This declares the Spring configuration classes. Here, we declared theAppConfig
class that was previously defined.
We created a controller class in the com.springcookbook.controller
package, which we declared in AppConfig
. When navigating to http://localhost:8080/hi
, the hi()
method will be called and Hello, world. will be displayed in the browser. This will be explained further in Chapter 3, Using Controllers and Views.