You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Dave Brondsema <da...@brondsema.net> on 2005/10/05 14:28:01 UTC

Re: Portlet Modes (was: Issue with MyFacesGenericPortlet)

Patrick Dalla Bernardina wrote:
> How can I call an managed bean action when portlet mode (EDIT,VIEW) is
> changed and process the navigation rules to discover the page to be
> rendered?

You can use http://wiki.apache.org/myfaces/UsingPortletModes as a
starting point.

To invoke a managed bean action, something like this will work I think:

ValueBinding binding =
FacesContext.getCurrentInstance().getApplication().createValueBinding("#{myBean.someAction}");
Object methodResult = binding.getValue(FacesContext.getCurrentInstance());


-- 
Dave Brondsema
Software Developer
Cornerstone University

Re: Portlet Modes

Posted by Patrick Dalla Bernardina <pa...@jfes.trf2.gov.br>.
Ok,

I had already seen this wiki page. An implemented a subclass o 
MyFacesGenericPortlet that implements these methods.

But it has some limitations.

First: I can't call an managed bean action when the mode is changed.
Second: The rules in navigation rules are not followed.
Third: It don't reset faces navigation state. So when I enter in edit 
mode, make some faces navigation, and go back to view mode everything 
work well. But when I maximize the portlet the last edit page I were 
before changing to view mode is displayed and not the view page I were.

I would like some suggestions on how to implement these requisites

Thanks


Dave Brondsema wrote:

>Patrick Dalla Bernardina wrote:
>  
>
>>How can I call an managed bean action when portlet mode (EDIT,VIEW) is
>>changed and process the navigation rules to discover the page to be
>>rendered?
>>    
>>
>
>You can use http://wiki.apache.org/myfaces/UsingPortletModes as a
>starting point.
>
>To invoke a managed bean action, something like this will work I think:
>
>ValueBinding binding =
>FacesContext.getCurrentInstance().getApplication().createValueBinding("#{myBean.someAction}");
>Object methodResult = binding.getValue(FacesContext.getCurrentInstance());
>
>
>  
>


Re: Portlet Modes

Posted by Patrick Dalla Bernardina <pa...@jfes.trf2.gov.br>.
New release of doEdit and doView:

In this code, facesRender is called.
An action configured in EDIT_ACTION portlet preference is called and the 
result is passed to handleNavigation.
handle navigation looks for an navigation rule where "from" can be "*" 
or value of DEFAULT_EDIT portlet init parameter (defaultEdit variable is 
loaded in method init() of portlet) and the "outcome" is the result of 
the action.

I've done little test but it seems to work.


    public void doEdit(RenderRequest request, RenderResponse 
response)throws PortletException, IOException {
       Boolean isPortletModeChanged = 
(Boolean)request.getAttribute(ATTR_PORTLET_MODE_CHANGED);

       if (isPortletModeChanged.booleanValue()) {
           response.setContentType("text/html");

            FacesContext fctx = facesContext(request, response);
            if(fctx!=null){
                editAction = 
request.getPreferences().getValue("EDIT_ACTION",editAction);

                MethodBinding mb = 
fctx.getApplication().createMethodBinding(editAction, null);
                if(mb!=null){
                    try{
                        Object methodResult = mb.invoke(fctx, null);
                        if(fctx.getViewRoot()==null){
                            
fctx.setViewRoot(fctx.getApplication().getViewHandler().createView(fctx,defaultEdit));
                        }
                        
fctx.getApplication().getNavigationHandler().handleNavigation(fctx, 
editAction, methodResult.toString());
                    }catch(EvaluationException e){
                        /* if the managed bean is not faound ignore the 
calling */
                    }
                }
            }
       }

       facesRender(request, response);
    }






Patrick Dalla Bernardina wrote:

