JSF - Redirect to page and remember parameter ID for searching

11,379

Solution 1

Just pass it as well.

E.g.

<h:commandLink value="#{item.topic}" action="#{myTools.navigate('posts', item.id)}"/>

with

public String navigate(String menuItem, Long id) {
    this.menuItem = menuItem;
    return menuItem + "?faces-redirect=true&id=" + id;
}

The bean doesn't and shouldn't need to be in session scope. The view scope is fine. Otherwise the enduser will face unintuitive behaviour when interacting with the same page in multiple browser tabs/windows.

Solution 2

You can use :

<f:setPropertyActionListener target="#{propertyToSet}" value="#{item.id}" />

inside your commandLink.

Solution 3

You can add a hidden field, store the ID in that field before submitting (use onclick in Javascript) and bind this hidden field to a variable inside the Bean.

<h:inputHidden id="selectedId" value="#{beakbean.selectedId}">

<h:commandLink value="#{item.topic}" onclick="updateSelectedId()" action="#{myTools.setMenuItem('posts')}"/>

function updateSelectedId(){
    //put the selected id in the field selectedId
}
Share:
11,379
gaffcz
Author by

gaffcz

JavaEEmbryo:-)

Updated on June 14, 2022

Comments

  • gaffcz
    gaffcz almost 2 years

    I have two xhtml pages and two managed beans.

    On first page I have a list of topics (records from database table - second column contains <h:commandLink> tags):

    enter image description here

    Part of reduced code:

    <rich:column><h:outputText value="#{item.id}"/></rich:column>
    <rich:column><h:outputText value="#{item.createdBy}"/></rich:column>
    <rich:column>
      <h:commandLink value="#{item.topic}" action="#{myTools.setMenuItem('posts')}"/>
    </rich:column>
    

    I'm using action="#{myTools.setMenuItem('posts')}" to redirect to page posts.xhtml. How can I pass the parameter "#{item.id}" to be able to find all posts to topic with given ID?

    UPDATE (using DataModel): This could be the way:

    <h:commandLink value="#{item.topic}" action="#{myTopic.submit}">
    
    public String submit()
    {
      topic = model.getRowData();
      return "/posts.xhtml?faces-redirect=true&id=" + topic.getId();
    }
    

    But I still don´t know how to pass the topic.getId() parameter to another bean (MyPosts)..?