JSF2:如何在部署时启动服务 [英] JSF2: How to initiate services at deployment
问题描述
对于大学项目,我正在使用JSF开发一个Web应用程序.我的工作是做前端.一个同伴应该做后端的东西.这两部分均设计为实用的应用程序.两者都通过RMI进行通信.我想在部署时打开一次连接.
For university project I am developing a webapplication with JSF. My excercise is to do the frontend. A fellow studend is supposed to do backend stuff. Both parts are designed to be seerate applications. Both communicate through RMI. I want to open the connection once at deployment.
我现在是要建立连接的时候了.我尝试使用@ApplicationScoped ManagedBean做到这一点:
I am at the point to settle up the connection now. I tried to do that with a @ApplicationScoped ManagedBean:
//Constructor of ApplicationScoped ManagedBean
public Communication() {
this.connect();
}
有可能吗?我试过了,但是似乎没有调用managedBean.
Is that way possible? I tried it but the managedBean seems not to be called..
您能提出最佳实践建议吗?
Can you advice a Best Practice?
@Brian:不幸的是,我根本不使用EJB -.-
@Brian: Unfortunately I don't use EJB at all -.-
@BalusC的底池: 我创建了一个communicationbean:
@BalusC's pot: I created a communicationbean:
@ManagedBean(name="communication")
@ApplicationScoped
public class Communication {
public static FrontendCommInterface server;
public Communication() {
this.connect();
}
然后我创建了LoginBean:
Then I created the 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";
}
}
但不幸的是,它引发了异常:
But unfortunately it throws exceptions:
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引用会创建它-.-
After debuging I found out that my ManagedProperty is Null ! It hasn't been created! How to do that? I thought referencing via managedproperty would create it -.-
推荐答案
仅当EL #{managedBeanName}
引用托管bean时才自动创建托管bean,这可以通过按原样访问view或通过作为另一个bean的托管属性注入,或由例如EL手动解析Application#evaluateExpressionGet()
.
The managed bean is only auto-created whenever it's been referenced by EL #{managedBeanName}
, which can happen by either accessing as-is in view, or by being injected as managed property of another bean, or being manually EL-resolved by e.g. Application#evaluateExpressionGet()
.
在您的特定情况下,您实际上想在webapp启动期间初始化一些内容.您宁愿为此使用 ServletContextListener
.
In your particular case, you actually want to intialize some stuff during webapp's startup. You rather want to use ServletContextListener
for this.
@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.
}
}
您甚至可以在必要时在此预先创建一个应用程序范围的托管Bean(如果您的意图是能够通过@ManagedProperty
从其他Bean访问它).
You could even pre-create an application scoped managed bean there whenever necessary (if your intent is to be able to access it from other beans by @ManagedProperty
).
public void contextInitialized(ServletContextEvent event) {
event.getServletContext().setAttribute("bean", new Bean());
}
JSF将应用程序范围的bean作为ServletContext
的属性存储,并且JSF不会自动创建另一个bean,因此JSF将使用与上面的代码示例创建的一个bean相同的对象.也是
JSF stores application scoped beans as an attribute of the ServletContext
and JSF won't auto-create another one when one is already present, so the one and the same as created by the above code example will be used by JSF as well.
这篇关于JSF2:如何在部署时启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!