Spring MVC tutorial provides an elegant solution
to use MVC in spring framework by the help of DispatcherServlet.
In Spring Web MVC, DispatcherServlet class
works as the front controller. It is responsible to manage the flow of the
spring mvc application.
The @Controller annotation
is used to mark the class as the controller in Spring 3.
The @RequestMapping annotation
is used to map the request url. It is applied on the method.
Understanding the flow of Spring
Web MVC
As displayed in the
figure, all the incoming request is intercepted by the DispatcherServlet that
works as the front controller. The DispatcherServlet gets entry of handler
mapping from the xml file and forwards the request to the controller. The
controller returns an object of ModelAndView. The DispatcherServlet checks the
entry of view resolver in the xml file and invokes the specified view
component.
Spring MVC Hello World Example
In this application,
I am creating most simple employee management application demo
having only one feature i.e. list all available employees in system. Let’s note
down the directory structure of this application.
pom.xml
Below pom.xml file contains dependencies for spring mvc and taglibs support for writing jsp files.
Below pom.xml file contains dependencies for spring mvc and taglibs support for writing jsp files.
web.xml
This minimum web.xml file declares
one servlet (i.e. dispatcher servlet) to receive all kind of requests.
Dispatcher servlet here acts as front controller
spring-servlet.xml (You can have
applicationContext.xml as well)
We are using annotated classes at request
handler, service and dao layer so I have enabled annotation processing for all
class files in base package “com.teachtojava.demo“.
EmployeeController.java
Annotation @RequestMapping at class
level and method level determine the URL at which method will be invoked.
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.teachtojava.demo.service.EmployeeManager;
@Controller
@RequestMapping("/employee-module")
public class EmployeeController
{
@Autowired
EmployeeManager manager;
@RequestMapping(value = "/getAllEmployees", method =
RequestMethod.GET)
public String getAllEmployees(Model model)
{
model.addAttribute("employees", manager.getAllEmployees());
return "employeesListDisplay";
}
}
EmployeeVO.java
This class act as model for MVC pattern.
package com.teachtojava.demo.model;
import java.io.Serializable;
public class EmployeeVO implements
Serializable
{
private static final long serialVersionUID = 1L;
private Integer id;
private String firstName;
private String lastName;
//Setters and Getters
@Override
public String toString() {
return "EmployeeVO [id=" + id + ", firstName=" +
firstName
+ ", lastName=" +
lastName + "]";
}
}
EmployeeDAO.java
The classes at third tier in 3-tier
architecture. Responsible for interacting with underlying DB storage.
import java.util.List;
import com.teachtojava.demo.model.EmployeeVO;
public interface EmployeeDAO
{
public List getAllEmployees();
}
EmployeeDAOImpl.java
import java.util.ArrayList;
import java.util.List;
import
org.springframework.stereotype.Repository;
import com.teachtojava.demo.model.EmployeeVO;
@Repository
public class EmployeeDAOImpl implements
EmployeeDAO {
public List getAllEmployees()
{
List employees = new ArrayList();
EmployeeVO vo1 = new EmployeeVO();
vo1.setId(1);
vo1.setFirstName("Lokesh");
vo1.setLastName("Gupta");
employees.add(vo1);
EmployeeVO vo2 = new EmployeeVO();
vo2.setId(2);
vo2.setFirstName("Raj");
vo2.setLastName("Kishore");
employees.add(vo2);
return employees;
}
}
EmployeeManager.java
The classes at second tier in 3-tier
architecture. Responsible for interacting with DAO Layer.
import java.util.List;
import com.teachtojava.demo.model.EmployeeVO;
public interface EmployeeManager
{
public List getAllEmployees();
}
EmployeeManagerImpl.java
import java.util.List;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Service;
import com.teachtojava.demo.dao.EmployeeDAO;
import com.teachtojava.demo.model.EmployeeVO;
@Service
public class EmployeeManagerImpl implements
EmployeeManager {
@Autowired
EmployeeDAO dao;
public List getAllEmployees()
{
return dao.getAllEmployees();
}
}
employeesListDisplay.jsp
This jsp is used to
display all the employees in system. It iterates the collection of employees in
loop, and print their details in a table. This fits into view layer of MVC
pattern.
Now deploy the application in your
application server (i am using tomcat 7). And hit the URL “http://localhost:8080/springmvcexample/employee-module/getAllEmployees“.
You will see below screen if you have configured everything correctly.
No comments:
Post a Comment