> What is setPortletRequestFlag(request); for?
>
>
>
> Dave Brondsema wrote:
>
>> Cool.  Make sure you call setPortletRequestFlag(request); before
>> nonFacesRequest.
>>
>> It might be useful to include part of this on the wiki page as an
>> example for anyone else who wants to do that.
>>
>> Patrick Dalla Bernardina wrote:
>>  
>>
>>> With your help I've made an improvement to the implementation in 
>>> wiki page.
>>>
>>> Before calling nonFacesRequest I call an action of an bean called
>>> PortletBackBean.
>>> If this bean is not declared in managed beans, no method is called.
>>> I'm showing only doEdit, but the same can be applied to doView.
>>>
>>>
>>>   public void doEdit(RenderRequest request, RenderResponse
>>> response)throws PortletException, IOException {
>>>      Boolean isPortletModeChanged =
>>> (Boolean)request.getAttribute(ATTR_PORTLET_MODE_CHANGED);
>>>
>>>      if (isPortletModeChanged.booleanValue()) {
>>>          response.setContentType("text/html");
>>>
>>>           FacesContext fctx = facesContext(request, response);
>>>           if(fctx!=null){
>>>               MethodBinding mb =
>>> fctx.getApplication().createMethodBinding("#{PortletBackBean.edit}", 
>>> null);
>>>               if(mb!=null){
>>>                   try{
>>>                       Object methodResult = mb.invoke(fctx, null);
>>>                   }catch(PropertyNotFoundException e){
>>>                       /* if the managed bean is not faound ignore the
>>> calling*/
>>>                   }
>>>               }
>>>           }
>>>
>>>          nonFacesRequest(request, response, defaultEdit);
>>>                   return;
>>>      }
>>>
>>>      facesRender(request, response);
>>>   }
>>>
>>>
>>>
>>>
>>> Dave Brondsema wrote:
>>>
>>>   
>>>
>>>> Patrick Dalla Bernardina wrote:
>>>>
>>>>
>>>>     
>>>>
>>>>> How can I call an managed bean action when portlet mode 
>>>>> (EDIT,VIEW) is
>>>>> changed and process the navigation rules to discover the page to be
>>>>> rendered?
>>>>>  
>>>>>       
>>>>
>>>> You can use http://wiki.apache.org/myfaces/UsingPortletModes as a
>>>> starting point.
>>>>
>>>> To invoke a managed bean action, something like this will work I 
>>>> think:
>>>>
>>>> ValueBinding binding =
>>>> FacesContext.getCurrentInstance().getApplication().createValueBinding("#{myBean.someAction}"); 
>>>>
>>>>
>>>> Object methodResult =
>>>> binding.getValue(FacesContext.getCurrentInstance());
>>>>
>>>>
>>>>
>>>>
>>>>     
>>>
>>
>>
>>  
>>
>
>


Re: Portlet Modes

Posted by Dave Brondsema <da...@brondsema.net>.
This description is off the top of my head, but I know for sure that I
had problems when I didn't set it.

There are some places in MyFaces where it needs to cast
ExternalContext.getContext() as either a PortletContext or a
ServletContext.  MyFaces shouldn't require having a portlet.jar so it
cannot make the determination of the type of request based on
"instanceof" checks and casting.  So it determines the request type with
an attribute flag.  setPortletRequestFlag() sets that.

I figured this out by looking at the source and reading some discussion
in the JIRA issue tracker regarding the initial creation of
MyFacesGenericPortlet.  Look into those areas if you want more info.

Dave

Patrick Dalla Bernardina wrote:
> What is setPortletRequestFlag(request); for?
> 
> 
> 
> Dave Brondsema wrote:
> 
>> Cool.  Make sure you call setPortletRequestFlag(request); before
>> nonFacesRequest.
>>
>> It might be useful to include part of this on the wiki page as an
>> example for anyone else who wants to do that.
>>
>> Patrick Dalla Bernardina wrote:
>>  
>>
>>> With your help I've made an improvement to the implementation in wiki
>>> page.
>>>
>>> Before calling nonFacesRequest I call an action of an bean called
>>> PortletBackBean.
>>> If this bean is not declared in managed beans, no method is called.
>>> I'm showing only doEdit, but the same can be applied to doView.
>>>
>>>
>>>   public void doEdit(RenderRequest request, RenderResponse
>>> response)throws PortletException, IOException {
>>>      Boolean isPortletModeChanged =
>>> (Boolean)request.getAttribute(ATTR_PORTLET_MODE_CHANGED);
>>>
>>>      if (isPortletModeChanged.booleanValue()) {
>>>          response.setContentType("text/html");
>>>
>>>           FacesContext fctx = facesContext(request, response);
>>>           if(fctx!=null){
>>>               MethodBinding mb =
>>> fctx.getApplication().createMethodBinding("#{PortletBackBean.edit}",
>>> null);
>>>               if(mb!=null){
>>>                   try{
>>>                       Object methodResult = mb.invoke(fctx, null);
>>>                   }catch(PropertyNotFoundException e){
>>>                       /* if the managed bean is not faound ignore the
>>> calling*/
>>>                   }
>>>               }
>>>           }
>>>
>>>          nonFacesRequest(request, response, defaultEdit);
>>>                   return;
>>>      }
>>>
>>>      facesRender(request, response);
>>>   }
>>>
>>>
>>>
>>>
>>> Dave Brondsema wrote:
>>>
>>>   
>>>
>>>> Patrick Dalla Bernardina wrote:
>>>>
>>>>
>>>>     
>>>>
>>>>> How can I call an managed bean action when portlet mode (EDIT,VIEW) is
>>>>> changed and process the navigation rules to discover the page to be
>>>>> rendered?
>>>>>  
>>>>>       
>>>>
>>>> You can use http://wiki.apache.org/myfaces/UsingPortletModes as a
>>>> starting point.
>>>>
>>>> To invoke a managed bean action, something like this will work I think:
>>>>
>>>> ValueBinding binding =
>>>> FacesContext.getCurrentInstance().getApplication().createValueBinding("#{myBean.someAction}");
>>>>
>>>>
>>>> Object methodResult =
>>>> binding.getValue(FacesContext.getCurrentInstance());
>>>>
>>>>
>>>>
>>>>
>>>>     
>>
>>
>>
>>  
>>
> 


-- 
Dave Brondsema
Software Developer
Cornerstone University

Re: Portlet Modes

Posted by Patrick Dalla Bernardina <pa...@jfes.trf2.gov.br>.
What is setPortletRequestFlag(request); for?



Dave Brondsema wrote:

>Cool.  Make sure you call setPortletRequestFlag(request); before
>nonFacesRequest.
>
>It might be useful to include part of this on the wiki page as an
>example for anyone else who wants to do that.
>
>Patrick Dalla Bernardina wrote:
>  
>
>>With your help I've made an improvement to the implementation in wiki page.
>>
>>Before calling nonFacesRequest I call an action of an bean called
>>PortletBackBean.
>>If this bean is not declared in managed beans, no method is called.
>>I'm showing only doEdit, but the same can be applied to doView.
>>
>>
>>   public void doEdit(RenderRequest request, RenderResponse
>>response)throws PortletException, IOException {
>>      Boolean isPortletModeChanged =
>>(Boolean)request.getAttribute(ATTR_PORTLET_MODE_CHANGED);
>>
>>      if (isPortletModeChanged.booleanValue()) {
>>          response.setContentType("text/html");
>>
>>           FacesContext fctx = facesContext(request, response);
>>           if(fctx!=null){
>>               MethodBinding mb =
>>fctx.getApplication().createMethodBinding("#{PortletBackBean.edit}", null);
>>               if(mb!=null){
>>                   try{
>>                       Object methodResult = mb.invoke(fctx, null);
>>                   }catch(PropertyNotFoundException e){
>>                       /* if the managed bean is not faound ignore the
>>calling*/
>>                   }
>>               }
>>           }
>>
>>          nonFacesRequest(request, response, defaultEdit);
>>                   return;
>>      }
>>
>>      facesRender(request, response);
>>   }
>>
>>
>>
>>
>>Dave Brondsema wrote:
>>
>>    
>>
>>>Patrick Dalla Bernardina wrote:
>>> 
>>>
>>>      
>>>
>>>>How can I call an managed bean action when portlet mode (EDIT,VIEW) is
>>>>changed and process the navigation rules to discover the page to be
>>>>rendered?
>>>>  
>>>>        
>>>>
>>>You can use http://wiki.apache.org/myfaces/UsingPortletModes as a
>>>starting point.
>>>
>>>To invoke a managed bean action, something like this will work I think:
>>>
>>>ValueBinding binding =
>>>FacesContext.getCurrentInstance().getApplication().createValueBinding("#{myBean.someAction}");
>>>
>>>Object methodResult =
>>>binding.getValue(FacesContext.getCurrentInstance());
>>>
>>>
>>> 
>>>
>>>      
>>>
>
>
>  
>


Re: Portlet Modes

Posted by Patrick Dalla Bernardina <pa...@jfes.trf2.gov.br>.
It can be improved by getting the action to be executed from portlet 
preferences.

instead of

fctx.getApplication().createMethodBinding("#{PortletBackBean.edit}", null);

use

fctx.getApplication().createMethodBinding(request.getPreferences().getValue("VIEW_ACTION","#{PortletBackBean.view}"), null);





Dave Brondsema wrote:

>Cool.  Make sure you call setPortletRequestFlag(request); before
>nonFacesRequest.
>
>It might be useful to include part of this on the wiki page as an
>example for anyone else who wants to do that.
>
>Patrick Dalla Bernardina wrote:
>  
>
>>With your help I've made an improvement to the implementation in wiki page.
>>
>>Before calling nonFacesRequest I call an action of an bean called
>>PortletBackBean.
>>If this bean is not declared in managed beans, no method is called.
>>I'm showing only doEdit, but the same can be applied to doView.
>>
>>
>>   public void doEdit(RenderRequest request, RenderResponse
>>response)throws PortletException, IOException {
>>      Boolean isPortletModeChanged =
>>(Boolean)request.getAttribute(ATTR_PORTLET_MODE_CHANGED);
>>
>>      if (isPortletModeChanged.booleanValue()) {
>>          response.setContentType("text/html");
>>
>>           FacesContext fctx = facesContext(request, response);
>>           if(fctx!=null){
>>               MethodBinding mb =
>>fctx.getApplication().createMethodBinding("#{PortletBackBean.edit}", null);
>>               if(mb!=null){
>>                   try{
>>                       Object methodResult = mb.invoke(fctx, null);
>>                   }catch(PropertyNotFoundException e){
>>                       /* if the managed bean is not faound ignore the
>>calling*/
>>                   }
>>               }
>>           }
>>
>>          nonFacesRequest(request, response, defaultEdit);
>>                   return;
>>      }
>>
>>      facesRender(request, response);
>>   }
>>
>>
>>
>>
>>Dave Brondsema wrote:
>>
>>    
>>
>>>Patrick Dalla Bernardina wrote:
>>> 
>>>
>>>      
>>>
>>>>How can I call an managed bean action when portlet mode (EDIT,VIEW) is
>>>>changed and process the navigation rules to discover the page to be
>>>>rendered?
>>>>  
>>>>        
>>>>
>>>You can use http://wiki.apache.org/myfaces/UsingPortletModes as a
>>>starting point.
>>>
>>>To invoke a managed bean action, something like this will work I think:
>>>
>>>ValueBinding binding =
>>>FacesContext.getCurrentInstance().getApplication().createValueBinding("#{myBean.someAction}");
>>>
>>>Object methodResult =
>>>binding.getValue(FacesContext.getCurrentInstance());
>>>
>>>
>>> 
>>>
>>>      
>>>
>
>
>  
>


Re: Portlet Modes

Posted by Dave Brondsema <da...@brondsema.net>.
Cool.  Make sure you call setPortletRequestFlag(request); before
nonFacesRequest.

It might be useful to include part of this on the wiki page as an
example for anyone else who wants to do that.

