Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Trustparency web doc spring 2.5 & hibernate

1,651 views

Published on

spring 2.5 and hibernate (example and explanations for beginners)

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Trustparency web doc spring 2.5 & hibernate

  1. 1. trustparency Spring 2.5 & Hibernate
  2. 2. 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ónWeb doc for Spring & Hibernate 11/07/2012 José Luis Muñoz de Morales 1.0 2 Copyright ©
  3. 3. INDEX 1. Spring MVC............................................................................. 4 2. Web Flow Case: Login............................................................ 5 3. Web Flow Case: User Registration ...................................... 11
  4. 4. 1. Spring MVCIn the following image is displayed the Spring architecture used intrustparency web design.Each entity represented below such as Handler Mapping, DispacherServlet, Controller…etc is a standard entity which belongs to Springframework. 4 Copyright ©
  5. 5. 2. Web Flow Case: First entry 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)<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> 5 Copyright ©
  6. 6. 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> 6 Copyright ©
  7. 7. 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> 7 Copyright ©
  8. 8. 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. 8 Copyright ©
  9. 9. Tiles send to the viewResolver the jsp, and this JSP file is sent tothe DispatcherServlet.Finally, DispatcherServlet return the “Login.jsp” back to user.JSP (Java Server Page): is a technology which combines htmllanguage with java language to create dynamic pages. Javalanguage is included using the following tag. <% java code %> 9 Copyright ©
  10. 10. Login.jsp: merge java code and hmtl, to create a complete htmlrespond to the user, which is what the user finally sees on thehis/her browser. 10 Copyright ©
  11. 11. 3. Web Flow Case: User Registration1. 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) 11 Copyright ©
  12. 12. 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:AnadirUsuarioController.java UserCommand available at UserCommand.java(java class) AnadirUsuarioController, (POJOs java class) submit method.It is the Controller in charge of * It contains the fields/attributesthe dialog with the mapped in the data base, alsoDispacherServlet. available in the Web Form sent by the user´s browser. The JPS is called automatically by Spring.UserValidator.java NuevoUsuario.jsp(java class) (form view JSP page)Validates NuevoUsuario.jsp data, which are <form action="anadirUsuario.htm"encapsulated into UserCommand.java. method="post">UserValidator is a Spring Framework JSP page which is the form thatstandard class, and it uses the method contains the textfield to collect“validate()”. user´s data. 12 Copyright ©
  13. 13. Relation between objets, all of them are related using /anadirusuario.htm requestLogin.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 thesuitable 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 arelocated the user´s data. These data is also encapsulated into thecommand object calle userCommand. 13 Copyright ©
  14. 14.  Analizing the same from the Controller perspective, known asanadirUsuarioController 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. anadirUsuarioController.java UserCommand.java nuevoUsuario.jsp 14 Copyright ©
  15. 15. 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 fromSpring´s Container, or better explained, an instance is injected by theContainer). This layer has been removed. 15 Copyright ©
  16. 16. The final class in charge of implementing the userService methods isuserServiceImpl, 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 isreferred as bean, which is located in the file proyecto-data.xml.Proyecto-data.xml 16 Copyright ©
  17. 17. The bean “hibernateTemplate” is injected into usersDAO across Spring.The bean is found at hibernate configuration file (xml), which is a class ofSpring´s Framework.Proyecto-ConfigHibernate.xmlTo solve “sessionFactory”, within the same file we found:At this stage, the insert operation is done, and therefore data are save intothe data base using Hibernate framework. 17 Copyright ©
  18. 18. 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. 18 Copyright ©
  19. 19. According to the xml file TilesConfigurer is the class in charge ofsolve the name.We need to see the name “detalleusuario” (returned by theController) into the proyecto-defs.xml.Tiles send to the viewResolver the jsp, and this JSP file is sent tothe DispatcherServlet.Finally, DispatcherServlet return the “detalleUsuario.jsp” back touser.* Within detalleUsuario.jsp, is used the object usuario get from therequest using the identifier or key “usuario”¸which has been savedby the Controller in the submit method. 19 Copyright ©

×