Hi Aaron,

Aaron Stromas wrote on 10/07/14 3:44:
 > Is there no way of avoiding the global change,
 > to restricting its scope?

The following post shows how to change the population strategy per 
action bean.

http://article.gmane.org/gmane.comp.java.stripes.user/5316

...and here's the one I have used in a few project (slightly edited for 
reducing lines).

-- population strategy --

public class SelectivePopulationStrategy implements PopulationStrategy {
   private static final Log LOG = 
Log.getInstance(SelectivePopulationStrategy.class);
   private Configuration config;
   private PopulationStrategy defaultDelegate;
   private Map<Class<? extends PopulationStrategy>, PopulationStrategy> 
delegates = new HashMap<Class<? extends PopulationStrategy>, 
PopulationStrategy>();
   private Map<Class<? extends ActionBean>, PopulationStrategy> 
actionBeanStrategies = new HashMap<Class<? extends ActionBean>, 
PopulationStrategy>();

   protected PopulationStrategy getDelegate(InputTagSupport tag) throws 
StripesJspException {
     ActionBean actionBean = tag.getActionBean();
     if (actionBean == null)
       return defaultDelegate;
     Class<? extends ActionBean> beanType = actionBean.getClass();
     PopulationStrategy delegate = actionBeanStrategies.get(beanType);
     if (delegate != null)
       return delegate;
     CustomPopulationStrategy annotation = 
beanType.getAnnotation(CustomPopulationStrategy.class);
     if (annotation == null)
       delegate = defaultDelegate;
     else {
       Class<? extends PopulationStrategy> type = annotation.value();
       delegate = delegates.get(type);
       if (delegate == null) {
         try {
           delegate = type.newInstance();
           delegate.init(config);
           delegates.put(type, delegate);
         } catch (Exception e) {
           delegate = defaultDelegate;
           LOG.info("Could not instantiate population strategy of name 
[" + type + "]", e);
         }
       }
     }
     actionBeanStrategies.put(beanType, delegate);
     return delegate;
   }

   public Object getValue(InputTagSupport tag) throws StripesJspException {
     PopulationStrategy strategy = getDelegate(tag);
     Object value = (strategy).getValue(tag);
     return value;
   }

   public void init(Configuration configuration) throws Exception {
     this.config = configuration;
     defaultDelegate = new DefaultPopulationStrategy();
     defaultDelegate.init(config);
   }
}

-- annotation --
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomPopulationStrategy {
   Class<? extends PopulationStrategy> value() default 
DefaultPopulationStrategy.class;
}

-- how to use --
Drop the SelectivePopulationStrategy into stripes' extension package and 
annotate the action bean which uses BeanFirstPopulationStrategy as follows.

@CustomPopulationStrategy(BeanFirstPopulationStrategy.class)
public class YourActionBean ...

Other (unannotated) action beans will use the DefaultPopulationStrategy.

Regards,
Iwao


------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to