September 2007
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
2
Contents continue
»Controllers
Classification of spring controllers
SimpleFormController
»Spring tag library
<spring-bind> tag
BindStatus object
»ViewResolver
»Spring I18n
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
3
DispatcherServlet
It is Front controller for Spring web mvc
Configuration in web.xml
<servlet>
<servlet-name>springWebMvcPract</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>namespace</param-name>
<param-value>XmlWebBeanFactory</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springWebMvcPract</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
4
Request flow in application
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
5
Meaning of Important terms
DispatherServlet
first component which receive request is DispathcerServlet
Handler Mapping
Holds Mapping between URL and controller objects in application
Default Handler Mapping is BeanNameUrlHandlerMapping
Controller
org.springframework.web.servlet.mvc.Controller is basic controller available and all spring’s
controllers implements this interface
ModelAndView
Spring controller can return instance of ModelAndView Default Handler.
This object act as helper for DispatcherServlet in getting view
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
6
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
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
7
HandlerMapping :- Commonly Used HandlerMappings
Commonly used HandlerMappings
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
8
HandlerMapping :- Configuration
For BeanNameUrlHandlerMapping <bean name="/security/login.htm"
class="com.gp.common.security.web.co
When client send request to application as ntroller.LoginController">
http://www.localhost.com:8080/spring_mvc/s <property name="formView"
ecurity/login.htm value="login" />
Dispatcher servlet will dispatch request to
<property name="successView"
LoginController
value="home" />
<property name="validator"
Imp note
ref="loginValidator" />
Since BeanNameUrlHandlerMapping is
default in spring application it is not </bean>
necessary to define in XML bean factory
When it is only URLHandlerMapper in
application
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
9
HandlerMapping :- Configuration
For SimpleUrlHandlerMapping URL HandlerMapping
<bean id="restrictedUrlMapping"
This handler mapping maps URL to
controller based on Property class="org.springframework.web.servlet.handler.Si
mpleUrlHandlerMapping">
URL is act as a key while bean id act a <property name="mappings">
Value <props>
<prop
key="/logout.htm">logoutController</prop>
Here /logout.htm (i.e. Key) is mapped to
bean with id logoutController (i.e. value) </props>
</property>
</bean>
Controller definition
<bean id="logoutController"
class="com.gp.common.security.web.controller.Lo
goutController">
<property name="logoutView"
value="security/login.htm" />
</bean>
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
10
HandlerMapping :- Registering More than one HandlerMapping
Need <bean id="publicUrlMapping"
class="org.springframework.web.servlet.handler.Bean
For large applications which need modular NameUrlHandlerMapping">
division of code <property name="order">
Here we defined 2 handler mappings <value>0</value>
</property>
With property order is initialized
</bean>
<bean id="restrictedUrlMapping"
Value of order property ↓ priority ↑
class="org.springframework.web.servlet.handler.Simpl
eUrlHandlerMapping">
here bean defined with id publicUrlMapping <property name="mappings">
has highest priority <props>
<prop key="/logout.htm">logoutController</prop>
</props>
</property>
<property name="order">
<value>1</value>
</property>
</bean>
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
11
HandlerMapping :- Best Practices
1] For large application divide application in small modules and for each
module define one handler mapping if possible
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
12
Interceptors :- Purpose
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
13
Interceptors :- Use cases
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
14
Interceptors :- Example
If you want to create your own interceptor
you can choose to extend your class public boolean preHandle(HttpServletRequest request,
HandlerInterceptorAdapter or implement
HttpServletResponse response, Object handler) throws
HandlerInterceptor Exception {
When Interceptor handle request? HttpSession session =
request.getSession(false);
It provides us functionality to process if (session == null) {
request before or after appropriate controller
has processed it response.sendRedirect(request.getContextPath() + "/"
+ this.viewLocationForInvalidUrlAccess);
return false;
} else {
// user is having
preExisting session then
return true;
}
}
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
15
Interceptors :- Registering Interceptor with URLHandlerMapping
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
</bean>
<bean id="publicUrlMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
<property name="order">
<value>0</value>
</property>
</bean>
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
16
Controllers :- classification
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
17
SimpleFormController
When to use SimpleFormController
When we need to process simple forms
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
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
18
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
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
19
Spring tag library :- BindStatus Object
Spring provides
org.springframework.web.servlet.support.BindStatus
object available in page scope with name status
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
20
ViewResolver :- ResourceBundleViewResolver
First Listing shows bean definition <bean name="/security/login.htm"
login.class=org.springframework.web.servlet.view.J
stlView
login.url=/WEB-INF/views/jsp/common/login.jsp
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
21
Spring I18n
Spring provides CookieLocaleResolver which allow
user to change application language on the fly
Configuration
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
</bean>
<bean id="publicUrlMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
22
Reference
»Pro Spring
By Rob Harrop
Apress publications
»Spring In Action
By Craig Walls
Manning Publications
Thursday, October 16, 2008 | © Kanbay Incorporated. All Rights Reserved CONFIDENTIAL
23
Kanbay
WORLDWIDE HEADQUARTERS 6400 SHAFER COURT ROSEMONT, ILLINOIS USA 60018
Tel. 847.384.6100 Fax 847.384.0500 WWW.KANBAY.COM