gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:42 24 4
gpt4 key购买 nike

我有两个 jsf Controller ,分别是 ControllerA 和 ControllerB。两者都在 session 范围内。我想从 ControllerA 调用 ControllerB 中的创建方法。我用了@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)

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

最佳答案

答案已经在异常消息中:

Property detailsXX for managed bean controllerA does not exist. Check that appropriate getter and/or setter methods exist.

您需要为属性提供 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() 方法移回到类中。然而,这不会导致您得到异常,而是导致一个简单的编译错误。以后请谨慎准备题中的代码,以免误入歧途。

关于java - 使用@ManagedProperty 在托管bean 之间调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8969791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com