我有一个xhtml页面,在其中我使用存储在辅助bean中的信息。但是我需要在xhtml使用之前在bean构造中做一些操作。该操作包括从数据库中检索数据
然后我使用一个方法进行此操作,并将其称为search
并使用@PostConstruct注释它。但当我打电话给我的xhtml页面时,我得到以下例外:
type Exception report
message An error occurred performing resource injection on managed bean MemberSearchControler
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: An error occurred performing resource injection on managed bean MemberSearchControler
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
root cause
com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on managed bean MemberSearchControler
com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:227)
com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103)
com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:69)
org.apache.el.parser.AstValue.getValue(AstValue.java:112)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
javax.faces.component.UIData.getValue(UIData.java:732)
javax.faces.component.UIData.getDataModel(UIData.java:1811)
javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:484)
javax.faces.component.UIData.setRowIndex(UIData.java:473)
com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:82)
javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:864)
javax.faces.component.UIData.encodeBegin(UIData.java:1133)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1894)
javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1896)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:451)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
org.apache.myfaces.tomahawk.application.ResourceViewHandlerWrapper.renderView(ResourceViewHandlerWrapper.java:169)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
root cause
com.sun.faces.spi.InjectionProviderException: java.lang.IllegalArgumentException: Invalid PostConstruct annotation
com.sun.faces.vendor.Tomcat6InjectionProvider.invokePostConstruct(Tomcat6InjectionProvider.java:120) com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:221) com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103) com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409) com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:69) org.apache.el.parser.AstValue.getValue(AstValue.java:112) org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186) com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) javax.faces.component.UIData.getValue(UIData.java:732) javax.faces.component.UIData.getDataModel(UIData.java:1811) javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:484) javax.faces.component.UIData.setRowIndex(UIData.java:473) com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:82) javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:864) javax.faces.component.UIData.encodeBegin(UIData.java:1133) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1894) javax.faces.render.Renderer.encodeChildren(Renderer.java:176) javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1896) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899) com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:451) com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) org.apache.myfaces.tomahawk.application.ResourceViewHandlerWrapper.renderView(ResourceViewHandlerWrapper.java:169) com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) root cause
java.lang.IllegalArgumentException: Invalid PostConstruct annotation org.apache.catalina.util.DefaultAnnotationProcessor.postConstruct(DefaultAnnotationProcessor.java:85) com.sun.faces.vendor.Tomcat6InjectionProvider.invokePostConstruct(Tomcat6InjectionProvider.java:118) com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:221) com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103) com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409) com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:69) org.apache.el.parser.AstValue.getValue(AstValue.java:112) org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186) com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) javax.faces.component.UIData.getValue(UIData.java:732) javax.faces.component.UIData.getDataModel(UIData.java:1811) javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:484) javax.faces.component.UIData.setRowIndex(UIData.java:473) com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:82) javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:864) javax.faces.component.UIData.encodeBegin(UIData.java:1133) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1894) javax.faces.render.Renderer.encodeChildren(Renderer.java:176) javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1896) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899) com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:451) com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) org.apache.myfaces.tomahawk.application.ResourceViewHandlerWrapper.renderView(ResourceViewHandlerWrapper.java:169) com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) note The full stack trace of the root cause is available in the Apache Tomcat/6.0.37 logs.
这是我的支持bean:
ManagedBean(name="MemberSearchControler")
@SessionScoped
public class MemebrSearchControl {
public MemebrSearchControl(){
System.out.println("Test");
}
private String text;
private List<Member> members;
//For Create CheckBox
private int[] membersId;
public int[] getMembersId() {
return membersId;
}
//For get Selected Member Id
private int[] selectedMembers;
public void setSelectedMember(int[] ids) {
selectedMembers=ids;
}
public List<Member> getMembers() {
return members;
}
public int getMembersCount() {
return membersCount;
}
private int membersCount;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@PostConstruct
public String search() throws DaoImplNotFoundException, DataSourceException{
MemberDao dao=(MemberDao) Factory.getDao(ConfigReader.getConfig().getProperty("dao.member"));
members=dao.list(1, 10);
membersId=new int[members.size()];
for(int i=0;i<members.size();i++)
membersId[i]=members.get(i).getId();
return "member";
}
}
答案 0 :(得分:6)
阅读the documentation总是有帮助的,更具体地说是这些行(强调我的):
应用PostConstruct注释的方法必须满足以下所有条件 - - 除了EJB拦截器之外,方法绝不能有任何参数,在这种情况下它需要一个InvocationC ontext EJB规范定义的对象。 - 方法的返回类型必须无效。 - 该方法不得抛出已检查的异常 ...(其他要求已省略)
所以,至少不满足返回类型要求(我们不知道您的异常是否被检查,所以我删除了它们,但是在这种情况下你需要处理方法中的异常)。
总而言之,用
替换你的方法@PostConstruct
public void search() {
MemberDao dao=(MemberDao) Factory.getDao(ConfigReader.getConfig().getProperty("dao.member"));
members=dao.list(1, 10);
membersId=new int[members.size()];
for(int i=0;i<members.size();i++)
membersId[i]=members.get(i).getId();
}
根据您的评论,您的例外类是经过检查的例外情况。因此,您必须在方法中引入try-catch块,或者让您的异常从RuntimeException
继承。如果您选择以前的方式,则可以重新抛出未经检查的异常,例如IllegalStateException
,或者,例如,发送重定向,以防它为时已晚:
@PostConstruct
public void search() {
try {
MemberDao dao=(MemberDao) Factory.getDao(ConfigReader.getConfig().getProperty("dao.member"));
members=dao.list(1, 10);
membersId=new int[members.size()];
for(int i=0;i<members.size();i++)
membersId[i]=members.get(i).getId();
} catch(DaoImplNotFoundException dinf) {
//option one
throw new IllegalStateException("Something went wrong.");
//option two
FacesContext.getCurrentInstance().getExternalContext().redirect("/error.xhtml");
} //other exception types
}