1

私は 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.  

これは私のバッキングビーンです:

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";
    }


}
4

1 に答える 1

7

documentation、より具体的にはこれらの行を読むことは常に役に立ちます(強調は私のものです):

PostConstruct アノテーションが適用されるメソッドは、次のすべての基準を満たさなければなりません - - メソッドは、EJB 仕様で定義された InvocationC ontext オブジェクトを取る EJB インターセプターの場合を除いて、パラメーターを持ってはなりません (MUST NOT)。-メソッドの戻り値の型は void でなければなりません。-メソッドは、チェックされた例外をスローしてはなりません... (その他の要件は省略)

したがって、少なくとも戻り値の型の要件は満たされていません (例外がチェックされているかどうかがわからないため、チェックされている場合に備えてそれらを削除しましたが、この場合はメソッドで例外を処理する必要があります)。

全体として、メソッドを次のように置き換えます

@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
}
于 2013-11-29T19:41:35.360 に答える