Spring MVC Example

Spring MVC Example

We have previously discussed the Spring Web MVC framework. In this tutorial, we are going to create a simple example of Spring Web MVC.

Following are the steps used to create an example of Spring MVC:

Step 1: Add the following maven dependencies into pom.xml or download the Spring jar files.

pom.xml


org.springframework
spring-webmvc
5.1.8.RELEASE


javax.servlet
javax.servlet-api
4.0.0
provided
 

Step 2:  Create a web request page index.jsp that contains two links.

index.jsp

Spring MVC Web application
Home page | About Us

Step 3: Create a Controller class with the name MainController. In the controller class we have used two annotations: @Controller and @RequestMapping.

  • @Controller: The annotation @Controller is used to indicate a class as a “Controller.”
  • @RequestMapping: The annotation @RequestMapping is used to map the HTTP requests to handler methods of MVC.

MainController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MainController {
@RequestMapping("/home_page")
public String showPage() {
return "homepage" ;
}
@RequestMapping("/about_us")
public String showPage2() {
return "about" ;
}
} 

Step 4: Add the entry of the Controller into web.xml. In the web.xml file, we are going to declare one Servlet (DispatcherServlet) to receive web requests. 

web.xml

springMVC 
  


  
  dispatcher
  org.springframework.web.servlet.DispatcherServlet
  
  contextConfigLocation
  /WEB-INF/spring-servlet.xml
  
  1
  


  dispatcher
  /

Step 5: Add the entry of bean into different XML (spring-servlet.xml) file. We can give any name to the bean xml file.In the spring-servlet.xml file, we are going to use the view resolver with the view component. The InternalResourceViewResolver class is used for the view resolver.

spring-servlet.xml

http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd" >

Step 6: Create JSP files that we have used in the controller class. We have used homepage.jsp and about.jsp pages.

homepage.jsp

Home Page of Spring MVC

about.jsp

About Spring MVC
Spring MVC is a web Model-View-Controller framework built on Servlet API which is used to build web applications.

Step 7: Run the project on server.

Output

Run the project on server.

When we click on the Home page, it returns the following response.

When we click on the Home page,

When we click on the About Us, it returns the following response.

When we click on the About Us

Directory Structure of Spring MVC example

The following image shows the directory structure of Spring MVC.

Directory Structure of Spring MVC example

Related Topics

Spring AOP – @Around Advice

Spring AOP – @Around Advice We have discussed in our previous tutorial, the concepts, and terminology of the Spring AOP. Around advice is one of the types of advices. It can be applied to...

4 minutes read.

Spring MVC Example – Reading HTML Form Data

We have discussed earlier a simple example of Spring MVC in which we have created multiple view pages. Here, we are going to create another Spring MVC example in which we are...

7 minutes read.

Spring Autowiring – Setter Injection

Spring Autowiring – Setter Injection We have previously discussed an example of Setter Dependency Injection (SDI) using an XML file. In the previous case, we have defined the property name for the dependency...

2 minutes read.

Spring MVC Form Checkbox

Spring MVC provides several form tags used to develop web applications (Usually JSP pages). One of them is a checkbox tag. The checkbox tag offers us to choose multiple options at the same...

12 minutes read.

Spring AOP – Pointcut Expressions

Spring AOP – Pointcut Expressions Pointcut refers to a collection of join points that specify where advice to be applied. In other words, pointcut represents a set of various join points. It defines that...

7 minutes read.

Spring Autowiring – Constructor Injection

Spring Autowiring – Constructor Injection We have previously discussed an example of Constructor Dependency Injection (CDI) using an XML-based configuration. In the previous case, we have defined the reference of the constructor in...

2 minutes read.

Spring MVC Form Tag Library

The Spring MVC provides a variety of tags available in the form of a tag library, which is used to develop web pages (JSP view pages). The Spring’s form tag...

6 minutes read.

Spring Autowiring

Spring Autowiring Autowiring is an essential feature of the Spring framework. The autowiring function is used for wiring beans automatically, without injecting the beans using <constructor-arg> and <property> elements in the bean-config file. It can...

3 minutes read.

Spring – Configuration using @Configuration

Spring – Configuration using @Configuration We have previously discussed the Spring configuration using an XML configuration file. In that case, we have defined all the bean information in the bean-configuration file (applicationContext.xml). ...

3 minutes read.

Spring MVC Form – Drop-Down List

We have already discussed the Spring MVC form tags available in the form tag library. Form tags are used to develop web applications easily. In this tutorial, we are going to discuss the...

13 minutes read.

Spring AOP – @AfterReturning Advice

Spring AOP – @AfterReturning Advice We have discussed earlier the types of advices available in Spring AOP. One of them is After Returning advice. It is an advice that executes after the normal execution...

3 minutes read.

Spring Autowiring –Field Injection

Spring Autowiring –Field Injection The Field Injection is used to inject the fields, variables, or properties of the beans. It is not available in autowiring using XML- based configuration. It is a...

2 minutes read.

Spring AOP - @AfterThrowing Advice

Spring AOP - @AfterThrowing Advice As we have discussed in our previous tutorial, Advice is one of the essential concepts of the Spring AOP. The after throwing advice is one of the types of...

3 minutes read.

Spring MVC Form Validation

Spring Validation is used to validate the @Controller inputs. It is used to check and accept the user input in any web application. Using Spring validation, we can validate the user input on...

12 minutes read.

Spring - Bean Scope

Spring - Bean Scope Bean Scope refers to a lifecycle of the bean. It allows us to have control over the creation of bean objects. It is a very powerful approach and provides...

6 minutes read.

Spring MVC Example

Spring MVC Example We have previously discussed the Spring Web MVC framework. In this tutorial, we are going to create a simple example of Spring Web MVC. Following are the steps used to create an example...

2 minutes read.

Spring MVC Tutorial

Spring MVC is a web Model-View-Controller framework built on Servlet API. It is used to build web applications. The MVC framework works around central servlet called DispatcherServlet. It dispatches the requests to the...

4 minutes read.

Spring - Bean Scopes with Annotations

Spring - Bean Scopes with Annotations We have previously discussed the Spring bean scopes using an XML-based configuration. In the XML-based configuration, we have defined the beans and its scope inside the bean-config...

3 minutes read.

Spring Model Interface

Model Interface Model is an interface that defines a holder for model attributes. It is also used to transfer data between various parts of the Spring MVC application. The Model interface is available...

8 minutes read.

Spring - Inversion of Control (IoC)

Spring - Inversion of Control (IoC) The Inversion of Control (IoC) is a process where the objects define their dependencies, that is, the dependencies of other objects with they are working. It is done...

4 minutes read.