|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.riotfamily.common.web.collaboration.SharedProperties
public class SharedProperties
Class that facilitates collaboration between controllers.
The Spring DispatcherServlet performs an attribute clean-up after an include so included controllers can't expose request attributes to other controllers unless you turn this feature off, which usually isn't a good idea.
Another level of complexity comes into play when you want to cache the output of a controller that contributes information needed by other collaborators. If a cached version is served, the controller's handleRequest() method is not invoked (which after all is the purpose of a cache), therefore the controller has no chance to set any request attributes.
One solution would be to cache the dependent controller too. But in this case we would have to ensure that the cached version of the first controller is invalidated as soon as the dependent cache item is removed from the cache. As there is no easy way to achieve this, the SharedProperites class uses another approach:
The SharedPropertiesInterceptor
exposes a HashMap as request
attribute before the top-level handler is executed. A controller that wants
to hand data on to a subsequent controller can use the
setProperty(HttpServletRequest, String, String)
method to add
entries to this map. Because the map is already present before the
the include is performed, it won't be cleaned up by the DispatcherServlet.
Another controller can now invoke
getProperty(HttpServletRequest, String)
to retrieve the previously
set values.
Currently shared properties are limited to Strings. The reason for this is that Cachius is aware of shared properties and caches them along with the actual output. It's basically a precaution to prevent users from caching their business objects. The fact that Cachius knows about shared properties allows us to serve cached content and still expose data to others.
A common use case for this is when you want to add information to the document title (or a meta-tag) that is provided by a controller which is deeply nested inside a template and would be processed too late. An elegant solution is to use the push-up feature of the TemplateController, which takes the nested controller out of the regular rendering flow and processes it first. This pushed-up controller can now expose shared properties which are then available everywhere else on the page, even if the pushed-up content comes from the cache.
Note: You can easily access shared properties in your FreeMarker views via
the common.getSharedProperty()
function and the
common.setSharedProperty()
macro.
Constructor Summary | |
---|---|
SharedProperties()
|
Method Summary | |
---|---|
static Map<String,String> |
getDiff(HttpServletRequest request,
Map<String,String> snapshot)
Returns a map containing all properties that have been added or modified since the snapshot was made. |
static String |
getProperty(HttpServletRequest request,
String key)
Retrieves a shared property. |
static Map<String,String> |
getSnapshot(HttpServletRequest request)
Returns all shared properties currently set. |
static void |
setProperties(HttpServletRequest request,
Map<String,String> properties)
Sets multiple shared property at once. |
static void |
setProperty(HttpServletRequest request,
String key,
String value)
Sets a shared property. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public SharedProperties()
Method Detail |
---|
public static void setProperty(HttpServletRequest request, String key, String value)
IllegalStateException
- if no properties map is found in the requestpublic static void setProperties(HttpServletRequest request, Map<String,String> properties)
public static String getProperty(HttpServletRequest request, String key)
IllegalStateException
- if no properties map is found in the requestpublic static Map<String,String> getSnapshot(HttpServletRequest request)
public static Map<String,String> getDiff(HttpServletRequest request, Map<String,String> snapshot)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |