Inject request parameters into view scoped bean (different portlets)

340 views Asked by At

I'm attempting to communicate two views, the first view within portlet-A, and the second view within portlet-B (both portlets within same .war). To do so, I've decided to use the JSF 'f:viewParam' and 'f:param' features in order to inject a property (from portlet-A view) into the request object, so that the portlet-B view can retrieve it from the request object and pass such property value to a view scoped backing bean property.

Portlet-A view code:

<p:dataScroller value="#{searchManager.List}" var="ccp" >
    ...
    <p:link value="#{ccp.title}" onclick="myonClick(event)" >
         <f:param name="id" value="#{ccp.id}" />             
    </p:link>
    ...
</p:dataScroller>

JS code:

function myonClick(event) {
    event.preventDefault();
    window.open("viewer", "_blank");
}

Note that portlet-B view have to be displayed on a Liferay-based page, different to the one where the portlet-A view is displayed.

Portlet-B view code:

<f:metadata>
    <f:viewParam name="id" value="#{resultItemManager.id}" />       
</f:metadata>

<h:head />

<h:body>
    <p>Details:</p>
        <h:outputText value="#{resultItemManager.id}" />
</h:body>

When portlet-B gets displayed, the browser address field is set to 'http://host:8080/viewer' and the tag gets rendered as '0' (zero).

I don't know if the way i'm doing the targeted task is the right one or not. But if it is, i don't know why it isn't working. So I'd really appreciate any help/comments. Thanks in advance.

1

There are 1 answers

0
txapeldot On

Portlet-A view code:

<p:dataScroller value="#{searchManager.List}" var="ccp" >
...
    <p:link value="#{ccp.title}" onclick="myonClick(event, #{ccp.id})" />
...
</p:dataScroller>

JS code:

function myonClick(event, itemId) {
    event.preventDefault();
    window.open("viewer" + "?id=" + itemId, "_blank");
}

Portlet-B view code (Portlet-B view have to be displayed on a Liferay-based page, different to the one where the portlet-A view is displayed):

<h:body>
    <p>Details</p>
    <h:outputText value="#{itemManager.id}" />              
</h:body>

Portlet-B side managed bean:

@ManagedBean(name = "resultItemManager")
@ViewScoped
public class ItemManager implements Serializable {

  @ManagedProperty(value = "")
  public long id;

  ...

  @PostConstruct
  public void init() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext extContext = facesContext.getExternalContext();
    RenderRequest renderRequest = (RenderRequest) extContext.getRequest();
    HttpServletRequest httpRequest = PortalUtil.getOriginalServletRequest(
         PortalUtil.getHttpServletRequest(renderRequest));
    this.id = Long.parseLong(httpRequest.getParameter("id"));
  }
}

This way, there is no need to use ViewParams. El 'itemId' param is appended to the http request so that it is retrieved in the init() PostConstruct method on the Portlet-B side. Moreover, the view is displayed on a new different page.