使用@ManagedProperty在托管bean之间调用方法

时间:2012-01-23 09:55:32

标签: java jsf-2

我有两个jsf控制器说ControllerA和ControllerB。两者都在Session范围内。我想从ControllerA调用ControllerB中的create方法。我使用@ManagedProperty来做。但它似乎不起作用

@ManagedBean(name = "manageAccountController")
@SessionScoped
public class ControllerA implements Serializable{

    public void accountSetup()
    {
        @ManagedProperty(value="#{controllerB}")
        private ControllerB details;
        System.out.print("Inside  controller ");
        details.create();

    }

}

    public String prepareCreate() {
          System.out.print("Inside prepare create ");
        current = new ControllerB();
        selectedItemIndex = -1;
        return "Create";
    }

    public String create() {
        try {
            System.out.print("Inside create ");
            getJpaController().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("Created"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

我无法调用controllerB中的方法。控制台显示以下错误

    SEVERE: JSF will be unable to create managed bean controllerA  when it is requested.  The following problems where found:
     - Property detailsXX for managed bean controllerA does not exist.  Check that appropriate getter and/or setter methods exist.
     - Property detailsXXX for managed bean controllerA does not exist.  Check that appropriate getter and/or setter methods exist.
WARNING: Unable to create managed bean controllerA.  The following problems were found:
     - Property detailsXX for managed bean controllerA does not exist.  Check that appropriate getter and/or setter methods exist.
     - Property detailsXXX for managed bean controllerA does not exist.  Check that appropriate getter and/or setter methods exist.
com.sun.faces.mgbean.ManagedBeanCreationException: Unable to create managed bean controllerA.  The following problems were found:
     - Property detailsXX for managed bean controllerA does not exist.  Check that appropriate getter and/or setter methods exist.
     - Property detailsXXX for managed bean controllerA does not exist.  Check that appropriate getter and/or setter methods exist.
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:265)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:99)
    at com.sun.el.parser.AstValue.getTarget(AstValue.java:127)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:217)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    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:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    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:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:619)

请指出我的错误...... 感谢

2 个答案:

答案 0 :(得分:10)

答案已在异常消息中:

  

托管bean controllerA的属性detailsXX不存在。 检查是否存在适当的getter和/或setter方法。

您需要为该属性提供一个getter和setter。

@ManagedBean("manageAccountController")
@SessionScoped
public class ControllerA implements Serializable{

    @ManagedProperty("#{controllerB}")
    private ControllerB details;

    public ControllerB getDetails() {
        return details;
    }

    public void setDetails(ControllerB details) {
        this.details = details;
    }

}

另请注意,我将托管属性声明从accountSetup()方法移回了类。但是,这不会导致您获得的异常,但是在一个简单的编译错误中。在将来,请小心准备问题中的代码以避免红色鲱鱼。

答案 1 :(得分:1)

尝试放置

@ManagedProperty(value="#{controllerB}")
private ControllerB details;

在ControllerA类中,而不是在方法accountSetup中,并为它添加getter / setter ......