A Simple Spring MVC Example Using Maven



Now we are going to create a simple Spring mvc example using Maven.

Let's check how it's done.

Step-1 : 

First Open the Eclipse IDE. Click New > Maven Project > select an archetype of webapp for Spring mvc project > Enter Group Id, artifact Id > Click Finish.


         







                                 
                                                



 Step - 2:

         You Can Notice an error showing on your project. You have to add your server in to it as follows.

  • Right-click on the Project. 

  •  Go to Properties > Targeted Runtimes > Select a Runtime.

  •  Click Apply and Close.

In my case i'm using Tomcat Server.

 


 

 Step - 3:

First add dependencies in your pom.xml file.

   <dependencies>

  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>5.2.2.RELEASE</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>5.2.2.RELEASE</version>

</dependency>

 </dependencies>


 Maven will take the responsibility of downloading all the dependencies. It  may take sometime to download all the dependencies.


Step - 4:

 Now modify the index.jsp file with the following code.

                     <html>

<form action="display">

<input type="text" name="t1"/>

<input type="submit"/>

</form>

</body>

</html>

Now create a java file DisplayContainer.java in src/main/java folder. We are creating a java file which is a controller ( @Controller ) and mapping to the index.jsp file using (@RequestMapping("/display")).

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

 

@Controller

public class DisplayController {


@RequestMapping("/display")

public ModelAndView display(@RequestParam("t1") String a){

ModelAndView mv = new ModelAndView();

mv.addObject("res", a);

mv.setViewName("result.jsp")

return mv;

}

}


Create another java class to make Configuration which configures everything which file has to be processed after clicking the submit button.

 MyFrontController.java : 

 

 import org.springframework.web.servlet.support.AbstractAnnotation ConfigDispatcherServletInitializer

public class MyFrontController extends                                                                                    AbstractAnnotationConfigDispatcherServletInitializer  {

@Override

protected Class<?>[] getRootConfigClasses() {

return new Class[] {MvcConfig.class};

}


@Override

protected Class<?>[] getServletConfigClasses() {

return null;

}


@Override

protected String[] getServletMappings() {

return new String[] {"/"};

        }

}


 MvcConfig.java : 

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;


@EnableWebMvc

@Configuration

@ComponentScan({"com.springmvc"})

public class MvcConfig {

                                        }


 result.jsp :

   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1" isELIgnored="false"%>

<!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>Result Page</title>

</head>

<body>

Hello ${res}

</body>

</html>

 

Step - 5 :

 At last, run the index.jsp file using server whatever you like to work with.

                                    

                            



 




Now you can see the we can run the project without any error by using Spring mvc and maven.