Hi Chut,
If the goal is to mimic ASP.NET VIEWSTATE why not just call it @ViewState?
Sorry if I'm missing something.
Cheers,
--
Samuel Santos
http://www.samaxes.com/
On Thu, Apr 12, 2012 at 4:08 AM, Chut Yee <yeec...@gmail.com> wrote:
> Here it is. I think the name BrowserScope is better. What do you think?
>
> ----------------------------------------------------------
> package org.stripesstuff.plugin.pagescope;
>
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Retention;
> import java.lang.annotation.RetentionPolicy;
> import java.lang.annotation.Target;
>
> /**
> * For annotating fields in ActionBean for persisting to the web browser.
> *
> * @see PageScopeInterceptor
> *
> * @author y...@eutama.com
> *
> */
> @Retention(RetentionPolicy.RUNTIME)
> @Target(ElementType.FIELD)
> public @interface PageScope {
> }
>
>
> ----------------------------------------------------------
> package org.stripesstuff.plugin.pagescope;
>
> import java.lang.reflect.Field;
> import java.util.HashMap;
>
> import org.apache.log4j.Logger;
>
> import net.sourceforge.stripes.action.Resolution;
> import net.sourceforge.stripes.controller.ExecutionContext;
> import net.sourceforge.stripes.controller.Interceptor;
> import net.sourceforge.stripes.controller.Intercepts;
> import net.sourceforge.stripes.controller.LifecycleStage;
> import net.sourceforge.stripes.util.Base64;
> import net.sourceforge.stripes.util.CryptoUtil;
>
> /**
> * Persisting actionbean fields to the web browser:
> * <ul>
> * <li>After LifecycleStage.ActioBeanResolution - fields annotated with
> * {@link PageScope} will be restored from the _pageScope parameter</li>
> * <li>After LifecycleStage.EventHandling - fields annotated with
> * {@link PageScope} will be serialized into a request attribute named
> * _pageScope</li>
> * <li>JSP: adds a hidden field <input type="hidden" name="_pageScope"
> * value="${_pageScope}" /> to form</li>
> * <ul>
> * <br/>
> *
> * @author y...@eutama.com
> *
> */
> @Intercepts({LifecycleStage.ActionBeanResolution,
> LifecycleStage.EventHandling})
> public class PageScopeInterceptor implements Interceptor
> {
> private static final String PAGE_SCOPE_KEY = "_pageScope";
>
> private static final Logger logger = Logger
> .getLogger(PageScopeInterceptor.class);
>
> public Resolution intercept(ExecutionContext context) throws
> Exception
> {
> Resolution resolution = context.proceed();
>
> // Restores annotated fields from _pageScope parameter
> if (LifecycleStage.ActionBeanResolution.equals(context
> .getLifecycleStage())) {
> bindPageScopeFields(context);
> }
>
> // serialise annotated fields into _pageScope parameter
> if
> (LifecycleStage.EventHandling.equals(context.getLifecycleStage())) {
> serializePageScopeFields(context);
> }
>
> return resolution;
> }
>
> private void serializePageScopeFields(ExecutionContext context)
> throws IllegalAccessException
> {
> HashMap<String, Object> map = new HashMap<String,
> Object>(5);
> collectPageScopeFields(context.getActionBean(), map);
>
> if (map.size() > 0) {
> String pageScopeStr =
> CryptoUtil.encrypt(Base64.encodeObject(map));
> logger.debug("pagescope length:" +
> pageScopeStr.length());
>
> context.getActionBeanContext().getRequest()
> .setAttribute(PAGE_SCOPE_KEY,
> pageScopeStr);
> }
> }
>
> private void collectPageScopeFields(Object actionBean,
> HashMap<String, Object> map) throws
> IllegalAccessException
> {
> Class<?> clazz = actionBean.getClass();
> while (clazz != null) {
> for (Field field : clazz.getDeclaredFields()) {
> // add field with PageScope annotation to
> map
> if (field.getAnnotation(PageScope.class) !=
> null) {
> field.setAccessible(true);
> if (field.get(actionBean) != null)
> map.put(field.getName(),
> field.get(actionBean));
> }
> }
> clazz = clazz.getSuperclass();
> }
> }
>
> private void bindPageScopeFields(ExecutionContext context)
> throws IllegalAccessException
> {
> String pageScopeStr =
> context.getActionBeanContext().getRequest()
> .getParameter(PAGE_SCOPE_KEY);
>
> if (pageScopeStr == null)
> return;
>
> pageScopeStr = CryptoUtil.decrypt(pageScopeStr);
>
> @SuppressWarnings("unchecked")
> HashMap<String, Object> map = (HashMap<String, Object>)
> Base64
> .decodeToObject(pageScopeStr);
>
> bindPageScopeFields(context.getActionBean(), map);
> }
>
> private void bindPageScopeFields(Object actionBean,
> HashMap<String, Object> map) throws
> IllegalAccessException
> {
> Class<?> clazz = actionBean.getClass();
> while (clazz != null) {
> if (map.size() == 0)
> break;
> for (Field field : clazz.getDeclaredFields()) {
> if (map.size() == 0)
> break;
> // try to bind field with PageScope
> annotation from map
> if (field.getAnnotation(PageScope.class) !=
> null) {
> field.setAccessible(true);
> if (map.get(field.getName()) !=
> null) {
> field.set(actionBean,
> map.get(field.getName()));
> // remove from map so that
> superclass fields of the same
> // name won't be bound
> map.remove(field.getName());
> }
> }
> }
> clazz = clazz.getSuperclass();
> }
> }
> }
>
>
>
>
> ------------------------------------------------------------------------------
> For Developers, A Lot Can Happen In A Second.
> Boundary is the first to Know...and Tell You.
> Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
> http://p.sf.net/sfu/Boundary-d2dvs2
> _______________________________________________
> Stripes-users mailing list
> Stripes-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/stripes-users
>
------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users