0

当我尝试覆盖 getAsObject 函数时遇到问题

@FacesConverter("countryConverter")
public class CountryConverter implements Converter {

@EJB private CountryService countryService;

     @Override
        public Object getAsObject(FacesContext context, UIComponent arg1, String arg2){

         Country country = countryService.findByName(arg2);
         return country;
    }

}

所以当我在每个帖子中加载我的组合框时

<p:selectOneMenu value="#{registerController.selectedCountry}"
                converter="countryConverter">
                <f:selectItems value="#{registerController.countries}" var="country"
                    itemLabel="#{country.name}" itemValue="#{country}" />
            </p:selectOneMenu>

我在 findbyName 行中遇到了一个 nullPointer 异常!我检查了 arg2 参数,它带有一个有效值,我还测试了服务功能,它也工作正常......在 CountryConverter 类之外。

正如我在 getAsObject 函数行中所描述的,我得到的错误如下

SEVERE: java.lang.NullPointerException
    at com.garagesalelive.controllers.converter.CountryConverter.getAsObject(CountryConverter.java:29)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:319)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.getConvertedValue(SelectOneMenuRenderer.java:58)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
    at javax.faces.component.UIInput.validate(UIInput.java:960)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
    at javax.faces.component.UIInput.processValidators(UIInput.java:698)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at org.primefaces.component.dialog.Dialog.processValidators(Dialog.java:378)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
    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:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    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 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    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:722)

SEVERE:     at com.garagesalelive.controllers.converter.CountryConverter.getAsObject(CountryConverter.java:29)
SEVERE:     at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
SEVERE:     at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202)
SEVERE:     at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:319)
SEVERE:     at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.getConvertedValue(SelectOneMenuRenderer.java:58)
SEVERE:     at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
SEVERE:     at javax.faces.component.UIInput.validate(UIInput.java:960)
SEVERE:     at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
SEVERE:     at javax.faces.component.UIInput.processValidators(UIInput.java:698)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at org.primefaces.component.dialog.Dialog.processValidators(Dialog.java:378)
SEVERE:     at javax.faces.component.UIForm.processValidators(UIForm.java:253)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
SEVERE:     at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
SEVERE:     at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
SEVERE:     at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
SEVERE:     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
SEVERE:     at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
SEVERE:     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
SEVERE:     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
SEVERE:     at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
SEVERE:     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
SEVERE:     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
SEVERE:     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
SEVERE:     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
SEVERE:     at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
SEVERE:     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
SEVERE:     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
SEVERE:     at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
SEVERE:     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
SEVERE:     at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
SEVERE:     at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
SEVERE:     at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
SEVERE:     at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
SEVERE:     at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
SEVERE:     at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
SEVERE:     at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
SEVERE:     at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
SEVERE:     at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
SEVERE:     at java.lang.Thread.run(Thread.java:722)
4

3 回答 3

2

我认为您的 countryService 为空。可能是注射问题。

试试这个

@ManagedBean
@RequestScoped
@FacesConverter("countryConverter")
public class CountryConverter implements Converter {...
于 2013-03-05T21:37:24.170 回答
1

@EJB 不适用于不受容器管理的 bean。据我了解,它仅适用于 JSF Managed Beans 、其他 EJB 和 servlet。

如果您也在使用 CDI 库,那么您可以尝试,

@Inject private CountryService countryService;

或以其他方式进行查找,

    InitialContext ctx = new InitialContext();
    CountryService  countryService = 
(CountryService)ctx.lookup("ejb/countryService");

Your JNDI name will depend on your container or will be value of mappedBy attribute in your EJB if that is supported by your container and is specified by you on EJB.

于 2013-03-06T03:49:29.630 回答
-2

by calling ManagedBean through FacesContext:

 context.getApplication().getELResolver().getValue(context.getELContext(), null, "nameOfManagedBean");
于 2013-11-02T13:44:37.933 回答