0

I am build a CRUD web application using JSF and have made a add form for creating a new entity which gets persisted to the database. On a succeful save I would like the user to get navigated to a page where the details of the newly created entity will be displayed.

More in detail what I wan't to do is to create a navigation rule where on a succeful save the user gets navigated to "dilutionDetail.xhtml". Which I do using "faces-navigation.xml". And this part works fine.

My problem is that I need to pass a parameter that allows me to fetch the id of the newly created entity. Bellow is the commandButton in the add form.

<p:commandButton value="Save" action="#{dilution.save()}">
    <f:actionListener binding="#{dilution.prepareForSave()}" />
</p:commandButton>

The entity gets persisted when dilution.save() is called (which also returns 'saved' on success to be used in the navigation case). dilution is a view scoped managed bean.

Save looks like this:

public String save(){
    das.create(dilution);

    return "saved";
}

After das.create(dilution) has been called the dilution entity will have the id that I want to pass as a parameter. How can I do this?

Ashot Karakhanyan
  • 2,734
  • 3
  • 21
  • 28
numfar
  • 1,397
  • 3
  • 15
  • 34

1 Answers1

1

You need to back your "dilutionDetail.xhtml" with its own @ViewScoped managed bean. Make it receive a view parameter, which will be the id of dilution you want to load:

<f:metadata>
    <f:viewParam name="id" value="#{dilutionDetailBean.id}" />
    <f:event type="preRenderView" listener="#{dilutionDetailBean.loadDilution}"/>
</f:metadata>

With this code you'll get your desired id set and later on, you could load it from DB with loadDilution method.

Now, how to navigate to it when saving? Just use implicit navigation.

Integer id = das.create(dilution);
return "dilutionDetail?id="+id;

See also:

Community
  • 1
  • 1
Xtreme Biker
  • 28,480
  • 12
  • 120
  • 195