JSF2:处理从WebListener到ManagedBeans的RMI连接

时间:2011-01-09 12:03:48

标签: java jsf jsf-2

Initial Problem

您好,

我使用@WebListener类在应用程序部署时启动RMI连接。这将我的JSF前端与后端连接起来。

工作正常!

接下来我想将连接交给ManagedBean,因为我想使用连接到例如从bean中保存一些东西,因为无法从xhtml页面访问weblistener。

我尝试将managedProperty放入该类,但我认为这是不允许的。那怎么办?

@WebListener
public class Config implements ServletContextListener {

public static final String SERVER_NAMING = "xxx";
public static final String SERVER_HOST = "xxx"; 

public static FrontendCommInterface server;


public void contextInitialized(ServletContextEvent event) {
    try {

        server = (FrontendCommInterface) Naming.lookup("rmi://" + SERVER_HOST + "/" + SERVER_NAMING); 
            System.out.println("Connection successfull!");
//HERE THE SERVER SHOULD HANDED TO ANOTHER MANAGEDBEAN !!! BUT HOW TO DO THAT??? 

        } catch (MalformedURLException e) {
        System.out.print("Error: " + e.getLocalizedMessage());
    } catch (RemoteException e) {
        System.out.print("Error: " + e.getLocalizedMessage());
    } catch (NotBoundException e) {
        System.out.print("Error: " + e.getLocalizedMessage());
    }
}

public void contextDestroyed(ServletContextEvent event) {
    // Do stuff during webapp's shutdown.
} 

1 个答案:

答案 0 :(得分:2)

您需要自己创建bean并放入应用程序范围。

event.getServletContext().setAttribute("communication", new Communication(server));