Trustparency web doc spring 2.5 & hibernate
Post on 11-Nov-2014
1174 Views
Preview:
DESCRIPTION
Transcript
Spring 2.5 & Hibernate
trustparency
2
Copyright ©
This guide explains in detail an example of Spring 2.5 and Hibernate.
You can download the code of the example at:
https://www.dropbox.com/s/23hna4kjc2zpf8y/spring2.5.rar?n=1151701
https://www.wetransfer.com/downloads/69e508b6731646d19eb8ee8094fb8fbd20130407090225/836b3b316555169f387db7e7df6b916b20130407090225/d0b40d
Título Fecha Autor Versión
Web doc for Spring & Hibernate 11/07/2012 José Luis Muñoz de Morales 1.0
INDEX
1. Spring MVC............................................................................. 4
2. Web Flow Case: Login ............................................................ 5
3. Web Flow Case: User Registration ...................................... 11
4
Copyright ©
In the following image is displayed the Spring architecture used in
trustparency web design.
Each entity represented below such as Handler Mapping, Dispacher
Servlet, Controller…etc is a standard entity which belongs to Spring
framework.
1. Spring MVC
5
Copyright ©
Petition: login.htm (it could be anyone)
1. Login.htm is requested.
2. URL is end with “.htm” extension, so it will redirect to
“DispatcherServlet” and send request to the default
“BeanNameUrlHandlerMapping” (in our case its name is
SimpleURLHandlerMapping).
Explanation: the HandlerMapping is called SimpleUrlHandlerMapping, it
receives the request, which is mapped within the proyecto-
servlet.xml.
Proyecto-servlet.xml (Spring)
2. Web Flow Case: First entry
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="defaultHandler" value="simpleUrlController"/> <property name="alwaysUseFullPath"><value>true</value></property> <property name="mappings"> <props> <prop key="/login.htm">loginController</prop> <prop key="/dentro.htm">loginController</prop> <prop key="/userInicio.htm">loginController</prop> <prop key="/anadirUsuario.htm">anadirUsuarioController</prop> </props> </property> </bean>
6
Copyright ©
3. BeanNameUrlHandlerMapping return a Controller to the
DispatcherServlet. In our case the controller is called loginController
as it could be shown in the text above (in yellow).
DispatcherServlet forward a request to the Controller, LoginController. To
recognize the controller.
Proyecto-servlet.xml (Spring)
<bean id="loginController" class="proyecto.controllers.LoginController" > <property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/login.htm">login</prop> <prop key="/dentro.htm">dentro</prop> <prop key="/userInicio.htm">inicio</prop> </props> </property>
7
Copyright ©
4. LoginController process it and return a ModelAndView object
named “login”.
<bean id="loginController" class="proyecto.controllers.LoginController" > <property name="methodNameResolver"> <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/login.htm">login</prop> <prop key="/dentro.htm">dentro</prop> <prop key="/userInicio.htm">inicio</prop> </props> </property> </bean> </property> </bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="defaultHandler" value="simpleUrlController"/> <property name="alwaysUseFullPath"><value>true</value></property> <property name="mappings"> <props> <prop key="/login.htm">loginController</prop> <prop key="/dentro.htm">loginController</prop> <prop key="/userInicio.htm">loginController</prop> <prop key="/anadirUsuario.htm">anadirUsuarioController</prop> </props> </property> </bean>
8
Copyright ©
Proyecto.controllers.LoginController.java
5. DispatcherServlet received the ModelAndView and call the
viewResolver to process it.
In our case, the viewResolver uses another technology to solve the
name. These technology is called Tiles.
According to the xml file TilesConfigurer is the class in charge of
solving “the name”.
We need to see the name “login” (returned by the Controller) into
the proyecto-defs.xml.
9
Copyright ©
Tiles send to the viewResolver the jsp, and this JSP file is sent to
the DispatcherServlet.
Finally, DispatcherServlet return the “Login.jsp” back to user.
JSP (Java Server Page): is a technology which combines html
language with java language to create dynamic pages. Java
language is included using the following tag.
<% java code %>
10
Copyright ©
Login.jsp: merge java code and hmtl, to create a complete html
respond to the user, which is what the user finally sees on the
his/her browser.
11
Copyright ©
1. anadirUsuario.htm is requested, using submit method from client
browser to send the data (post method).
2. URL is end with “.htm” extension, so it will redirect to
“DispatcherServlet” and send request to the default
“BeanNameUrlHandlerMapping” (in our case its name is
SimpleURLHandlerMapping).
Explanation: the HandlerMapping is called
SimpleURLHandlerMapping. It receives the request, which is
mapped within the proyecto-servlet.xml.
Proyecto-servlet.xml (Spring)
3. Web Flow Case: User Registration
12
Copyright ©
3. BeanNameUrlHandlerMapping return a Controller to the
DispatcherServlet. In our case the controller is called
anadirUsuarioController.
4. DispatcherServlet forward request “/anadirUsuario.htm” to the
anadirUsuarioController, in our case.
Proyecto-servlet.xml (Spring)
Diagram: relation between objects:
UserCommand.java
(POJOs java class)
* It contains the fields/attributes
mapped in the data base, also
available in the Web Form sent by the
user´s browser.
AnadirUsuarioController.java
(java class)
It is the Controller in charge of
the dialog with the
DispacherServlet.
NuevoUsuario.jsp
(form view JSP page)
<form action="anadirUsuario.htm"
method="post">
JSP page which is the form that
contains the textfield to collect
user´s data.
UserCommand available at
AnadirUsuarioController,
submit method.
UserValidator.java
(java class)
Validates NuevoUsuario.jsp data, which are
encapsulated into UserCommand.java.
UserValidator is a Spring Framework
standard class, and it uses the method
“validate()”.
The JPS is called
automatically by Spring.
13
Copyright ©
Relation between objets,
all of them are related using /anadirusuario.htm request
Login.jsp: is the first object called, it is used as a door to see the form.
Proyecto-servlet.xml: it has a mapping of the request, indicating the
suitable Controller object to handle /anadirusuario.htm.
Be careful:
1. this request not must be mapped within a Controller with a method,
it must be have a unique entry, specifiying: Controller name,
Command name, Command class, Validator and formView.
2. The formView name (which is also a JSP file) must be included
within proyecto-defs.xml, relating the jsp file used to collect user´s
data.
nuevoUsuario.jsp: is the form view, which has the textfields where are
located the user´s data. These data is also encapsulated into the
command object calle userCommand.
14
Copyright ©
Analizing the same from the Controller perspective, known as
anadirUsuarioController
Proyecto-servlet.xml (Spring)
Analysis of AnadirUsuarioController, has:
- userCommand: on submit, anadirUsuarioController receives a
Command object, which is a container of the data sent from the
browser.
nuevoUsuario.jsp
UserCommand.java
anadirUsuarioController.java
15
Copyright ©
Analysis of AnadirUsuarioController, has:
Property “userService”: is an attribute declared in the class file. In itself,
UserService is an interface (I guess that an instance is passed from
Spring´s Container, or better explained, an instance is injected by the
Container).
This layer has been
removed.
16
Copyright ©
The final class in charge of implementing the userService methods is
userServiceImpl, which has the implemented method “insertar()”.
Proyecto-service.xml: relation between userService and DAO (userDAO)
UserDAO: is the class property within UserServiceImpl class, and it is
referred as bean, which is located in the file proyecto-data.xml.
Proyecto-data.xml
17
Copyright ©
The bean “hibernateTemplate” is injected into usersDAO across Spring.
The bean is found at hibernate configuration file (xml), which is a class of
Spring´s Framework.
Proyecto-ConfigHibernate.xml
To solve “sessionFactory”, within the same file we found:
At this stage, the insert operation is done, and therefore data are save into
the data base using Hibernate framework.
18
Copyright ©
5. AnadirUsuarioController, once the user object has been save at the
end of the submit method, it returns a ModelAndView object named
“detalleusuario” to the DispacherServlet.
Proyecto.controllers.AnadirUsuarioController.java
At the same time, is set into the request map (a map table to save
data using Key-Value pairs) the object user with the identifier
“usuario”, which could be used in the JSP file thereafter.
6. DispatcherServlet received the ModelAndView and call the
viewResolver to process it.
In our case, the viewResolver uses another technology to solve the
name. These technology is called Tiles.
19
Copyright ©
According to the xml file TilesConfigurer is the class in charge of
solve the name.
We need to see the name “detalleusuario” (returned by the
Controller) into the proyecto-defs.xml.
Tiles send to the viewResolver the jsp, and this JSP file is sent to
the DispatcherServlet.
Finally, DispatcherServlet return the “detalleUsuario.jsp” back to
user.
* Within detalleUsuario.jsp, is used the object usuario get from the
request using the identifier or key “usuario”¸which has been saved
by the Controller in the submit method.
top related