CS 520 Web Programming Spring MVC Framework Chengyu
CS 520 Web Programming Spring – MVC Framework Chengyu Sun California State University, Los Angeles
Roadmap Web flow Controllers and validation Transactions and hibernate support Bits and pieces n n n Tomcat server setup Context, data source, and connection pooling Creating WAR files JSP pre-compilation Displaytag
Understand Web Flow What happens when the server received a request like http: //cs 3. calstatela. edu: 808 0/csns/instructor/home. html
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
Web Flow of Simple J 2 EE Application URL Mapping Request Application Server Response Servlet
URL Mapping … Specified by the Servlet Specification Configured in web. xml n <servlet> and <servlet-mapping> w “/servlet/*” w “*. do” n n <welcome-file-list> <error-page>
… URL Mapping HTTP Request URL Mapping Directory Listing (/) welcome file Matches servlet mapping pattern servlet no match 404 error page
Web Flow of a 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 Optional n n csns-data. xml csns-service. 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 csns-servlet. xml, csns-data. xml, csns-service. xml server-config. wsdd Under /META-INF n context. xml
Controller URL Mapping … Maps a URL pattern to a controller that will handle the request Bean. Name. Url. Handler. Mapping (default) <bean id="index" name=“/index. html /public/*index. html” class="evelyn. spring. controller. Index. Controller" />
… Controller URL Mapping … Simple. Url. Handler. Mapping <bean id="index" class="evelyn. spring. controller. Index. Controller" /> <bean id="url. Mapping" class="org. springframework. web. servlet. handler. Simple. Url. Handler. Mapping"> <property name="mappings"> <props> <prop key="/index. html">index</prop> <prop key="/public/*index. html">index</prop> </props> </property> </bean>
… Controller URL Mapping More than one URL handler n <property name=“order” value=“ 1”/> No mapping found n 404 error
Model. And. View ( String view. Name, String model. Name, Object model. Object ) Resolve to a view Attribute in Request scope
Web Flow of a MVC Framework Controller URL Mapping Response Application Server Request Front Controller model View Resolution
View Resolvers URL-based resolvers n n Internal. Resource. View. Resolver for JSP Velocity. View. Resolver for Velocity View classes resolvers n n n Bean. Name. View. Resolver Xml. View. Resolver Resource. Bundle. View. Resolver Multiple resolvers n <property name=“order” value=“ 1”/>
Special Views Redirect n new Model. And. View( “redirect: ” + url ) Forward n new Model. And. View( “forward: ” + url )
Interceptors Response Request controller interceptors view interceptors Similar to Filters in J 2 EE specification Handler. Interceptor. Adapter Example: Authorization. Interceptor
Recap request Servlet Specification 404 error page URL Mapping welcome page Dispatcher. Servlet Controller URL Mapping Interceptor(s) View Controller View Resolver Spring Interceptor(s)
Roadmap Web flow Controllers and validation Transactions and hibernate support Bits and pieces n n n Tomcat server setup Context, data source, and connection pooling Creating WAR files JSP pre-compilation Displaytag
Controller Class Hierarchy org. springframework. web. servlet. mvc n Controller
Select A 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
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
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 Display the form view Handle input: Bind the request parameters to the command object Call controller. on. Submit()
Controllers in CSNS Abstract. Controller n n Home, Logout, Download. Zip View, Grade, Email. Grades, … Simple. Form. Controller n n Account, Register, Reset. Password Drop. Students Abstract. Form. Controller n Email. Students Parameterized. View. Controller n Index, Login
Controller Example Delete an assignment n n n Create a controller Create a view (JSP) Add the controller to spring-servlet. xml
Exception Handling An Exception resolver catches all exceptions thrown by controllers and chooses the proper view to display Examples n n Simple. Mapping. Exception. Resolver in CSNS
Validation org. springframework. validation n n Validator Errors Handle input: Bind the request parameters to the command object Examples n Account. Validator(s) fail Form view success Call controller. on. Submit()
messages. properties <name, value> pairs A single place for output messages n n Easy to change I 18 N Need to declare a message. Source bean in Spring configuration file Can be used by <fmt> tags in JSTL
Display Errors Spring Tag Library n http: //static. springframework. org/spring/do cs/1. 2. 5/taglib/ <spring: has. Bind. Errors> A few other useful tags Example: account. jsp
Limitation of Spring Validation Server-side only Takes lots of coding for anything other than empty/white spaces Vs. Common-validator support in Struts Spring Modules https: //springmodules. dev. java. net/
Roadmap Web flow Controllers and validation Transactions and hibernate support Bits and pieces n n n Tomcat server setup Context, data source, and connection pooling Creating WAR files JSP pre-compilation Displaytag
Programmatic vs. Declarative Transaction Management Programmatic: void save. User( User u ) { transaction. start(); … transaction. end(); } Declarative: <transaction> <method> save. User </method> </transaction>
Spring Transaction Managers JDBC Hibernate n n V 3 Before V 3 JTA Object-Relational Bridge (ORB)
Configure Hibernate Transaction Manager Hibernate Transaction manager Session Factory Data Source
Transaction Attributes Propagation behaviors Isolation levels Read-only hints Transaction timeout period
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.
Adding Transaction Aspect Transaction. Proxy. Factory. Bean n target transaction. Manager transaction. Attribute. Source
Other Hibernate Support Hibernate. Template Open. Session. In. View. Filter and Open. Session. In. View. Interceptor
Roadmap Web flow Controllers and validation Transactions and hibernate support Bits and pieces n n n Tomcat server setup Context, data source, and connection pooling Creating WAR files JSP pre-compilation Displaytag
Tomcat Server Setup Unzip the Tomcat binary release package to a directory Copy the JDBC driver of your database to common/lib [Optional] Edit conf/server. xml to change the default port number
Context, Data Source, and Connection Pooling Connection pooling Configure Tomcat and DBCP n http: //tomcat. apache. org/tomcat-5. 5 doc/jndi-datasource-examples-howto. html
Creating WAR Files Understand the directory structure Use the war ANT task
JSP Pre-compilation Usually a JSP is converted to a servlet and then compiled into byte code when the JSP is request for the first time. JSP pre-compilation n n Eliminate the “first request overhead” Speed up development Tomcat provides JSP pre-compiler which can be used as an ANT task n Need to specify the compiled servlets in web. xml
Displaytag http: //displaytag. sourceforge. net/ Sortable columns and result paging
- Slides: 45