Primefaces 自动完成在 spring 上获得 NPE

Primefaces autocomplete gets NPE on spring

我正在尝试在 spring(4.1.3) + jsf(2.1.29) 上实现 primefaces(5.1) 的自动完成功能。 这是 primefaces 实际展示的示例代码。最终,我不得不尝试一下基本概念。我的问题是,支持 bean 按预期工作,但不知何故,建议列表设置为 null,因此 AutoCompleteRenderer:492 处的增强 for 循环抛出 NPE,这是预期的。我正在使用 Spring bean 来利用 AOP。我认为它与 spring-jsf-scope 相关,但我真的很困惑如何解决这个问题。感谢任何帮助。

java.lang.NullPointerException: null
at org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeSuggestionsAsList(AutoCompleteRenderer.java:492) ~[primefaces-5.1.jar:5.1]
at org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeSuggestions(AutoCompleteRenderer.java:393) ~[primefaces-5.1.jar:5.1]
at org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeResults(AutoCompleteRenderer.java:123) ~[primefaces-5.1.jar:5.1]
at org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeEnd(AutoCompleteRenderer.java:105) ~[primefaces-5.1.jar:5.1]
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:881) ~[glassfish.jsf_2.0.0.0_2-1-20.jar:2.0.0.0_2-1-20]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786) ~[glassfish.jsf_2.0.0.0_2-1-20.jar:2.0.0.0_2-1-20]
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:543) ~[jsf-impl-2.1.29.jar:2.1.29]
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183) ~[jsf-impl-2.1.29.jar:2.1.29]
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1612) ~[glassfish.jsf_2.0.0.0_2-1-20.jar:2.0.0.0_2-1-20]
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623) ~[glassfish.jsf_2.0.0.0_2-1-20.jar:2.0.0.0_2-1-20]
at javax.faces.component.UIForm.visitTree(UIForm.java:371) ~[glassfish.jsf_2.0.0.0_2-1-20.jar:2.0.0.0_2-1-20]
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623) ~[glassfish.jsf_2.0.0.0_2-1-20.jar:2.0.0.0_2-1-20]
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623) ~[glassfish.jsf_2.0.0.0_2-1-20.jar:2.0.0.0_2-1-20]
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:383) ~[jsf-impl-2.1.29.jar:2.1.29]
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:302) ~[jsf-impl-2.1.29.jar:2.1.29]
at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60) ~[primefaces-5.1.jar:5.1]
at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:973) ~[glassfish.jsf_2.0.0.0_2-1-20.jar:2.0.0.0_2-1-20]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779) ~[glassfish.jsf_2.0.0.0_2-1-20.jar:2.0.0.0_2-1-20]
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:421) ~[jsf-impl-2.1.29.jar:2.1.29]
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125) ~[jsf-impl-2.1.29.jar:2.1.29]
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) ~[jsf-impl-2.1.29.jar:2.1.29]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) ~[jsf-impl-2.1.29.jar:2.1.29]
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) [jsf-impl-2.1.29.jar:2.1.29]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) [glassfish.jsf_2.0.0.0_2-1-20.jar:2.0.0.0_2-1-20]
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:346) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79) [weblogic.server.merged.jar:12.1.3.0.0]
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:105) [primefaces-5.1.jar:5.1]
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79) [weblogic.server.merged.jar:12.1.3.0.0]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.3.RELEASE.jar:4.1.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344) [spring-web-4.1.3.RELEASE.jar:4.1.3.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261) [spring-web-4.1.3.RELEASE.jar:4.1.3.RELEASE]
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79) [weblogic.server.merged.jar:12.1.3.0.0]
at tr.com.innova.portal.web.filter.XSSFilter.doFilter(XSSFilter.java:19) [XSSFilter.class:na]
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3436) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120) [com.oracle.css.weblogic.security.wls_7.1.0.0.jar:CSS 7.1 0.0]
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2285) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2201) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1572) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311) [weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.work.ExecuteThread.run(ExecuteThread.java:263) [weblogic.server.merged.jar:12.1.3.0.0]

我的支持 bean(注意它使用自定义范围管理):

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import tr.com.innova.portal.model.entity.Employee;
import tr.com.innova.portal.model.entity.EmployeeEvaluation;
import tr.com.innova.portal.service.business.api.EmployeeService;
import tr.com.innova.portal.service.business.api.EvaluationService;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Component(value = "employeeAutocompleteView")
@Scope("session")
public class EmployeeAutocompleteView {
    private String name;
    private Employee current;
    private List<Employee> employees;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private EvaluationService evaluationService;

    @PostConstruct
    public void init(){
        this.current = null;
        this.name = null;
    }

    public List<String> completeText(String query) {
        List<String> results = new ArrayList<String>();
        for (int i=0 ; i< 10 ; i++) {
            results.add(query + i);
        }
        return results;
    }

    public void onItemSelect(SelectEvent event) {
        fillReport();
        RequestContext.getCurrentInstance().update(":reportForm");
    }

还有我的 xhtml:

    <h:form>
        <div>
            <p:autoComplete id="employeeSelector" 
                value="#{employeeAutocompleteView.name}" 
                completeMethod="#{employeeAutocompleteView.completeText}"/>
        </div>
    </h:form>

我们想通了。 pom.xml 中有 jsf 的 mojarrra 和 sun 实现。我不知道它是如何到达那里的,也不知道为什么,但是删除 sun 实现解决了这个问题。希望这对以后的人有所帮助。

另一个原因:p:autocomplete 在 h:form 标签之外。