简体   繁体   English

如何在Session侦听器或Servlet中的JSF中设置Backing Bean中的值?

[英]How to set values in Backing Bean in JSF from Session listener or Servlet?

SessionListener我想设置@ManagedBean一些值,其中包含我想在JSF中显示的Session scope

You'd need to create the managed bean instance yourself. 您需要自己创建托管bean实例。

Bean bean = new Bean();
bean.setSomething(something);
event.getSession().setAttribute("bean", bean); // "bean" is managed bean name.

JSF will just reuse it if it already exist in the session scope (you see, the JSF "session scope" is under the covers represented by attributes of HttpSession ). 如果JSF已经存在于会话范围内,那么JSF将重用它(你看,JSF“会话范围”在HttpSession的属性所代表的覆盖范围内)。 Note that this way any @PostConstruct won't be invoked and any dependencies which needs to be injected by @ManagedProperty , @EJB , etc, won't be injected at all. 请注意,这样任何@PostConstruct都不会被调用,并且任何需要由@ @ManagedProperty ,@ @EJB等注入的依赖项都不会被注入。 You'd need to do it yourself as well. 你也需要自己做。

Designtechnically, much better is to perform the job just in the constructor or @PostConstruct method of the backing bean class itself. 从技术上讲,更好的方法是在构造函数或支持bean类本身的@PostConstruct方法中执行作业。

@ManagedBean
@SessionScoped
public class Bean {

    @PostConstruct
    public void init() {
        // Here.
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM