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 :- Registering More than one HandlerMapping Need For large applications which need modular division of code Here we defined 2 handler mappings With property order is initialized
<property name="order"> 0
Value of order property ↓ priority ↑ here bean defined with id publicUrlMapping has highest priority
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
Interceptors :- Example If you want to create your own interceptor you can choose to extend your class HandlerInterceptorAdapter or implement HandlerInterceptor When Interceptor handle request? It provides us functionality to process request before or after appropriate controller has processed it
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(false); if (session == null) { response.sendRedirect(request.getContextPath() + "/" + this.viewLocationForInvalidUrlAccess); return false; } else { // user is having preExisting session then return true; } }
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
Spring I18n Spring provides CookieLocaleResolver which allow user to change application language on the fly Configuration <property name="interceptors"> <list>