JSF2-Behaviour of viewParam on viewscoped bean aft

2019-08-27 17:59发布

问题:

I have a viewscoped bean wich receives parameters through f:viewparam and set values in the model using them. After a page postback, the parameters dissapear from the url but the model values setted by the initial params are retained somehow.

Is this a safe way of keeping parameters or should I rewrite the url on postback so it keeps the initial parameters subsequent calls?

How are the parameter retained? In the viewstate?

回答1:

Here's what the f:viewParam documentation says:

[...] this tag causes a UIViewParameter to be attached as metadata for the current view. Because UIViewParameter extends UIInput all of the attributes and nested child content for any UIInput tags are valid on this tag as well.

This means that the <f:viewParam> value attribute will be updated with the request parameter, stored in the viewstate and re-set/revalidated when you submit the page again.

Arjan Tijms has the full story here: http://arjan-tijms.omnifaces.org/2011/07/stateless-vs-stateful-jsf-view.html

In short: using a @ViewScoped bean is perfectly fine, but if you use an expensive validator/converter on that value be aware that it will be called again on each postback from that page. If you want to avoid that, have a look at this article from BalusC, which explains o:viewParam, OmniFaces' stateless alternative.



标签: java http jsf-2