【问题标题】:JSF2: Handling RMI Connection from WebListener to ManagedBeansJSF2:处理从 WebListener 到 ManagedBeans 的 RMI 连接
【发布时间】:2011-01-09 12:03:48
【问题描述】:

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.
} 

【问题讨论】:

    标签: java jsf jsf-2


    【解决方案1】:

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

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

    【讨论】:

    • 我可以在 WebListners 中使用 ManagedProperties 吗?
    • 不,绝对不是。请注意,我更新了答案,因为我误解了最初的问题,因为今天早上没有咖啡;)
    • 没问题:-D。但与此同时,我尝试添加您的其他建议,包括 postConstruct 并且它有效:-) 谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-04-13
    • 2011-09-04
    • 2012-01-18
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多