1 / 43

CS520 Web Programming Spring – Web MVC

CS520 Web Programming Spring – Web MVC. Chengyu Sun California State University, Los Angeles. Spring Framework. Roadmap. Introduction to Spring MVC Database access Controller examples Spring MVC in CSNS2. The SHAM Example. Spring and Hibernate from Scratch

Download Presentation

CS520 Web Programming Spring – Web MVC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS520 Web ProgrammingSpring – Web MVC Chengyu Sun California State University, Los Angeles

  2. Spring Framework

  3. Roadmap • Introduction to Spring MVC • Database access • Controller examples • Spring MVC in CSNS2

  4. The SHAM Example • Spring and Hibernate from Scratch • http://csns.calstatela.edu/wiki/content/cysun/course_materials/cs520/sham/

  5. Add Spring MVC to A Maven Webapp • Spring dependencies • spring-webmvc • Front controller • DispatcherServlet in web.xml • Bean configuration file • /WEB-INF/<servlet-name>-servlet.xml

  6. Request Processing in Spring Web MVC Controller URL Mapping Controller URL Mapping Controller Request Front Controller Controller model Application Server Response View Resolution View

  7. Spring Controllers • Spring controllers are beans annotated with @Controller • In <servlet-name>-servlet.xml • <mvc:annotation-driven> • <context:component-scan>

  8. Controller URL Mapping • Requests are mapped to controller methods using @RequestMapping • value: URL pattern(s) • Mapping can be further refined by using method, params, and headers • http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping

  9. View • A controller method returns a view name, which will be resolved to a view implementation by a view resolver

  10. Resolve JSP Views <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix“ value=".jsp" /> </bean> Prefix + ViewName + Suffix = JSP File Why not just return the path to the JSP file??

  11. View Technologies and Resolvers • http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/web/servlet/ViewResolver.html

  12. Model • Model objects are passed to view using a ModelMap • put( String, Object ) attribute name attribute value

  13. Request Processing Recap 3 Controller URL Mapping Controller 1 URL Mapping 2 Controller Request Front Controller Controller 4 5 model Application Server Response View Resolution 6 View 7

  14. Database Access Dependencies • Hibernate • Spring ORM • Database connection pooling • DBCP vs. Tomcat JDBC • Database driver

  15. Configuration • Hibernate JPA • persistence.xml • Spring • web.xml • applicationContext.xml

  16. Spring Transaction Management • Transaction management code is added through AOP • <context:annotation-config> enables annotations like @Autowired and @PersistenceContext • <tx:annotation-driven>enables annotations like @Transactional

  17. Model Classes and Database • Model classes, e.g. User • JPA annotations • Database • SQL scripts • hibernate_sequence

  18. Database Access • DAO interfaces, e.g. UserDao • DAO implementations, e.g. UserDaoImpl

  19. Spring DAO Annotations • @Repository for DAO implementation classes • @PersistenceContext injects an entity manager to the class • @Transactionalfor methods that write to the database

  20. Controller Examples List all users Display a selected user Add a new user Edit a user

  21. Example: List All Users • Controller basics • @Controller • @RequestMapping • @ModelMap • Using DAO

  22. Example: Display A User Using @RequestParam required Using @PathVariable

  23. About Wildcard in Servlet <url-pattern> • A string beginning with a *. • E.g. *.html, *.do • A string beginning with a / and ending with a /* • E.g. /*, /users/* See Servlet Specification 3.0, Section 12

  24. About Spring @RequestMapping http://localhost/springmvc/user/viewUsers.html <url-pattern>*.html</url-pattern> @RequestMapping(“/usr/viewUsers.html”) http://localhost/springmvc/users/1 <url-pattern>/users/*</url-pattern> @RequestMapping(“/{userId}”)

  25. Example: Add A User Model attribute (a.k.a. command object, form backing object) Concept Binding @ModelAttribute @RequestMapping method The “redirect” view

  26. Command Object Bind form fields to properties of the object HTML Form model attribute Binding public class User { String username; String password; … } Usename: Password: Login

  27. Handling Forms • One controller method for • Creating a model attribute • Returning the form view • Form view • Use Spring <form> tags to bind the model attribute to the form • One controller method for • Processing the command object (annotated with @ModelAttribute) • Returning the success view

  28. Spring’s form Tag Library Documentation - http://static.springsource.org/spring/docs/current/spring-framework-reference/html/view.html#view-jsp-formtaglib Tag reference – http://static.springsource.org/spring/docs/current/spring-framework-reference/html/spring-form.tld.html

  29. Example: Edit A User • Store model attribute in session • @SessionAttributes • SessionStatus • Use setComplete() to remove the model attribute in session

  30. Non-Session Model Attributes Form request Form Submission Create a model attribute in controller Create a model attribute Populate the model attribute with submitted data Use the model attribute to populate the form fields in view Pass the model attribute to controller

  31. Session Model Attributes Form request Form Submission Create a model attribute in controller Retrieve the model attribute from session Populate the model attribute with submitted data Save the model attribute in session Use the model attribute to populate the form fields in view Pass the model attribute to controller

  32. Access HTTP Request, Response, and Session • Simply add an argument of that type to the controller method • Examples in CSNS2 • DownloadController • instructor/SectionController

  33. Spring MVC in CSNS2 • Configuration • Apache Tiles

  34. Spring Configuration Files /WEB-INF/<servlet-name>-servlet.xml /WEB-INF/spring.xml /WEB-INF/applicationContext.xml /WEB-INF/spring/*.xml

  35. Additional Spring Configurations • web.xml • OpenEntityManagerInViewFilter • spring.xml • <mvc:resources> • <mvc:view-controller>

  36. Resource Filtering Using Maven • Replace place holders like ${db.url} in resource files with actual values • <filter> • build.properties • Resources • <resources>, <testResources>, <webResources>

  37. Apache Tiles • http://tiles.apache.org/ • A template framework for JSP • Similar to Master Page in ASP.NET

  38. A Tiles Example page1.jsp page2.jsp Header Header Content 1 Content 2 Footer Footer

  39. Tiles – Template Page template.jsp Header <tiles:insertAttribute name=“content” /> Footer

  40. Tiles – Content Pages page1.jsp page2.jsp Content 1 Content 2

  41. Tiles – Definitions <definition name=“page1” template=“template.jsp”> <put-attribute name=“content” value=“page1.jsp” /> </definition> <definition name=“page2” template=“template.jsp”> <put-attribute name=“content” value=“page2.jsp” /> </definition>

  42. Resolve Tiles Views in Spring <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/> <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/tiles.xml</value> </list> </property> </bean>

  43. Tiles Usage in CSNS2 • WEB-INF/layouts for template and header/footer pages • WEB-INF/pages for content pages • WEB-INF/tiles.xml

More Related