CS 520 Web Programming Spring MVC Framework Chengyu
CS 520 Web Programming Spring – MVC Framework Chengyu Sun California State University, Los Angeles
Roadmap Request Processing Transactions and hibernate support (Model) Controllers (Controller) Data input and display (View)
Understand Request Processing What happens when the server received a request like http: //sun. calstatela. edu/csn s/instructor/view. Submissions. html? assignment. Id=50001
Direct Resource Mapping http: //cs 3. calstatela. edu/~cysun/home. html /home/cysun/public_html/home. html http: //cs 3. calstatela. edu: 8080/cysun/home. jsp /home/cysun/www/home. jsp
URL Mapping in a Java EE Application URL Mapping Request Application Server Response Servlet
URL Mapping … Configured in web. xml n n n <servlet> and <servlet-mapping> <welcome-file-list> <error-page> Specified in the Servlet Specification
… URL Mapping HTTP Request URL Mapping Directory Listing (/) welcome file Matches servlet mapping pattern servlet no match 404 error page
Request Processing in an MVC Framework URL Mapping Response Application Server Request Controller URL Mapping Front Controller View model
Spring Configuration File(s) <name>-servlet. xml n <name> must be the same as the <servlet -name> of the Dispatcher. Servlet specified in web. xml Can have additional bean configurations files n E. g. application. Context. xml, csns-data. xml, csns-email. xml, csns-acegi. xml. . .
More About Configuration Files (Welcome to Metadata Hell!) Under classpath (/WEB-INF/classes) n n n hibernate. cfg. xml ehcache. xml *. properties Under /WEB-INF n n n web. xml Spring configuration files server-config. wsdd Under /META-INF n context. xml
Configuration Files in CSNS. . . All configuration files are under /conf init target in build. xml n n n Copy configuration files to the right places Rename spring-*. xml to ${app. name} -*. xml Insert some parameter values into the configuration files
. . . Configuration Files in CSNS Advantages n n n One folder (i. e. /conf) for all metadata files One file (i. e. build. properties) for all configurable parameters Reusable for other applications Disadvantages n n Non-standard “Resource out of sync” error in Eclipse
Controller URL Mapping … Maps a URL pattern to a controller that will handle the request Bean. Name. Url. Handler. Mapping (default) <bean name="/instructor/home. html" class="csns. spring. controller. instructor. View. Sections. Controller">
… Controller URL Mapping … Simple. Url. Handler. Mapping <bean id="home" class="csns. spring. controller. Home. Controller" /> <bean id="url. Mapping" class="org. springframework. web. servlet. handler. Simple. Url. Handler. Mapping"> <property name="mappings"> <props> <prop key="/home. html">home</prop> </props> </property> </bean>
… Controller URL Mapping More than one URL handler n <property name=“order” value=“ 0”/> No mapping found n 404 error
Request Processing in an MVC Framework Controller URL Mapping Response Application Server Request Front Controller model View Resolution
Model and View Examples Add. Instructor. Controller View. Submissions. Controller Take. Survey. Controller
Model. And. View ( String view. Name, String model. Name, Object model. Object ) Resolve to a view Attribute in Request scope Model. And. View( String view. Name ). add. Object( String model. Name, String model. Object ). . .
View Resolvers. . . http: //static. springsource. org/spring/do cs/2. 5. x/api/org/springframework/web/s ervlet/View. Resolver. html
. . . View Resolvers Internal. Resource. View. Resolver for JSP Support for non-JSP view technologies n Velocity, Free. Marker, Jsper. Reports, XSLT Views generated by Java classes n n n Bean. Name. View. Resolver Xml. View. Resolver Resource. Bundle. View. Resolve Multiple view resolvers n <property name=“order” value=“ 0”/>
Internal. Resource. View. Resolver Example <bean id="view. Resolver" class="org. springframework. web. servlet. view. Internal. Resource. View. Resolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix“ value=". jsp" /> </bean> Prefix + View. Name + Suffix = JSP File
Special Views Redirect n n new Model. And. View( “redirect: ” + url ) E. g. Logout. Controller null n E. g. Download. Controller
Recap request web. xml 404 error page URL Mapping welcome page Dispatcher. Servlet View Controller URL Mapping View Resolver Controller spring-servlet. xml
Database Access DAO interfaces n E. g. csns. model. dao DAO implementations n n E. g. csns. model. dao. hibernate Hibernate DAO implementation classes inherit from Hibernate. Dao. Support Hibernate. Template n http: //static. springsource. org/spring/docs/2. 5. x/ap i/org/springframework/orm/hibernate 3/Hibernate. T emplate. html
Programmatic vs. Declarative Transaction Management Programmatic: void save. User( User u ) { transaction. start(); … transaction. end(); } Declarative: <transaction> <method> save. User </method> </transaction>
Hibernate Support in Spring Without Spring Transaction tx = null; try { tx = s. begin. Transaction(); s. save. Or. Update( e ); tx. commit(); } catch( Exception e ) { if( tx != null ) tx. rollback(); e. print. Stack. Trace(); } With Spring get. Hibernate. Template(). save. Or. Update( user );
Spring Transaction Managers JDBC Hibernate n n V 3 Before V 3 JTA Object-Relational Bridge (ORB)
Configure Hibernate Transaction Manager data. Source n n Database connection information DBCP - http: //jakarta. apache. org/commons/dbcp/ session. Factory n Hibernate information transaction. Manager transaction. Attribute. Source See /conf/spring-data. xml
Transaction Attributes Isolation levels Read-only hints Transaction timeout period Method name patterns Propagation behaviors
Propagation Behaviors Determines whether the method should be run in a transaction, and if so, whether it should run within an existing transaction, a new transaction, or a nested transaction within an existing transaction.
The Need for Propagation Behaviors Class Foobar. Dao { void save. Foo( Foo foo ) { save(foo); } void save. Bar( Bar bar ) { save(bar); } void save. Foobar( Foobar foobar ) { save. Foo( foobar. get. Foo() ); save. Bar( foobar. get. Bar() ); } }
Propagation Behaviors in Spring Propagation Behaviors Run in. . . PROPAGATION_MANDATORY Existing transaction PROPAGATION_NESTED Nested transaction PROPAGATION_NEVER No existing transaction PROPAGATION_NOT_SUPPORTED Suspended during existing transaction PROPAGATION_REQUIRED Existing or new transaction PROPAGATION_REQUIRES_NEW New transaction PROPAGATION_SUPPORTS May run in existing transaction
Add Transaction Support for DAO Classes Transaction. Proxy. Factory. Bean definition inheritance class Foo. Dao. Proxy { class Foo. Dao { } void save. Foo( Foo foo ) { save(foo); } transaction. begin(); save(foo); } } transaction. end();
Examples: Course Management List courses Add a course Edit a course
Controller Interface org. springframework. web. servlet. mvc. C ontroller http: //static. springsource. org/spring/do cs/2. 5. x/api/org/springframework/web/s ervlet/mvc/Controller. html Model. And. View handle. Request ( Http. Servlet. Request request, Http. Servlet. Response response )
Select A Controller Parameterizable. View. Controller Simply display a view, i. e. the request does not need to be processed Controller (interface) Abstract. Controller do not use request parameters or use only simple parameters Base. Command. Controller Abstract. Command. Controller request parameters can be mapped to an object; can use validators to validate request parameters Abstract. Form. Controller Simple. Form. Controller Abstract. Wizard. Form. Controller Handles form input Handles multi-page form input
Parameterizable. View. Controller CSNS Examples n login
Abstract. Controller CSNS Examples n n View. Submission. Controller Delete. Assignment. Controller Example 1: List all courses
Displaytag http: //displaytag. sourceforge. net/ Sortable columns and result paging Use displaytag, not displaytag-el
Displaytag Examples CSNS Examples n n n courses. jsp instructor/view. Submissions. jsp forum/view. Topic. jsp
<display: table> name n n Name of the collection to be displayed Like items in <c: for. Each> uid n n Name of the object in each iteration Like var in <c: for. Each> request. URI n n The URL used to request the page E. g. view. Submission. html, not instructor/view. Submission. jsp
<display: column> property sortable title sort. Property
Customize Displaytag Tables Using CSS n class and style attributes for <display: table> and <display: column> Using properties n n n displaytag. properties for the whole application <display: set. Property> for a particular table http: //displaytag. sourceforge. net/11/configuration. html
Simple. Form. Controller CSNS Examples n n Edit. Assignment. Controller Create. Topic. Controller Example 2: Add a course
Command Object HTML Form Command Object Binding Usename: Password: Login public class User { String username; String password; … } Any class can be used as a command class in Spring n vs. Action. Form in Struts 1
Simple Form Handling The controller needs to handle two cases: n n Display form Process input data Handle first request: Create a command object and expose the object as a page scope variable Handle input: Bind the request parameters to the command object Call controller. on. Submit() Display the form view Display the success view
Spring’s form Tag Library Documentation http: //static. springsource. org/spring/docs/2. 5. x/reference/view. html#view-jsp-formtaglib Tag reference – http: //static. springsource. org/spring/docs/2. 5. x/reference/spring-form. tld. html Example n forum/create. Topic. jsp
form. Backing. Object() Override default command object creation Example 3: Edit a course
Abstract. Wizard. Form. Controller CSNS Examples n Import. Roster. Controller
Summary Request mapping and view resolution Model n n Controller n n n Abstract. Controller and Simple. Form. Controller Command object /conf/spring-servlet. xml Dao and Dao. Impl /conf/spring-data. xml View n n Form view and success view <spring: form>
- Slides: 50