This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Contents »DispatcherServlet »Request flow in application »Meaning of Important terms »HandlerMapping Commonly Used handlerMappings Configuration Registering more than one handlerMapping Best Practices for designing handlerMapping »Interceptors Purpose Use cases Example Registering interceptor with URLHandlerMapping
Meaning of Important terms continue ViewResolver ViewResolver helps DispatcherServlet for getting instance of View that will render view Main Purpose of ViewResolver is to map logical view name with view Default ViewResolver for DispatcherServlet is InternalResourceViewResolver
View This object is responsible for rendering view org.springframework.web.servlet.View is basic interface available commonly used View implementation for JSP org.springframework.web.servlet.view.JstlView
HandlerMapping :- Configuration For SimpleUrlHandlerMapping
URL HandlerMapping
This handler mapping maps URL to controller based on Property URL is act as a key while bean id act a Value Here /logout.htm (i.e. Key) is mapped to bean with id logoutController (i.e. value)
HandlerMapping :- Best Practices 1] For large application divide application in small modules and for each module define one handler mapping if possible 2] Try to avoid using BeanNameUrlHanlderMappings since this scatters our URL mappings across multiple xml files 3] Define all handler mappings for whole application at one location
Spring Provides extremely useful functionality when we want to apply some specific functionalities to client requests Handler Interceptor process request before or after appropriate controller process request
SimpleFormController When to use SimpleFormController When we need to process simple forms Mandatory attributes SimpleFormController commadClass :- class to be bind as a command object formView :- logical name of form view Some other Imp attributes successView:- logical name of view after form submission validator :-
bean to validate form fields
Features Provide Separation of Validation logic from controller Binding of value objects struts lacks in it Limitations Controller chaining is difficult or not possible to implement. Struts Provide Action Chaining
Spring tag library <spring-bind> tag Purpose Used to access command object and error associated with command object Attributes It has only one attribute named path indicates bean or bean property being used e.g. in following listing we are accessing username attribute in comand object
Spring tag library :- BindStatus Object Spring provides org.springframework.web.servlet.support.BindStatus object available in page scope with name status Imp methods in BindStatus object available getDisplayValue() This method gives actual value of command attribute getErrorMessage() This method gives error associated with command attribute if any
While second listing shows definition of ViewResolver in XML bean factory When any client want to see Login page It will send request as http://localhost:8080/spring_mvc/security/login.htm 3rd Listing shows definition View object and corresponding view location
Spring I18n Spring provides CookieLocaleResolver which allow user to change application language on the fly Configuration <property name="interceptors"> <list>