user2911374
user2911374

Reputation: 159

f:param does not pass parameter or the parameter does not get set

I use h:link with includeViewParams=true to navigate between my list and view pages and make the view page URLs bookmarkable but it doesn't work. Below is related portion of my code:

in layout.xhtml (my template):

<f:view locale="#{localeBean.lang}">
    <ui:insert name="metadata"/>

in Articles.xhtml (my list page):

<h:form>
    <p:datagrid var="item" value="articleController.items">
        <p:column>
            <h:link outcome="View_Articles?faces-redirect=true&amp;includeViewParams=true" styleClass="view-details">
               <h:outputText value="#{item.title}"/>     
               <f:param name="id" value="#{item.articleId}"/>
            </h:link>
        </p:column>
    </p:datagrid>
</h:form>

in View_Article.xhtml (my view details page):

<ui:composition template="./WEB-INF/templates/layout.xhtml"> 
     <ui:define name="metadata">
         <f:metadata>
             <f:viewParam name="id" value="#{articleController.id}" />
             <f:viewParam name="id" value="#{articleLclController.id}"/>
         </f:metadata>
</ui:define>

Further information: I use JSF 2.2, ocpsoft rewrite filter, primefaces library, and ArticleController.java is session scoped.

I have tried the following:

1- I used h:commandLink; with this, the navigation worked but the URL didn't include view-params

Articles.xhtml:

<h:commandLink id="viewArticleDetails" action="#{articleController.viewArticleByID()}" styleClass="view-details">
    <h:outputText value="View Details"/>
    <f:setPropertyActionListener target="#{articleController.id}" value="#{item.articleId}"/>
    <f:setPropertyActionListener target="#{articleLclController.id}" value="#{item.articleId}"/>
</h:commandLink>

ArticleController.java: ...

private Long id;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
    current = ejbFacade.find(id);
}

public String viewArticleByID(){                      
    return "View_Article?faces-redirect=true&amp;includeViewParams=true";
}

...

I expected the URL to be http://localhost:8080/testApp/en/View_Article?id=1 but it was like `http://localhost:8080/testApp/en/View_Article

2- I inserted http://localhost:8080/testApp/en/View_Article?id=1 and it worked in both cases.

Upvotes: 1

Views: 6329

Answers (1)

Aritz
Aritz

Reputation: 31649

The includeViewParams parameter works only for view parameters which are already in the given view (in your case, Articles.xhtml, where you haven't got any of them). Just pass them as f:param. As you want to perform a plain GET request, your best is to go with h:link:

<h:link outcome="View_Articles" styleClass="view-details">
   <h:outputText value="#{item.title}"/>     
   <f:param name="id" value="#{item.id}" />
</h:link>

That way you'll obtain item's id at your destination page, but is not enough to have the url changed in the address bar. To achieve that add ?faces-redirect=true to your outcome to force a redirection.

See also:

Upvotes: 1

Related Questions