Using flash attributes Web MVC framework

4.3.9.RELEASE Spring Framework 527 The InternalResourceViewResolver handles the translation of view names and JSP pages, while the BeanNameViewResolver returns a view based on the name of a bean. See Resolving views with the ViewResolver interface for more details on how Spring looks up and instantiates a view. In this example, the content bean is a class that inherits from AbstractAtomFeedView , which returns an Atom RSS feed. For more information on creating an Atom Feed representation, see the section Atom Views. In the above configuration, if a request is made with an .html extension, the view resolver looks for a view that matches the texthtml media type. The InternalResourceViewResolver provides the matching view for texthtml . If the request is made with the file extension .atom , the view resolver looks for a view that matches the applicationatom+xml media type. This view is provided by the BeanNameViewResolver that maps to the SampleContentAtomView if the view name returned is content . If the request is made with the file extension .json , the MappingJackson2JsonView instance from the DefaultViews list will be selected regardless of the view name. Alternatively, client requests can be made without a file extension but with the Accept header set to the preferred media- type, and the same resolution of request to views would occur. Note If `ContentNegotiatingViewResolver’s list of ViewResolvers is not configured explicitly, it automatically uses any ViewResolvers defined in the application context. The corresponding controller code that returns an Atom RSS feed for a URI of the form http:localhostcontent.atom or http:localhostcontent with an Accept header of applicationatom+xml is shown below. Controller public class ContentController { private ListSampleContent contentList = new ArrayListSampleContent; GetMappingcontent public ModelAndView getContent { ModelAndView mav = new ModelAndView; mav.setViewName content ; mav.addObject sampleContentList , contentList; return mav; } }

22.6 Using flash attributes

Flash attributes provide a way for one request to store attributes intended for use in another. This is most commonly needed when redirecting — for example, the PostRedirectGet pattern. Flash attributes are saved temporarily before the redirect typically in the session to be made available to the request after the redirect and removed immediately. Spring MVC has two main abstractions in support of flash attributes. FlashMap is used to hold flash attributes while FlashMapManager is used to store, retrieve, and manage FlashMap instances. Flash attribute support is always on and does not need to enabled explicitly although if not used, it never causes HTTP session creation. On each request there is an input FlashMap with attributes passed from a previous request if any and an output FlashMap with attributes to save for a subsequent request. Both FlashMap instances are accessible from anywhere in Spring MVC through static methods in RequestContextUtils . 4.3.9.RELEASE Spring Framework 528 Annotated controllers typically do not need to work with FlashMap directly. Instead an RequestMapping method can accept an argument of type RedirectAttributes and use it to add flash attributes for a redirect scenario. Flash attributes added via RedirectAttributes are automatically propagated to the output FlashMap. Similarly, after the redirect, attributes from the input FlashMap are automatically added to the Model of the controller serving the target URL. Matching requests to flash attributes The concept of flash attributes exists in many other Web frameworks and has proven to be exposed sometimes to concurrency issues. This is because by definition flash attributes are to be stored until the next request. However the very next request may not be the intended recipient but another asynchronous request e.g. polling or resource requests in which case the flash attributes are removed too early. To reduce the possibility of such issues, RedirectView automatically stamps FlashMap instances with the path and query parameters of the target redirect URL. In turn the default FlashMapManager matches that information to incoming requests when looking up the input FlashMap . This does not eliminate the possibility of a concurrency issue entirely but nevertheless reduces it greatly with information that is already available in the redirect URL. Therefore the use of flash attributes is recommended mainly for redirect scenarios .

22.7 Building URIs