这个问题已经有一个答案在这里:

大学项目,我发展web与JSF.我的练习就是要做的前端。同studend是应该做的后端的东西。这两个部分都设计成可seerate应用程序。两者都通过RMI。我想要打开连接,一旦在部署。

我在点解决了连接。我试图做到这一@ApplicationScoped ManagedBean:

//Constructor of ApplicationScoped ManagedBean  
public Communication() {
    this.connect();
}

是那种可能吗?我尝试过了但managedBean似乎不是叫..

你可以建议一个最好的做法吗?


@布莱恩:不幸的是,我不用EJB在全.-

@BalusC的锅:我创建了一个communicationbean:

@ManagedBean(name="communication")
@ApplicationScoped
public class Communication {

    public static FrontendCommInterface server;

    public Communication() {
        this.connect();
    }

然后我创造了LoginBean:

@ManagedBean
@ViewScoped
public class Login {

@ManagedProperty(value="#{communication}")
private Communication communicationBean;

public FrontendCommInterface server;

private String username;
private String password;

public Login() {
    server = communicationBean.getConnection();
}

public String login(){
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    String sessionId = session.getId();

    try {
        server.login(getUsername(), getPassword(), sessionId);
        return "start.xhtml";

    } catch (RemoteException e) {
        e.printStackTrace();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Anmeldung nicht erfolgreich: ", getUsername()+", "+getPassword()+", "+sessionId));
        return "login.xhtml";
    }
}

但不幸的是,它将例外规定:

com.sun.faces.mgbean.ManagedBeanCreationException: Klasse org.dhbw.stg.wwi2008c.mopro.ui.managedBeans.Login can not be instanciated.

java.lang.NullPointerException
    org.dhbw.stg.wwi2008c.mopro.ui.managedBeans.Login.<init>(Login.java:28)

后调试我发现我的ManagedProperty是Null!它没有创建了!如何做到这一点?我想引用经managedproperty会创建了-.-

有帮助吗?

解决方案

管理豆只是自创建时,它已经引用过EL #{managedBeanName}, ,这可能会发生通过访问是在看来,或者通过被注入作为管理财产的另一个豆,或者是手动EL-解决,例如通过 Application#evaluateExpressionGet().

在特定情况下,你实际上想要intialize一些东西过网络应用程序的启动。你不想要使用 ServletContextListener 这一点。

@WebListener
public class Config implements ServletContextListener {

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

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

}

你甚至可以预创造一个应用程序范围内的管理豆有必要时(如果你的意图是能够访问它从其他豆 @ManagedProperty).

    public void contextInitialized(ServletContextEvent event) {
        event.getServletContext().setAttribute("bean", new Bean());
    }

JSF储存应用程序范围内豆作为一个属性 ServletContext 和JSF不会自动建立的另一个之一时,一个是已经存在,所以同一个作为创建上述码的例子将使用由JSF。

其他提示

如果你可以使用EJB3.1精简{1}在你的网页应用程序,然后可以使用一个单一届会议豆、附加说明的与@启动,并@PostConstruct方法。我有一个看起来像:

@Singleton
@Startup
public class CachePrimer {

    @PostConstruct
    public void loadOpenRequests() {
         ...
    }
}

{1}:EJB3.1精简为包括在网配置文件的JavEE6,并提供网络配置的服务器喜欢 我们, Boss6, , 树脂.在使用这种网络配置的服务器,只需包括你总是在你的.战争的文件,没有额外的工作是必需的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top