简体   繁体   中英

open a menuitem page in a new tab in primefaces

I have a xhtml page with a session scoped backing bean. Now i have to open this page in a new tab with right click open link in new tab. When this page opens the model class of the backing bean must be cleared. I have used the following code:

<p:menuitem value="Details" action="#{beanMB.clearDetailModel()}"/>

backing bean code:

public void clearDetailModel()
    {
        memberModel=null;
        ......
        return "/pages/member/MemberDetails.xhtml?faces-redirect=true";
   }

The above code clears the session scoped model but it does not open the page in new tab.

Is there any method to open the page in new tab with the above code or is there any alternate way for the above problem?.Any help would be appreciated. Thank you.

You can try this:

You need to put the following code in MemberDetails.xhtml page.

<f:metadata>
<f:event type="preRenderView" listener="#{beanMB.clearDetailModel}"/>
</f:metadata>

and use url in menuitem to open your page.

<p:menuitem value="Details"  url="/pages/member/MemberDetails.xhtml" />

you need to modify your backing bean code as follows:

public void clearDetailModel()
    {
        if (isNewRequest()){
        memberModel=null;
        ......
        }

   }

public boolean isNewRequest() {
        final FacesContext fc = FacesContext.getCurrentInstance();
        final boolean getMethod = ((HttpServletRequest) fc.getExternalContext().getRequest()).getMethod().equals("GET");
        final boolean ajaxRequest = fc.getPartialViewContext().isAjaxRequest();
        final boolean validationFailed = fc.isValidationFailed();
        return getMethod && !ajaxRequest && !validationFailed;
    }

Here method isNewRequest() checks if the request is new or not if this is not checked your method clearDetailModel() will be called each time new request is made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM