Spring Mvc

  • Uploaded by: api-3850119
  • 0
  • 0
  • June 2020
  • PDF

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


Overview

Download & View Spring Mvc as PDF for free.

More details

  • Words: 1,408
  • Pages: 24
Spring MVC September 2007

Wednesday, November 25, 2009 © Kanbay Incorporated - All Rights Reserved

CONFIDENTIAL

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

Wednesday, November 25, 2009 | © 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

Wednesday, November 25, 2009 | © 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-class> org.springframework.web.servlet.DispatcherServlet <param-name>namespace <param-value>XmlWebBeanFactory 1 <servlet-mapping> <servlet-name>springWebMvcPract *.htm

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

4

Request flow in application

Wednesday, November 25, 2009 | © 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

Wednesday, November 25, 2009 | © 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

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

7

HandlerMapping :- Commonly Used HandlerMappings Commonly used HandlerMappings

Name

Details

BeanNameUrlHandlerMapping

Maps controller to URL based on By default DispatcherServlet use this implementation bean name

SimpleUrlHandlerMapping

Maps Controller to URL based on Property

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

Imp note

CONFIDENTIAL

8

HandlerMapping :- Configuration For BeanNameUrlHandlerMapping


When client send request to application as

class="com.gp.common.security.web.con troller.LoginController">

http://www.localhost.com:8080/spring_mvc/s ecurity/login.htm

<property name="formView" value="login" />

Dispatcher servlet will dispatch request to LoginController

<property name="successView" value="home" />

Imp note

<property name="validator" ref="loginValidator" />

Since BeanNameUrlHandlerMapping is default in spring application it is not necessary to define in XML bean factory



When it is only URLHandlerMapper in application

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

9

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)

class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/logout.htm">logoutController
Controller definition

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

10

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

class="org.springframework.web.servlet.handler.SimpleU rlHandlerMapping"> <property name="mappings"> <props> <prop key="/logout.htm">logoutController

<property name="order"> 1


Wednesday, November 25, 2009 | © 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 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

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

12

Interceptors :- Purpose

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

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

13

Interceptors :- Use cases 1] Our URLs can be classified on Macro level in 2 parts Public URL e.g. login page, forgot password page etc Restricted URL which need user’s authentication e.g.. Logout In this use case we need to check if user is having credentials to access this pages 2] We need to capture some important information related to client when client send request to server e.g.. Client’s IP address, his/her Locale, accessed Pages etc This information can be logged either in file or persisted in database 3] Increasing user experience This kind of use case can play important role when application moved to production mode and we need to check frequent visited pages by client Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

14

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; } }

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

15

Interceptors :- Registering Interceptor with URLHandlerMapping <property name="interceptors"> <list> <property name="order"> 0

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

16

Controllers :- classification

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

17

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

Wednesday, November 25, 2009 | © 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

<spring: bind path="command. username"> <%=status.getErrorMessage()%>

Wednesday, November 25, 2009 | © 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 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

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

20

ViewResolver :- ResourceBundleViewResolver First Listing shows bean definition


For LoginController this is nothing but child of SimpleFormController

class="com.gp.common.security.web.controller.Login Controller"> <property name="formView" value="login" />

While second listing shows definition of

<property name="successView" value="home" />

ViewResolver in XML bean factory

<property name="validator" ref="loginValidator" />

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

<property name="basename" value="views" /> login.class=org.springframework.web.servlet.view.Jst lView login.url=/WEB-INF/views/jsp/common/login.jsp

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

21

Spring I18n Spring provides CookieLocaleResolver which allow user to change application language on the fly Configuration <property name="interceptors"> <list>

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

22

Reference »Pro Spring By Rob Harrop Apress publications

»Spring In Action By Craig Walls Manning Publications

Wednesday, November 25, 2009 | © Kanbay Incorporated. All Rights Reserved

CONFIDENTIAL

23

Kanbay WORLDWIDE HEADQUARTERS

Wednesday, November 25, 2009 © Kanbay Incorporated - All Rights Reserved

6400 SHAFER COURT ROSEMONT, ILLINOIS USA 60018 Tel. 847.384.6100 Fax 847.384.0500 WWW.KANBAY.COM

CONFIDENTIAL

Related Documents

Spring Mvc
June 2020 13
Spring Mvc
May 2020 7
Spring Mvc
November 2019 14
Spring Mvc 3
May 2020 3
Mvc
November 2019 39
Spring Mvc Step By Step
November 2019 4