由于某种原因,以下代码无法正常工作,我也不明白为什么。我目前的看法是,由于有些细微的改动而大惊小怪,我的想法是,因为在ExternalContext
导致异常之前创建该bean。但是,ExternalContext
应该在这一点上存在,或者我相信。
我有一些解决方法,例如在LoginBean中实例化并发送到MessagesBean以供以后使用的MessageProvider类,但是我更喜欢将会话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()
上崩溃。新问题:如何将Messages.properties链接到getMessageBundle()
调用,还是应该将其转换为getResourceBundle("bundle")
?更新资料
我通过在我的faces-config.xml中设置
<message-bundle>
使其工作。除了声明为<resource-bundle>
之外,是否有任何其他方法可以包含它而无需在每个.xhtml文件中加载? 最佳答案
请检查不等于null的@Postconstruct
方法中的messageBundle语句。
即
if(messageBundle != null)
messageBundle = ResourceBundle.getBundle(
facesContext.getApplication().getMessageBundle(),
locale);