How to Create Spring MVC application using Java Based Configuration

In our earlier tutorial we have seen How to Create Spring MVC application using Spring Tool Suite(STS). Earlier all Spring MVC application will be XML based configuration, but now all new Spring MVC applications are based on Java Configuration. In this tutorial we will see how to create Spring MVC application using Java Based Configuration.

How to Create Spring MVC application using Java Based Configuration

How to Create Spring MVC using Java Based Configuration

Pre-requiste to Create Spring MVC application using Java Based Configuration:

1. Eclipse IDE (Any Latest IDE)
2. No Jars is needed, Since we are using Maven

Steps – How to Create Spring MVC application using Java Based Configuration

Step 1: Create a Maven Project

1.a. File -> New -> Other -> Maven -> Maven Project -> Next -> Next -> Type Filter as maven-archetype-webapp -> Select maven-archetype-webapp -> Next
1.b. Specify Group ID and Artifact Id and give Finish

"<yoastmark

 

 

Step 2: We will get a project structure as specified below.

How to Create Spring MVC application using Java Based Configuration - Project Structure

How to Create Spring MVC using Java Based Configuration – Project Structure

Step 3: Configure Buildpath : Remove old JRE and Update to new JRE

         3.a Remove old JRE System Library(In your case you might already have latest JRE)

How to Create Spring MVC application using Java Based Configuration - Add JRE 1.8

How to Create Spring MVC application using Java Based Configuration – Remove Old JRE

3.b Add new JRE(1.8 JRE)

How to Create Spring MVC application using Java Based Configuration - Add JRE 1.8 - 1

How to Create Spring MVC application using Java Based Configuration – Add JRE 1.8

Step 4: New Project Structure after updating to new EAR

How to Create Spring MVC application using Java Based Configuration - Maven New Project Structure

How to Create Spring MVC application using Java Based Configuration – Maven New Project Structure

Step 5: Update pom.xml as mentioned below, when you see this tutorial you might be seeing older Spring Version. Get the latest Spring Version from here.

<?xml version="1.0"?>
<project
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

 <modelVersion>4.0.0</modelVersion>
 <groupId>com.learnfromexamples.springmvcjavaconfig</groupId>
 <artifactId>SpringMVCJavaConfig</artifactId>
 <packaging>war</packaging>
 <version>1.0.0</version>
 <name>SpringMVCJavaConfig</name>

 <properties>
 <springframework.version>4.3.7.RELEASE</springframework.version>
 </properties>

 <dependencies>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>${springframework.version}</version>
 </dependency>

 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>javax.servlet-api</artifactId>
 <version>3.1.0</version>
 </dependency>
 <dependency>
 <groupId>javax.servlet.jsp</groupId>
 <artifactId>javax.servlet.jsp-api</artifactId>
 <version>2.3.1</version>
 </dependency>
 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>jstl</artifactId>
 <version>1.2</version>
 </dependency>
 </dependencies>

 <build>
 <pluginManagement>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>3.2</version>
 <configuration>
 <source>1.6</source>
 <target>1.6</target>
 </configuration>
 </plugin>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-war-plugin</artifactId>
 <version>2.4</version>
 <configuration>
 <warSourceDirectory>src/main/webapp</warSourceDirectory>
 <warName>SpringMVCJavaConfig</warName>
 <failOnMissingWebXml>false</failOnMissingWebXml>
 </configuration>
 </plugin>
 </plugins>
 </pluginManagement>
 <finalName>SpringMVCJavaConfig</finalName>
 </build>
</project>

Step 6: Delete web.xml from WEB-INF folder. Since we are configuring this application based on Java Configuration, we will be removing all the xml files.

Step 7: Configure spring-config.xml through Java Way. For Spring MVC application, we used to specify all the dependencies in xml. Now we will configure all through Java Based Configuration. Create a package “com.learnfromexamples.springmvcjavaconfig.configuration” and Java file “SpringMVCConfiguration” by extending WebMvcConfigurerAdapter class.

To configure the Jsp file, override the configureViewResolvers


package com.learnfromexamples.springmvcjavaconfig.configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.learnfromexamples.springmvcjavaconfig")
public class SpringMVCConfiguration extends WebMvcConfigurerAdapter {
 @Override
 public void configureViewResolvers(ViewResolverRegistry registry) {
 InternalResourceViewResolver internalResourceViewResolver
 = new InternalResourceViewResolver();
 internalResourceViewResolver.setViewClass(JstlView.class);
 internalResourceViewResolver.setPrefix("/WEB-INF/views/");
 internalResourceViewResolver.setSuffix(".jsp");
 registry.viewResolver(internalResourceViewResolver);
 }
}


Step 8: Configure web.xml in Java way. Create a Java package for web.xml configurations. I have created com.learnfromexamples.springmvcjavaconfig.configuration package and Java File SpringMVCInitializer. By extending AbstractAnnotationConfigDispatcherServletInitializer and overriding all the methods we are specifying the web.xml file configuration in Java way. Also delete the web.xml file.

package com.learnfromexamples.springmvcjavaconfig.configuration;

import org.springframework.web.servlet.support.
 AbstractAnnotationConfigDispatcherServletInitializer;

public class SpringMVCInitializer extends 
 AbstractAnnotationConfigDispatcherServletInitializer {

 @Override
 protected Class<?>[] getRootConfigClasses() {
 return new Class[]{SpringMVCConfiguration.class};
 }

 @Override
 protected Class<?>[] getServletConfigClasses() {
 return null;
 }

 @Override
 protected String[] getServletMappings() {
 return new String[]{"/"};
 }

}

Step 9: Create a folder called views inside WEB-INF folder (/WEB-INF/views) and delete all the views(.jsp file) which is inside WEB-INF folder.

Step 10: Create a package for Controller(s). I have created “com.learnfromexamples.springmvcjavaconfig.controller” package and created SpringMVCController.java for Controller.


package com.learnfromexamples.springmvcjavaconfig.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class SpringMVCController {
 @RequestMapping(method = RequestMethod.GET)
 public String greeting(ModelMap map){
 map.addAttribute("greetMessage", "Hello!!! Learn From Examples!!!! LFX!!!");
 return "welcome";
 }
}


Step 11: Create welcome.jsp inside view folder (/WEB-INF/views/welcome.jsp)


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>How to Create Spring MVC - Java Based Configuration</title>
</head>
<body>
<h1>Greeting : ${greetMessage}</h1>
</html>
How to Create Spring MVC application using Java Based Configuration - Final Project Structure

How to Create Spring MVC application using Java Based Configuration – Final Project Structure

Step 12: Create and Project Project to the Server as mentioned in below images and Start the server.

How to Create Spring MVC application using Java Based Configuration - Add Server

How to Create Spring MVC application using Java Based Configuration – Add Server

How to Create Spring MVC application using Java Based Configuration - Select Server

How to Create Spring MVC application using Java Based Configuration – Select Server

How to Create Spring MVC application using Java Based Configuration - Add Project to Server

How to Create Spring MVC application using Java Based Configuration – Add Project to Server

How to Create Spring MVC application using Java Based Configuration - Start Server

How to Create Spring MVC application using Java Based Configuration – Start Server

 

Open Browser and hit the Below URL

http://localhost:7171/SpringMVCJavaConfig/

How to Create Spring MVC application using Java Based Configuration - Output

How to Create Spring MVC application using Java Based Configuration – Output

 

In this tutorial we have seen How to Create Spring MVC application using Java Based Configuration. If you have any questions or doubts , please post it in the comment box. If you like this tutorial, please share it with your friends.