JSF 1.2: How to keep request scoped managed bean alive across postbacks on same view?

…衆ロ難τιáo~ 提交于 2019-11-26 11:22:06

问题


Is it possible to keep a request scoped bean alive across postbacks on the same page?

The general problem is, as the bean gets trashed on end of request and recreated on every form submit, for example the booleans behind dynamically manipulated disabled, readonly and rendered get reset to their default values and cause the forms to not work as intented anymore.


回答1:


I'll assume that the session scope is not an option, otherwise this question makes little sense.

You can do it using Tomahawk <t:saveState>. Add the following line somewhere to the page:

<t:saveState value="#{bean}" />

RichFaces <a4j:keepAlive> does also the same:

<a4j:keepAlive beanName="#{bean}" />

Or if there is room, upgrade to at least JSF 2.x and put the bean in view scope:

@ManagedBean
@ViewScoped
public class Bean implements Serializable {
    // ...
}

Regardless of the way, the same bean will be there when you postback to the same view and keep returning null or void from action methods.

See also:

  • How to choose the right bean scope?
  • Difference between View and Request scope in managed beans



回答2:


Not really, unless you store the Bean somewhere e.g. a Map in application scope, to retrieve it later.

Why not just make it Session scoped? This is what Session scope is there for, so multiple Requests during the same Session can hit the same state.



来源:https://stackoverflow.com/questions/5897262/jsf-1-2-how-to-keep-request-scoped-managed-bean-alive-across-postbacks-on-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!