由于某种原因,下面的代码不能工作,我不明白为什么。通过对几个细微的变化进行处理,我目前的想法是,因为bean是在ExternalContext
出现之前创建的,所以会导致异常。然而,ExternalContext
应该在这一点上存在,至少我是这样认为的。
我有一些变通的想法,例如在LoginBean中实例化MessageProvider类并将其发送到MessagesBean以供以后使用,但我更喜欢使用与下面格式相近的会话bean,而不是让它充当包装器。如果你能解释一下为什么这样做失败了,如果可能的话,我将非常感激。
下面是两个主要关注的类:
LoginBean
@Named( value = "loginBean")
@RequestScoped
public class LoginBean {
private static final Logger LOG = Logger.getLogger(LoginBean.class.getName());
@Inject
private MessagesBean messagesBean;
@Inject
private LoginFormBean loginFormBean;
@Inject
private PersonBean personBean;
@Inject
private PersonFacade personFacade;
@NotNull
private String username;
@NotNull
private String password;
public LoginBean() {
}
/**
* Get the value of username
*
* @return the value of username
*/
public String getUsername() {
return username;
}
/**
* Set the value of username
*
* @param username new value of username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Get the value of password
*
* @return the value of password
*/
public String getPassword() {
return password;
}
/**
* Set the value of password
*
* @param password new value of password
*/
public void setPassword(String password) {
this.password = password;
}
public String login() {
Person person = personFacade.find(username);
if(person != null && person.getPasswordHash().equals(password)) {
personBean.setPerson(person);
return "success";
}
else {
messagesBean.setMessage(
loginFormBean.getUsernameInput().getClientId(),
"login.failed");
return "failure";
}
}
public String logout() {
return null;
}
}
MessagesBean
@Named(value = "messagesBean")
@SessionScoped
public class MessagesBean implements Serializable {
private static final long serialVersionUID = 1L;
private FacesContext facesContext;
private Locale locale;
private ResourceBundle messageBundle;
/**
* Creates a new instance of MessagesBean
*/
public MessagesBean() {
}
@PostConstruct
private void init()
{
facesContext = FacesContext.getCurrentInstance();
locale = facesContext.getExternalContext().getRequestLocale();
messageBundle =
ResourceBundle.getBundle(
facesContext.getApplication().getMessageBundle(),
locale);
}
public void setMessage(String clientId, String messageKey)
{
FacesMessage facesMessage =
new FacesMessage(messageBundle.getString(messageKey));
facesContext.addMessage(clientId, facesMessage);
}
}
下面是上述类发生错误的堆栈跟踪:
javax.faces.el.EvaluationException: org.jboss.weld.exceptions.WeldException: WELD-000049: Unable to invoke private void com.healthdataviews.healthdataviewstrial.support.MessagesBean.init() on com.healthdataviews.healthdataviewstrial.support.MessagesBean@1126aa67
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.jboss.weld.exceptions.WeldException: WELD-000049: Unable to invoke private void com.healthdataviews.healthdataviewstrial.support.MessagesBean.init() on com.healthdataviews.healthdataviewstrial.support.MessagesBean@1126aa67
at org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.invokeMethods(DefaultLifecycleCallbackInvoker.java:100)
at org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.postConstruct(DefaultLifecycleCallbackInvoker.java:81)
at org.jboss.weld.injection.producer.BasicInjectionTarget.postConstruct(BasicInjectionTarget.java:126)
at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:162)
at org.jboss.weld.util.bean.IsolatedForwardingBean.create(IsolatedForwardingBean.java:45)
at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96)
at org.jboss.weld.context.PassivatingContextWrapper$AbstractPassivatingContextWrapper.get(PassivatingContextWrapper.java:76)
at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:101)
at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.get(ContextualInstanceStrategy.java:178)
at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
at com.healthdataviews.healthdataviewstrial.support.MessagesBean$Proxy$_$$_WeldClientProxy.setMessage(Unknown Source)
at com.healthdataviews.healthdataviewstrial.controller.ejb.LoginBean.login(LoginBean.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 36 more
更新
我发现了错误发生的确切位置。当我调用getMessageBundle
时,我得到一个NullPointerException
,这导致ResourceBundle.getBundle()
搜索一个空包,这导致它在@PostConstruct init()
上崩溃。新问题:我如何链接我的消息。属性到getMessageBundle()
调用,或者我应该把它翻译成getResourceBundle("bundle")
吗?
更新
我通过在我的faces-config.xml中设置<message-bundle>
使其工作。除了将其声明为<resource-bundle>
之外,是否有任何方法可以在不加载每个.xhtml文件的情况下包含它?
请检查不等于null @Postconstruct
方法中messageBundle的语句
。
if(messageBundle != null)
messageBundle = ResourceBundle.getBundle(
facesContext.getApplication().getMessageBundle(),
locale);