How to pass objects from one page to another page in JSF without writing a converter
Asked Answered
G

1

5

first of all sorry for my english. I have two pages in JSF2, one to list Passengers and another one to create/update passengers. I have also two @ViewScoped beans, one with the list of passengers and one for hold in pageB the selected passenger. I see the ways to pass the passenger through viewParam or @ManagedProperty but i don´t want to write a converter.

What i want to know if there is a way to pass the object from pageA to pageB without passing the id of the passenger and write a converter or without passing the id and then go to the DB to retrieve the passenger. What i do and works is the following. I set in flash scope through setPropertyActionListener the selected object and navigate to pageB, in the @PostConstruct of the viewScopedBean i get the flashScope and retrieve the object. As i said, this works but i don´t know if it is correct. Here is the code Page A:

<p:column width="10" style="text-align: center;">                    
    <p:commandButton icon="ui-icon-pencil" action="editClientes?faces-redirect=true">                         
        <f:setPropertyActionListener target="#{flash.pax}" value="#{row}"/> 
     </p:commandButton>
</p:column>

@PostConstruct of pageB bean

@PostConstruct
private void initBean(){          
    this.pax = (Passenger) JSFUtils.getFlashScope().get("pax"); 
    if(this.pax == null){
        this.pax = new Passenger();
    }           
}

Is this correct, or the correct way is to write a converter? Thanks.

Grissom answered 24/8, 2014 at 1:48 Comment(0)
U
8

Depends on whether you want the /editClientes request to be idempotent ("bookmarkable") or not.

The flash approach is not idempotent. It's not possible to link/share/bookmark the /editClientes URL in order to edit a specific client. When the enduser copies this URL for sharing/bookmarking and re-executes the request on it (even though it's just pressing [enter] in browser's address bar), all the enduser would face is an empty edit form for a new client instead of the one the enduser initially selected via flash scope.

The request parameter approach is idempotent. The enduser is able to get exactly the same response everytime the enduser re-executes the request.

It's not our decision whether your /editClientes page should be idempotent or not. It's yours.

See also:

Utu answered 24/8, 2014 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.