2

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)..?

gaffcz
  • 3,469
  • 14
  • 68
  • 108

3 Answers3

4

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you BalusC, I've tried something similar, but when I redirect using `menuItem + "?faces-redirect=true&id=" + id;`, i don't know how to use parameter `id` in the bean MyPosts..?? – gaffcz Oct 26 '11 at 13:04
  • Use `` in the target view `posts.xhtml` or `@ManagedProperty` in the target bean `MyPosts`. See also http://stackoverflow.com/questions/4888942/viewparam-vs-managedpropertyvalue-param-id – BalusC Oct 26 '11 at 13:07
2

You can use :

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

inside your commandLink.

kgautron
  • 7,915
  • 9
  • 39
  • 60
1

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
}
mohdajami
  • 9,604
  • 3
  • 32
  • 53