[ 
https://issues.apache.org/struts/browse/SHALE-425?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_40601
 ] 

Matthias Wuttke commented on SHALE-425:
---------------------------------------

Don't know if this can be helpful:

package teuto.base.shale;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import org.apache.shale.view.Constants;
import org.apache.shale.view.ViewControllerMapper;
import org.apache.shale.view.impl.DefaultViewControllerMapper;

/**
 * ViewControllerMapper that allows for dynamic
 * registration of mappings at runtime or application startup.
 * Provides convenience methods for registering a mapping
 * for the currently executing controller bean and
 * the view currently rendered.
 * @author Matthias Wuttke
 */
public class MemoryViewControllerMapper
extends DefaultViewControllerMapper {

        /**
         * Returns the name of the controller bean to
         * be used with the passed view id. Falls back
         * to the default Shale implementation if there
         * is no dynamically registered view controller.
         * @param viewId view id
         * @return controller name
         */
        public String mapViewId(String viewId) {
                String beanName = mapping.get(viewId);
                if (beanName != null)
                        return beanName;
                
                return super.mapViewId(viewId);
        }
        
        /**
         * Registers a mapping for the given view id and
         * bean name.
         * @param viewId id of the view
         * @param beanName name of the controller bean
         */
        public void registerMapping(String viewId, String beanName) {
                if (!mapping.containsKey(viewId))
                        mapping.put(viewId, beanName);
        }
        
        /**
         * Registers the given bean for the view that is
         * currently being rendered. 
         * @param beanName name of the controller bean
         */
        public void registerMapping(String beanName) {
                FacesContext ctx = FacesContext.getCurrentInstance();
                UIViewRoot viewRoot = ctx.getViewRoot();
                if (viewRoot != null) {
                        String viewId = viewRoot.getViewId();
                        registerMapping(viewId, beanName);
                }       
        }
        
        /**
         * Convenience method for registering multiple
         * mappings at once. This can be used to initialize
         * this class from a properties file.
         * @param mappings
         */
        public void setMappings(Properties mappings) {
                for (Object key : mappings.keySet())
                        mapping.put((String)key, 
mappings.getProperty((String)key));
        }
                
        /**
         * Convenience method for retrieving the instance of
         * this object from the Faces application context.
         * @return instance or null
         */
        public static MemoryViewControllerMapper getInstance() {
                FacesContext ctx = FacesContext.getCurrentInstance();
                if (ctx != null) {
                        ViewControllerMapper vcm = (ViewControllerMapper) 
                                
ctx.getExternalContext().getApplicationMap().get(Constants.VIEW_MAPPER);
                        if (vcm instanceof MemoryViewControllerMapper)
                                return (MemoryViewControllerMapper)vcm;
                }
                
                return null;
        }
        
        private Map<String, String> mapping =
                new HashMap<String, String>();

}


> ViewControllerMapper allows mapping only to one bean
> ----------------------------------------------------
>
>                 Key: SHALE-425
>                 URL: https://issues.apache.org/struts/browse/SHALE-425
>             Project: Shale
>          Issue Type: Improvement
>          Components: View
>    Affects Versions: 1.0.4
>            Reporter: Matthias Wuttke
>            Priority: Minor
>
> ViewControllerMapper.mapViewId(String viewId) allows only to map a view to a 
> single bean. A key feature of JSF (IMHO) is the ability to have multiple 
> backing beans / view controllers that contribute to a single page. An 
> extension of this interface could allow other mapping strategies to associate 
> multiple beans with a given page that can then receive phase change events. A 
> possiblte mapping strategy would be a XmlViewControllerMapper which takes a 
> XML file that associates view ids with bean names.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to