How to pass object to another view in JSF?

Asked

Viewed 6,415 times

2

What better way to pass an object to another view in JSF?

I have two pages a query and another with the data to edit a particular object, the query page has a list of objects and each object has the edit button that causes it to redirect to the editing page, I can’t send the object and click on the editing page. My Bean I’m using Viewscoped.

In other languages it is possible to go through POST but in java it is kind of complicated I am not able to adapt. If possible place a code snippet.

  • I see two simple alternatives: As a Request attribute, if using forward or as an attribute in Flash, if redirect is used.

  • Ever tried to use a f:setPropertyActionListener? Based on a CRUD system, I believe it would work very well. The button would redirect, and within the tags of the button you f:setPropertyActionListener with the data template to be sent, the code would be more or less this: <f:setPropertyActionListener target="#{bean.objeto}" value="#{objeto}">.

  • For the best user experience and for simplicity of design, I recommend: each button should be a link to the URL of the editing page, passing the ID of the object as parameter. On the edit page you reload the object from this ID received by parameter. Use Sessionscoped only to keep data that is naturally from the session as user credentials and your preferences.

2 answers

1


The scope of View (View Scope) is discarded whenever a navigation occurs from one screen to another.

An alternative is to store the object in the Session scope (Session Scope).

It is also possible to pass the ID of the object a URL parameter or in the Flash Scope and then load it again on the destination page.

  • Ok, but if I leave Sessionscoped when the user browse another page and return to the registration page the data will be there, could exemplify with codes?

  • @Juareza.Francojunior The data will be there if you do nothing but you can clear or reset the data in the method that redirects to the screen. Unfortunately I have no time to elaborate a more detailed answer with codes, because I would have to resurrect some projects that I haven’t touched for some time.

  • @utluiz, I tried to implement with the answer code below and it did not work. You can elaborate a more concrete answer?

-1

Setting the object in Flash Scope

String str = "teste";

FacesContext.getCurrentInstance().getExternalContext().getFlash().put("teste", str);

In the other Bean create a method and use annotation @PostConstruct.

@PostConstruct
public void init() {
     String strAux = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("teste");
}

It is possible to pass a List.

FacesContext.getCurrentInstance().getExternalContext().getFlash().put(LISTA_STRING, listaString);

@PostConstruct
public void init() {
     List<String> listaString = (List<String>) FacesContext.getCurrentInstance().getExternalContext().getFlash().get(LISTA_STRING);
}
  • Friend, I tried to implement this way using init in the other bean. More it’s coming Null. You know why?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.