将相同组件的值作为参数传递给p:ajax listener:“PropertyNotFoundException”

时间:2014-03-02 14:30:35

标签: java ajax jsf jsf-2 primefaces

我已在括号内输入了值('06717-100'),方法正常,但我不知道如何将同一个组件的值作为参数传递。我希望用户在p:inputMask中键入一个zipcode,当焦点丢失时(或者 - 会更好 - 当达到字段的最大长度时),调用在数据库中搜索它的方法。 / p>

<h:form id="frm">

    <h:panelGrid>

    <p:outputPanel autoUpdate="true"
               id="pnl">

        <p:inputMask id="txtCep"
             mask="99999-999"
             placeholder="Digite o CEP"
             value="#{cepMB.cep.cep}">
        <p:ajax event="blur"
            listener="#{cepMB.busca('06717-100')}"
            process="@this"
            update="frm:pnl" />
        </p:inputMask>

以下是方法busca的代码:

public void busca(String cep) {
    Cep c = this.cepRepository.findByCep(cep);
    if(c.getId() != null) {
        this.cep = c;
    }
}

我正在使用JSF 2.2,PrimeFaces 4.0,GlassFish 4.0和带有JPA 2.1的Hibernate 4.3.1。

堆栈跟踪:

    Advertência:   javax.el.PropertyNotFoundException: /principal.xhtml @59,31 value="#{cepMB.cep.cep}": Target Unreachable, 'null' returned null
javax.faces.FacesException: javax.el.PropertyNotFoundException: /principal.xhtml @59,31 value="#{cepMB.cep.cep}": Target Unreachable, 'null' returned null
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:270)
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:57)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1193)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    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:646)
    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:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)
Caused by: javax.el.PropertyNotFoundException: /principal.xhtml @59,31 value="#{cepMB.cep.cep}": Target Unreachable, 'null' returned null
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    at org.primefaces.util.ComponentUtils.getConverter(ComponentUtils.java:124)
    at org.primefaces.renderkit.InputRenderer.getConvertedValue(InputRenderer.java:183)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046)
    at javax.faces.component.UIInput.validate(UIInput.java:976)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1249)
    at javax.faces.component.UIInput.processValidators(UIInput.java:712)
    at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:552)
    at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1689)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at javax.faces.component.UIForm.visitTree(UIForm.java:371)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:399)
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:263)
    ... 33 more
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, 'null' returned null
    at com.sun.el.parser.AstValue.getTarget(AstValue.java:192)
    at com.sun.el.parser.AstValue.getType(AstValue.java:86)
    at com.sun.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:201)
    at org.jboss.weld.el.WeldValueExpression.getType(WeldValueExpression.java:93)
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98)
    ... 50 more

#{cepMB.cep}引用实体Cep#{cepMB.cep.cep}引用public String getCep() Cep

1 个答案:

答案 0 :(得分:1)

将您的JSF代码更改为:

<p:inputMask id="txtCep"
     mask="99999-999"
     placeholder="Digite o CEP"
     value="#{cepMB.cep.cep}">
<p:ajax event="blur"
    listener="#{cepMB.busca(cepMB.cep.cep)}"
    process="@this"
    update="frm:pnl" />
</p:inputMask>

另外,请确保cep中的cepMB字段始终使用权限null进行初始化:

@ManagedBean
@ViewScoped
public class CepMB {
    Cep cep;
    @PostConstruct
    public void init() {
        cep = new Cep();
    }
    //rest of your getters, setters, code...
}