1

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);
    }
}
artgrohe
  • 3,082
  • 2
  • 25
  • 31
  • ups, I corrected the mistake in the post. But the problem is that RequestParameterMap.get("paramName") only returns strings and not the Entity object. – artgrohe Nov 20 '12 at 11:52
  • than pass an `id` and get an *entity* by that `id` , or use a `converter` and inside it translate `id` into entity... – Daniel Nov 20 '12 at 12:02
  • This works! Thanks. I will update my question to provide the solution. But nevertheless, I thought there is a simpler solution ;) – artgrohe Nov 20 '12 at 12:13

2 Answers2

0

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"/>
Daniel
  • 36,833
  • 10
  • 119
  • 200
0

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

Community
  • 1
  • 1
Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
  • I think if navigate from listView to editView, the views have changed, and therefore the @ViewScoped Beans are cleared(is this correct?). Isn't there a way to send Beans through via RequestScope? Because this is just a request...or not? – artgrohe Nov 20 '12 at 12:04