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;
}
@Override
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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestWebb</groupId>
<artifactId>TestWebb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<org.springframework.version>4.2.6.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring MVC support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Tag libs support for view layer -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<testSourceDirectory>src/main/test</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
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;
importjava.util.List;
import org.springframework.stereotype.Repository;
import com.howtodoinjava.demo.model.Alien;
@Repository
public class AlienDAOImpl implements AlienDAO {
public List<Alien> getAllAliens() {
List<Alien>
aliens = new ArrayList<Alien>();
Alien
alien1 = new Alien();
alien1.setId(1);
alien1.setName("Alien1");
alien1.setPlanet("Mars");
aliens.add(alien1);
Alien
alien2 = new Alien();
alien2.setId(2);
alien2.setName("Alien2");
alien2.setPlanet("X-Planet");
aliens.add(alien2);
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;
@Service
public class AlienManagerImpl
implements AlienManager {
@Autowired
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;
@Controller
public class AlienController {
@Autowired
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">
<display-name>TestWebb</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring-applicationContext</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-applicationContext</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
9 : WEB-INF/spring-applicationContext-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<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"
/>
</bean>
</beans>
10 : WEB-INF/views/displayAlien.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Spring MVC Hello Alien</title>
</head>
<body>
<div>
<h2>All Aliens</h2>
<table border="3" width="20%">
<tr>
<th>Id</th>
<th>Name</th>
<th>Planet</th>
</tr>
<c:forEach items="${aliens}" var="aliens">
<tr>
<td>${aliens.id}</td>
<td>${aliens.name}</td>
<td>${aliens.planet}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="getAllAliens">Show Aliens</a>
</body>
</html>
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.

No comments