Question

I have the following structure:

listView.xhtml

<h:dataTable value="#{listBean.myList} ...>
  //for every row I create a commandLink
  <h:commandLink action="editView" value="edit" />
</h:dataTable>

ListBean.java

@ManagedBean
@ViewScoped
public class ListBean{
   public List<Entity> myList; // also getters and setters
}

editView.xhtml

<h:inputText value="#{editBean.selectedEntity.name}" />

EditBean.java

@ManagedBean
@ViewScoped
public class EditBean{
   public Entity selectedEntity; // also getters and setters
}

You know the question: How can I transport the selected Entity from listView to editView? This should be very simple I thought, but after a whole day, I didnt get it work.

I tried different stuff, like @ManagedProperty and <f:param name="" value=""> but I didnt help me. So please, show me how simple and nice this can be :)

Thanks in advance!


UPDATE - Solution#1

Thanks to Daniel, a possible way that works is, when the entity is hold by an EntityManager, so you can access the entity by its id. So you will pass the id as an request Parameter. Here we go:

listView.xhtml

<h:dataTable value="#{listBean.myList} ...>
  //for every row I create a commandLink, so you can click on that entity to edit it
  <h:commandLink action="editView" value="edit">
     <f:param name="selectedEntityId" value="#{entity.id}" />
  </h:commandLink>
</h:dataTable>

EditBean.java

@ManagedBean
@ViewScoped
public class EditBean{

    private Entity selectedEntity;

    @PostConstruct
    public void init() {
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        long selectedEntityId = Long.parseLong(params.get("selectedEntityId"));

        selectedEntity = SomeEntityManagerUtil.getEntity(selectedEntityId);
    }
}
Was it helpful?

Solution

The general idea could be :

To pass an id of that entity and get an entity by that id later on...

You also could use a converter and inside it translate that id into entity...

like this :

<h:inputText value="#{editBean.selectedEntity.name}" converter="myEntityConverter"/>

OTHER TIPS

Perhaps you should merge your beans if they have the same scope ? You may also use the context: jsf-get-managed-bean-by-name

Also look at this question: passing-data-between-managed-components-in-jsf

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top