Spring Framework

Spring Projects

Spring Project 1

adplus-dvertising
Spring MVC Implementation
Previous Home Next

Introduction: In Spring MVC implementation the controller is an application component. Which is responsible for processing the request. It does not controlled the workflow of request processing as done by struts controller.

Workflow of request is controlled by front controller which is implemented by servlet name "DispatcherServlet" is provides a single entry point in to the application.

Model View Controller (MVC):

Model: Model component represent a data for spring MVC usually a map of model object is used as Model component.

View: View is a generic component of the framework which is responsible for presenting data to the end user. Multiple implementation of view are provided the framework to support different presentation technique.

Controller: Controller component provide the facility to change the state and flow the data. Controller component is responsible for the controlled the workflow of the web application in the spring framework.

How can we implement spring MVC:

Following steps are required to used spring MVC implementation:

Step 1: First select the web project in our IDE and add the capability of spring framework (it means that add the jar file). The following jar file are add before implement the spring MVC application.

  1. Spring 2.5 core Library
  2. Spring 2.5 web Library

Step 2: Servlet entry is to be made in web.xml file for DispatcherServlet class.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
 <servlet-name>login</servlet-name>
 <servlet-class>
 org.springframework.web.servlet.DispatcherServlet
 </servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>login</servlet-name>
 <url-pattern>*.spring</url-pattern>
 </servlet-mapping>
</web-app>

step 3: Define in xml file to specified a mapping requested url and controller of spring application. This file is conventionally name as follows

Example:

ServletName-servlet.xml

step 4: If DispatcherServlet is assign "frontController" name in web.xml then this file must be named as"frontController-servlet.xml"

step 5: The file "frontController-servlet.xml" used the bean element to mapped request url and controller Class

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/tableone</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>

<bean id="jdbcTemp" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref local="datasource"/>
</constructor-arg>
</bean>

<bean id="loginModel" class="org.r4r.LoginModel"/>

step 6: Define controller class, Controller is a subclass of AbstractController class. AbstractController class provides an abstract method name

handleRequestInternal(HttpServletRequest request,HttpServletResponse response); 

which is invoked each time a request of the controller received. This method returned a ModelAndView object.

Previous Home Next