Professional Documents
Culture Documents
Spring is an open source framework , which is very flexible while developing any
kind of application.
● IOC(Inversion of Control).
● J2EE.
MVC = Model-View-Controller.
Clearly separates business, navigation and presentation
logic.
Controller
Handles navigation logic and interacts with the service
tier for business logic
Model
The contract between the Controller and the View
Contains the data needed to render the View
View
Renders the response to the request
Pulls data from the model
UI Tier: User Interface Tier. Like a web browser.
JSP WEB.XML
Request HANDLER MAPPING
Response 4 DAO
To
7 JSP 6 CONTROLLER (Saving or
User
5 retrieving/up
dating data)
The transition of the spring MVC flow is described below:-
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
DispacherServlet class
It is the servlet which handles the Http request and is inherited from
the HttpServlet base class.
Inside DispacherServlet we will also configure the view in which we will define that
it should take .jsp file as a View or .html file as a View etc.
Inside DispacherServlet we will also configure the controller
CONTROLLER
Spring provides many types of controllers and the best way to decide
which controller type to use probably is by knowing what type of
functionality you need. For example, do your screens contain a form?
Do you need wizard like functionality? Do you just want to redirect to
a JSP page and have no controller at all? These are the types of
questions you will need to ask yourself to help you narrow down the
choices.
ABSTRACT CONTROLLER
{
EmployeeDao employeeDao;
public void setEmployeeDao(EmployeeDao employeeDao)
{
this.employeeDao = employeeDao;
}
protected ModelAndView handleRequestInternal(HttpServletRequest
request,HttpServletResponse response) throws Exception
{
Map model = new HashMap();
String view = "employeeHome";
model.put("employees", employeeDao.getEmployeeList());
}
The Model And View Class
//check whether the id is null or not by getting the id and if its null then create a new employee
object or else call the employee dao to get the details.
}
protected ModelAndView showForm(HttpServletRequest request,HttpServletResponse
response, BindException errors)throws Exception
{
String viewName = "edit";
System.out.println("c");
if (request.getParameter("edit") != null)
{
}
else if (request.getParameter("save") != null)
{
}
if(!errors.hasErrors()){
Map map = new HashMap();
employeeDao.save((Employee)command);
System.out.println("d");
map.put("employee",command);
return new ModelAndView("successView",map);
}
return showForm(request,response, errors);
}
}
SimpleFormController:- best to use for processing forms.
FormBackingObject():- Retrive the command object which
can be from session or may be from database.
ShowForm:- Completes ModelAndView and returns
command object stored in session if configured . Renders
the actual form.
onBindAndValidate:- Allows you to bind parameters to the
command that don't need validation.
If validation fails then add errors to the ModelAndView and
show the form again.
Implementing Hibernate With Spring Framework
While integrating the hibernate and the spring framework, you need to
perform several steps and functions.
}
POJO Classes
POJO:-(Plain old Java object)For creating Persistent object for saving
datas
public class Employee
{
Integer id =null;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
}
Mapping
● The process of specifying the bindings between an object
model and a database schema
● Principal mechanism is via XML mapping files
● Defacto file name extension: is .hbm.xml
● Multiple ways to set this up: a single file, one file per
class. Best practice is is to use one file per class,
with each file placed next to its corresponding class
file in the package hierarchy, and loaded as a resource
One-to-One Mapping
EXAMPLE
<hibernate-mapping> (Employee.hbm.xml)
<class name="com.employee.model.Employee" lazy="false" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name"/>
<property name="middleName" column="middleName"/>
<property name="lastName" column="lastName"/>
<one-to-one name="contactInfo" lazy="false" class="com.employee.model.ContactInfo" />
</class>
</hibernate-mapping>
<hibernate-mapping> (ContactInfo.hbm.xml)
<class name="com.employee.model.ContactInfo" lazy="false" table="contactinfo">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="email" column="email"/>
<property name="city" column="city"/>
<property name="state" column="state"/>
<property name="phno" column="phno" />
</class>
</hibernate-mapping>
The ViewResolver Interface
class="org.springframework.web.servlet.view.InternalResourceViewResolve
r"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
The urlBasedViewResolver class implements the viewResolver interface to map the logical
views with URL's. This class uses the URL to render response in the form of View technologies
such as a JSP page etc.
Creating the Views
To create a view , we need to create jsp pages and that are used to
display the view(Web page) based on the Spring Web MVC paradigm.