Patrick Dalla Bernardina wrote:
> With your help I've made an improvement to the implementation in wiki page.
> 
> Before calling nonFacesRequest I call an action of an bean called
> PortletBackBean.
> If this bean is not declared in managed beans, no method is called.
> I'm showing only doEdit, but the same can be applied to doView.
> 
> 
>    public void doEdit(RenderRequest request, RenderResponse
> response)throws PortletException, IOException {
>       Boolean isPortletModeChanged =
> (Boolean)request.getAttribute(ATTR_PORTLET_MODE_CHANGED);
> 
>       if (isPortletModeChanged.booleanValue()) {
>           response.setContentType("text/html");
> 
>            FacesContext fctx = facesContext(request, response);
>            if(fctx!=null){
>                MethodBinding mb =
> fctx.getApplication().createMethodBinding("#{PortletBackBean.edit}", null);
>                if(mb!=null){
>                    try{
>                        Object methodResult = mb.invoke(fctx, null);
>                    }catch(PropertyNotFoundException e){
>                        /* if the managed bean is not faound ignore the
> calling*/
>                    }
>                }
>            }
> 
>           nonFacesRequest(request, response, defaultEdit);
>                    return;
>       }
> 
>       facesRender(request, response);
>    }
> 
> 
> 
> 
> Dave Brondsema wrote:
> 
>> Patrick Dalla Bernardina wrote:
>>  
>>
>>> How can I call an managed bean action when portlet mode (EDIT,VIEW) is
>>> changed and process the navigation rules to discover the page to be
>>> rendered?
>>>   
>>
>>
>> You can use http://wiki.apache.org/myfaces/UsingPortletModes as a
>> starting point.
>>
>> To invoke a managed bean action, something like this will work I think:
>>
>> ValueBinding binding =
>> FacesContext.getCurrentInstance().getApplication().createValueBinding("#{myBean.someAction}");
>>
>> Object methodResult =
>> binding.getValue(FacesContext.getCurrentInstance());
>>
>>
>>  
>>
> 


-- 
Dave Brondsema
Software Developer
Cornerstone University

Re: Portlet Modes

Posted by Patrick Dalla Bernardina <pa...@jfes.trf2.gov.br>.
With your help I've made an improvement to the implementation in wiki page.

Before calling nonFacesRequest I call an action of an bean called 
PortletBackBean.
If this bean is not declared in managed beans, no method is called.
I'm showing only doEdit, but the same can be applied to doView.


    public void doEdit(RenderRequest request, RenderResponse 
response)throws PortletException, IOException {
       Boolean isPortletModeChanged = 
(Boolean)request.getAttribute(ATTR_PORTLET_MODE_CHANGED);

       if (isPortletModeChanged.booleanValue()) {
           response.setContentType("text/html");

            FacesContext fctx = facesContext(request, response);
            if(fctx!=null){
                MethodBinding mb = 
fctx.getApplication().createMethodBinding("#{PortletBackBean.edit}", null);
                if(mb!=null){
                    try{
                        Object methodResult = mb.invoke(fctx, null);
                    }catch(PropertyNotFoundException e){
                        /* if the managed bean is not faound ignore the 
calling*/
                    }
                }
            }

           nonFacesRequest(request, response, defaultEdit);
          
           return;
       }

       facesRender(request, response);
    }




Dave Brondsema wrote:

>Patrick Dalla Bernardina wrote:
>  
>
>>How can I call an managed bean action when portlet mode (EDIT,VIEW) is
>>changed and process the navigation rules to discover the page to be
>>rendered?
>>    
>>
>
>You can use http://wiki.apache.org/myfaces/UsingPortletModes as a
>starting point.
>
>To invoke a managed bean action, something like this will work I think:
>
>ValueBinding binding =
>FacesContext.getCurrentInstance().getApplication().createValueBinding("#{myBean.someAction}");
>Object methodResult = binding.getValue(FacesContext.getCurrentInstance());
>
>
>  
>