首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSF接收FacesContext外部请求

JSF接收FacesContext外部请求
EN

Stack Overflow用户
提问于 2012-07-23 21:10:22
回答 1查看 1K关注 0票数 0

据我所知,FacesContext仅在请求范围内可用。我已经创建了一个线程,它试图接收FacesContext的实例,但它返回null。

我的观点是每10秒更新一次应用程序范围的bean。

线程的run方法:

代码语言:javascript
复制
@Override
public void run()
{
    while (true)
    {
        try
        {
            TimeView timeView = (TimeView)FacesContext.getCurrentInstance().
                    getExternalContext().getApplicationMap().get("timeView"); 
            // FacesContext.getCurrentInstalce() returns null

            timeView.update();
            Thread.sleep(10000);
        }
        catch (InterruptedException ex)
        {
            System.out.println(ex);
        }
    }
}

TimeView的标题(我跳过了getters/setters):

代码语言:javascript
复制
@ManagedBean(eager=true, name="timeView")
@ApplicationScoped
public class TimeView implements Serializable
{
    private int hour;
    private int minutes;
    private int seconds;

    public TimeView()
    {
        update();
    }

    public void update()
    {
        Date date = new Date();
        setHour(date.getHours());
        setMinutes(date.getMinutes());
        setSeconds(date.getSeconds());
    }

faces-config.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">

    <managed-bean>
        <managed-bean-name>timeView</managed-bean-name>
        <managed-bean-class>foogame.viewBeans.TimeView</managed-bean-class>
        <managed-bean-scope>application</managed-bean-scope>
    </managed-bean>
</faces-config>

那么,有没有办法在这个线程中接收对我的应用程序范围的bean的引用呢?

EN

回答 1

Stack Overflow用户

发布于 2012-07-24 04:23:52

由于现在有办法在Servlet环境之外访问/构造FacesContext,因此我建议您将应用程序范围的对象传递给工作线程(执行批处理作业的线程)的构造函数。更新线程中的引用将导致更新应用程序范围的引用,因为它们都指向同一个实例。

如果你有一个EJB3环境,你可以使用EJB timer + @Singleton bean,而不需要处理线程和作用域。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11613218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档