No completar la solicitud de bean con ámbito

I have a request scoped bean which is filled with various properties from a form. These properties are then used to update another view scoped bean. Now I want to give the user the possibility to the reset the form in such a way that all form fields are holding the values they had when the page was loaded the first time. These values are defined through the bean itself:

public class ItemSearchBean {

    private Rarity minRarity = Rarity.None;
    private Rarity maxRarity = Rarity.None;



Notice though that the form submiting button actually invokes a ajax request, therefor no full page reload goes on.

The submitting button:

<p:commandButton value="Search"

I already tried to use a simple reset button, but it only reseted the form to the last submitted values:

<p:commandButton type="reset" value="Reset"/>

One has to somehow ask the server for a fresh new bean (or prevent it to fill the bean), but I have no clue how to do this.

preguntado el 31 de julio de 12 a las 14:07

2 Respuestas

You should be able to do that with a plain HTML link to the same view:

<a href="yourpage.xhtml">Reset</a>

or let JSF create the link for you:

<h:link value="Reset" />

This way you'll have a new GET request that will create a new UIViewRoot, just like if you were accessing the same view in a new browser tab.

If you want a button instead, you can use an h:button

<h:button value="Reset" />

This button will rely on a Javascript that will reload the page on click.

Respondido 31 Jul 12, 14:07

Thank you, this works as intended, but is there a also a way to do this on ajax basis (hopefully not by setting each property by hand) to omit a complete page reload? - Sebastián Hoffmann

You're welcome. No, not that I am aware of. But really, reloading the page is not bad, the HTTP flow won't get any simpler than that and your code is kept to a minimum. :) I don't think AJAX for this kind of thing would bring much gain. - Elias Dorneles

you could also do a two phase setup. a little more work, but allows all sorts of Undo workflows if you take it further.

basically, have two beans that encapsulate your form with the usual accessor/mutator pairs. then in your controller, expose only one (either x, or y) and that is your "form bean". in y, you store the pristine copy that is retrieved from the repository, that way you can do field level or bean level reversion without having to reload the page or do a full data remarshall from the repo.

so, simply:

  • 2 beans to encapsulate the form (you might even be able to re-use your entity if it maps onto the form).
  • fill both from the repo during data marshall
  • expose one to the user in the facelets
  • keep the second as a backup for resetting/reverting either fields or the entire form

there are other ways to skin the "undo" cat, and this is only another.

Respondido 31 Jul 12, 18:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.