1 / 38

CS520 Web Programming Spring – MVC Framework

CS520 Web Programming Spring – MVC Framework. Chengyu Sun California State University, Los Angeles. Roadmap. Request Processing DAO implementation (Model) Tiles (View) Controllers (Controller). Understand Request Processing.

winklerm
Download Presentation

CS520 Web Programming Spring – MVC Framework

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 – MVC Framework Chengyu Sun California State University, Los Angeles

  2. Roadmap • Request Processing • DAO implementation (Model) • Tiles (View) • Controllers (Controller)

  3. Understand Request Processing • What happens when the server received a request like http://localhost:8080/csns2/student/viewSubmission.html?assignmentId=2000005 Host URL Application Context Name

  4. Application Context • Allows multiple applications to be deployed on the server without interfering with each other • Application context name • Project name in Eclipse, or • WAR file name, or • Specified in WEB-INF/META-INFO/context.xml

  5. URL Mapping in a Java EE Application URL Mapping Request Servlet Servlet Application Server Servlet Response

  6. URL Mapping … • Configured in web.xml • <servlet> and <servlet-mapping> • <welcome-file-list> • <error-page>

  7. … URL Mapping HTTP Request URL Mapping Matches servlet mapping pattern no match Directory Listing (/) 404 error page welcome file servlet

  8. Other Configuration Files • Under classpath • Properties files • Hibernate, EHCache, Log4j, … • Under WEB-INF • Spring bean definitions • Tiles, …

  9. Spring MVC Namespace • <mvc:annotation-driven> • <mvc:resources> • Serve static resources • Can set cache period • <mvc:view-controller> • A controller that simply returns a view (without doing anything else)

  10. Request Processing in an MVC Framework Controller URL Mapping Controller URL Mapping Controller Request Front Controller Controller Application Server model Response View

  11. Controller URL Mapping • Controller classes are annotated with @Controller • Controller methods are annotated with @RequestMapping • value: URL pattern(s) • Mapping can be further refined by using method, params, and headers

  12. Database Access • DAO interfaces • E.g. csns.model.core.dao • DAO implementations • E.g. csns.model.core.dao.jpa

  13. Spring Transaction Management • Transaction management code is added through AOP • <tx:annotation-driven>enables configuration of transactional behavior based on annotations

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

  15. Request Processing in an MVC Framework Controller URL Mapping URL Mapping Request Front Controller Controller View Resolution model Application Server Response View View

  16. Model and View • A view is identified by a name (not a path like /WEB-INF/foo.jsp) • Model objects are passed to view using a ModelMap

  17. View Technologies Supported by Spring • JSP, Velocity, FreeMarker, XSLT, Tiles,JasperReports • Views generated by Java classes

  18. View Resolvers • http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/ViewResolver.html

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

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

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

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

  23. 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>

  24. 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>

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

  26. Special Views • “redirect:” + url • E.g. instructor/AssignmentController • null • E.g. DownloadController

  27. Recap request web.xml URL Mapping 404 error page welcome page Front Controller View Controller URL Mapping View Resolver Controller Spring

  28. Create Controllers • List all courses • Display a selected course • Add a new course • Edit a course

  29. Example: List All Courses • Controller basics • @Controller • @RequestMapping • ModelMap • Database access • Use Tiles • Create a content page • Add a tiles definition

  30. Example: Display A Course • @RequestParam • required

  31. Example: Add A Course • Command object (a.k.a. form backing object) • Concept • Binding • @ModelAttribute • @RequestMapping • method • The “redirect” view

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

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

  34. 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

  35. Example: Edit A Course • Store command object in session • @SessionAttributes • SessionStatus • Property editor • Convert between an object and its string representation • E.g. a Calendar object  “10/26/2011” • The <select> tag in Spring form taglib

  36. Exercise: Delete A Course • Delete the course from the database, or • Set a deleted flag

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

  38. Further Readings • Spring in Action (3rd Ed) • Chapter 5-7 • Spring Framework Reference Documentation http://static.springsource.org/spring/docs/current/spring-framework-reference/html/ • Chapter 16.2.4 Using Spring’s form tag library • Appendix G. spring-form.tld

More Related