web application with spring mvc example

Web Application With Spring MVC Example
Hello Readers, in this tutorial we are going to see how to create a web application with spring MVC (Model View Controller).

Basically spring MVC is a java framework which is used to create a web applications. It is has all the basic features of core spring so don’t think too much about it.

The spring web MVC framework is designed around DispatcherServlet class (which extends FrameWorkServlet) that dispatches the requests to handlers, with configurable handler mappings, view resolution, locale and theme resolutions as well as support for uploading files(ref : spring 17.1 part V).

In a simple word we may say DispatcherServlet is a class that receives the incoming request and maps it to the appropriate controllers which resolve the views.

So let’s begin by creating a model or entity class.

1 : Alien.java  
package com.ma3.model;

import java.io.Serializable;

public class Alien implements Serializable {
     private static final long serialVersionUID = 1L;

     private Integer id;
     private String name;
     private String planet;

      * @return the id
     public Integer getId() {
          return id;

      * @param id the id to set
     public void setId(Integer id) {
          this.id = id;

      * @return the name
     public String getName() {
          return name;

      * @param name the name to set
     public void setName(String name) {
          this.name = name;

      * @return the planet
     public String getPlanet() {
          return planet;

      * @param planet the planet to set
     public void setPlanet(String planet) {
          this.planet = planet;

     public String toString() {
          return "Alien [id=" + id + ", name=" + name + ", planet=" + planet + "]";


2 : Now add the following dependencies in pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">




          <!-- Spring MVC support -->


          <!-- Tag libs support for view layer -->




3 : Now create AlienDao.java interface.
package com.ma3.dao;
import java.util.List;
import com.howtodoinjava.demo.model.Alien;
public interface AlienDAO {
            public List<Alien> getAllAliens();

4 : Now create AlienDaoImpl.java (annotated with @Repository) class which implement ALienDao.java interface.
package com.ma3.dao;
import java.util.ArrayList;
import org.springframework.stereotype.Repository;
import com.howtodoinjava.demo.model.Alien;
public class AlienDAOImpl implements AlienDAO {
            public List<Alien> getAllAliens() {
                        List<Alien> aliens = new ArrayList<Alien>();
                        Alien alien1 = new Alien();

                        Alien alien2 = new Alien();
                        return aliens;

5 : Now create  AlienManager.java interface.
package com.ma3.AlienManager;
import java.util.List;
import com.howtodoinjava.demo.model.Alien;
public interface AlienManager {
            public List<Alien> getAllAliens();

6 : Now create AlienManagerImpl.java class (which is annotated with @Service annotation) which implements AlienManager.java interface.
package com.ma3.AlienManager;

import  java.util.List;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.stereotype.Service;
import com.howtodoinjava.demo.dao.AlienDAO;
import com.howtodoinjava.demo.model.Alien;
public class AlienManagerImpl implements AlienManager {
            AlienDAO empDao;
            public List<Alien> getAllAliens() {
                        return empDao.getAllAliens();

7 : Now create AlienController.java which is annotated with @Controller annotation which indicates that it is a controller of our application and DispatcherServlet class will looks for it.
package com.ma3.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.howtodoinjava.demo.AlienManager.AlienManager;
public class AlienController {
     AlienManager manager;

     @RequestMapping(value = "/getAllAliens", method = RequestMethod.GET)
     public String getAllAliens(Model model) {
          model.addAttribute("aliens", manager.getAllAliens());
          return "displayAlien";

8 : WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">


9 : WEB-INF/spring-applicationContext-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>

    <context:annotation-config />
    <context:component-scan base-package="com.ma3" />
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />


10 : WEB-INF/views/displayAlien.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<title>Spring MVC Hello Alien</title>

              <h2>All Aliens</h2>

              <table border="3" width="20%">
                   <c:forEach items="${aliens}" var="aliens">

12 : WEB-INF/index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<a href="getAllAliens">Show Aliens</a>

13 : Now run the application (I am using tomcat 9.0).
Go to this URL : http://localhost:8080/TestWebb/ the following default index.jsp will be displayed with a link called Show Alien, click on that link to see all aliens.


When you click on the Show Aliens the below screen will be displayed.

Tested on eclipse 2019-06, GOOD LUCK.

In the next tutorial we will see the same example with spring and hibernate integration.

No comments

You May Like Also

Programming Knowledge

Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database