服务器启动的渲染:EJB-& gt; FacesContext? [英] Server-initiated Rendering: EJB -> FacesContext?

查看:74
本文介绍了服务器启动的渲染:EJB-& gt; FacesContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Icefaces论坛上提出了这个问题,但是与此同时,我意识到这是一个更通用的问题.

I've already asked this question on the Icefaces forum, but meanwhile I realized that this is a more generic problem.

当我在MDB中收到消息时,我想更新JSF页面的某些部分.

I'd like to update parts of a JSF page when I get a message in my MDB.

问题是,如何从EJB容器中获取FacesContext?

The problem is, how do I get the FacesContext from the EJB container?

在消息处理函数FacesContext.getCurrentInstance()中返回null.

In the message processing function FacesContext.getCurrentInstance() returns null.

我也试图将JSF管理的bean变成MDB,但是我做不到(看来你们不能在同一个类中同时拥有这两个?).

I've also tried to make a JSF managed bean be a MDB, but I couldn't (it seems you can't have both in the same class?).

由于我是JSF领域的初学者,所以现在有点卡住了.有办法使它起作用吗?

Since I'm a beginner in the JSF world I'm kind of stuck now. Is there a way to make it work?

(Glassfish v3 + Netbeans 6.8,JSF2 + Icefaces 2.0 alpha2)

(Glassfish v3 + Netbeans 6.8, JSF2 + Icefaces 2.0 alpha2)

推荐答案

FacesContext基于HTTP请求,因此仅在HTTP请求处理期间可用,甚至仅在请求URL与FacesServlet的网址模式匹配时才可用.如果您不在服务器执行的处理HTTP请求的线程中,那么也就没有FacesContext的意思.在EJB容器中,完全没有HTTP请求的方式.

The FacesContext is HTTP request based and thus only available during the HTTP request processing and even then only when the request URL matches the url pattern of the FacesServlet. If you're not inside the thread which is executed by the server to process the HTTP request, then there's also no means of a FacesContext. In an EJB container there's totally no means of HTTP requests.

从技术上讲,让EJB通知JSF有关新消息的唯一方法是让EJB在与FacesServlet的url模式匹配的URL上以消息作为请求参数触发HTTP请求.您可以为此使用java.net.URLConnection.然后,JSF可以像执行Comet/HTTP push这样的操作来使用您提到的IceFaces方式消息来更新视图.

Technically, the only way to let EJB inform JSF about a new message is to let EJB fire a HTTP request on an URL matching the url pattern of the FacesServlet with the message as request parameter. You can use java.net.URLConnection for this. JSF in turn can then do the Comet/HTTP push like stuff to update the view with the message the IceFaces way as you mentioned.

例如

URL url = new URL("http://example.com/context/poll.jsf?msg=" + URLEncoder(msg, "UTF-8"));
URLConenction connection = url.openConnection();
InputStream response = connection.getInputStream();

poll.jsf附加到支持Bean上,如下所示:

and a poll.jsf which is attached to a backing bean like this:

@ManagedBean
public class Poll {

    @ManagedProperty(value="#{param.msg}")
    private String msg;

    @PostConstruct
    public void init() {
        // Do something with msg.
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

注意:使用JSF 2.0批注,但是它们应该足够容易解释.

这篇关于服务器启动的渲染:EJB-& gt; FacesContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