0

My site has a page with a datatable. This datatable has a button called 'Edit', where the user can click to edit the data of a single row. When the use clicks on it, he gets redirected to another page, where the data of the selected item will be displayed for him to edit.

The way I make it right now is with a SessionScoped bean, where I can keep the value for the selectedItem variable. However, it feels gross to use SessionScoped here.

Simplified Code:

<h:form>
    <p:dataTable id="dataTable" var="item" value="#{exampleController.itens}">
         <p:column headerText="Name">
             <h:outputText value="#{item.name}" />
         </p:column>
         <p:column>  
             <p:commandLink title="Edit" action="/pages/anotherPage?faces-redirect=true">
                  <f:setPropertyActionListener value="#{item}" target="#exampleController.selectedItem}" />
             </p:commandLink>
         </p:column>  
     </p:dataTable> 
</h:form>

What is a better way of doing this?

Thank you.

Diogo Garcia
  • 27
  • 1
  • 9

1 Answers1

0

You should try view params, that way you win two things:

To do this you need in your action:

<p:commandLink title="Edit" action="/pages/anotherPage?faces-redirect=true">
    <f:param name="foo" value="#{item}"/>
</p:commandLink>

And in your edit view:

<f:metadata>
  <f:viewParam name="foo" value="#{viewBean.item}"/>
</f:metadata>

More info with the great Balus Balus - Communication in JSF

rekiem87
  • 1,565
  • 18
  • 33