JSF ViewScope bean、viewParam、ajax 请求用 NULL 更新 MB 属性

JSF ViewScope bean, viewParam, ajax request update the MB attribute with NULL

我有一个应用程序在 payara 4.1.9.2 上使用 JSF 2.2

 <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
 </context-param>

这里一切都按预期工作。

现在我正在尝试将其更新到 Payara 5.1.9.3,即 运行 Mojarra 2.3.9.payara-p3。

我现在面临的问题是,如果 ViewScope bean 在创建 bean 时使用 f:viewParam 设置参数来触发 ajax 请求 viewParams 丢失(正常) 并且模型更新为 NULL 值

所以最初 viewParams 工作正常,值取自 GET 并设置模型 accordingly.When 第一个 ajax 请求在 UPDATE_MODEL_VALUES PHASE bean 属性中触发value 设置为旧值(可以),但在第二个 ajax 请求中,也在 PHASE 4 中,属性值更新为 NULL。

我知道有很多关于这个主题的帖子(f:viewParams 在 ajax 请求中丢失了),但我不清楚的是:

为什么如果我禁用 javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL 指令一切正常,如果我不再启用它?

PS。使用 Payara 4.1.2.174 和 Mojarra 2.2.14 正在运行

更新:

这里我们有 counter.xhtml

的视图
<ui:composition template="../layouts/user.xhtml"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:p="http://primefaces.org/ui"
  xmlns:sec="http://www.springframework.org/security/tags">

<f:metadata>
    <f:viewParam name="id" value="#{counterMB.id}"/>        
</f:metadata>

<ui:param name="currentMB" value="#{counterMB}"/>   
<ui:define name="title">Counter </ui:define>

<h:form id="myForm">
    Id is <h:outputText value="#{counterMB.id}" id="outputTxt"/>

    <p:commandLink 
            action="#{counterMB.incrementCounter()}"
            value="Increment"
            update="outputTxt"
    />

    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</h:form>   

和豆子:

@Named
@Scope("view")
public class CounterMB extends GenericMB implements Serializable{

    private static final long serialVersionUID = 1L;


    private Integer id;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        System.out.println("Setting the id with:"+id);
        this.id = id;
    }



    public void incrementCounter() {        

        id = id +1;

        // do something with id
        System.out.println("Id is="+Integer.toString(id));      
    }
}  

和一些日志:

http://localhost:8080/myapp/counter.xhtml?id=4

(LifecycleListener.java:22) - START PHASE RESTORE_VIEW 1
(LifecycleListener.java:26) - END PHASE RESTORE_VIEW 1
(LifecycleListener.java:22) - START PHASE APPLY_REQUEST_VALUES 2
(LifecycleListener.java:26) - END PHASE APPLY_REQUEST_VALUES 2
(LifecycleListener.java:22) - START PHASE PROCESS_VALIDATIONS 3
(LifecycleListener.java:26) - END PHASE PROCESS_VALIDATIONS 3
(LifecycleListener.java:22) - START PHASE UPDATE_MODEL_VALUES 4

2019-10-24T13:43:52.573+0000|Info: Setting the id with:4
(LifecycleListener.java:26) - END PHASE UPDATE_MODEL_VALUES 4
(LifecycleListener.java:22) - START PHASE INVOKE_APPLICATION 5
(LifecycleListener.java:26) - END PHASE INVOKE_APPLICATION 5
(LifecycleListener.java:22) - START PHASE RENDER_RESPONSE 6
(LifecycleListener.java:26) - END PHASE RENDER_RESPONSE 6


//click on increment 1st time
(LifecycleListener.java:22) - START PHASE RESTORE_VIEW 1
(LifecycleListener.java:26) - END PHASE RESTORE_VIEW 1
(LifecycleListener.java:22) - START PHASE APPLY_REQUEST_VALUES 2
(LifecycleListener.java:26) - END PHASE APPLY_REQUEST_VALUES 2
(LifecycleListener.java:22) - START PHASE PROCESS_VALIDATIONS 3
(LifecycleListener.java:26) - END PHASE PROCESS_VALIDATIONS 3
(LifecycleListener.java:22) - START PHASE UPDATE_MODEL_VALUES 4
2019-10-24T13:44:07.013+0000|Info: Setting the id with:4
(LifecycleListener.java:26) - END PHASE UPDATE_MODEL_VALUES 4
(LifecycleListener.java:22) - START PHASE INVOKE_APPLICATION 5
2019-10-24T13:44:07.014+0000|Info: Id is=5
(LifecycleListener.java:26) - END PHASE INVOKE_APPLICATION 5
(LifecycleListener.java:22) - START PHASE RENDER_RESPONSE 6
(LifecycleListener.java:26) - END PHASE RENDER_RESPONSE 6


// click on icrement the second time
(LifecycleListener.java:22) - START PHASE RESTORE_VIEW 1
(LifecycleListener.java:26) - END PHASE RESTORE_VIEW 1
(LifecycleListener.java:22) - START PHASE APPLY_REQUEST_VALUES 2
(LifecycleListener.java:26) - END PHASE APPLY_REQUEST_VALUES 2
(LifecycleListener.java:22) - START PHASE PROCESS_VALIDATIONS 3
LifecycleListener.java:26) - END PHASE PROCESS_VALIDATIONS 3
(LifecycleListener.java:22) - START PHASE UPDATE_MODEL_VALUES 4
2019-10-24T13:44:12.548+0000|Info: Setting the id with:null
(LifecycleListener.java:26) - END PHASE UPDATE_MODEL_VALUES 4
(LifecycleListener.java:22) - START PHASE INVOKE_APPLICATION 5
2019-10-24T13:44:12.549+0000|Warning: #{counterMB.incrementCounter()}: java.lang.NullPointerException
javax.faces.FacesException: #{counterMB.incrementCounter()}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.getNavigationOutcome(ActionListenerImpl.java:96)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:71)
    at org.springframework.faces.webflow.FlowActionListener.processAction(FlowActionListener.java:71)
    at org.springframework.faces.model.SelectionTrackingActionListener.processAction(SelectionTrackingActionListener.java:64)
    at org.primefaces.application.DialogActionListener.processAction(DialogActionListener.java:54)
    at javax.faces.component.UICommand.broadcast(UICommand.java:222)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:847)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1395)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:58)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:177)
    at javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:707)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1628)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:339)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:209)
    at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:251)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:209)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:124)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:155)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:251)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:209)
    at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:251)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:209)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:755)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:575)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:520)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:217)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:182)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:156)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:218)
    at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:95)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:524)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:33)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:114)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
    at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:76)
    at com.sun.faces.application.ActionListenerImpl.getNavigationOutcome(ActionListenerImpl.java:82)
    ... 82 more
Caused by: java.lang.NullPointerException
    at com.myapp.jsf.user.CounterMB.incrementCounter(CounterMB.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.el.ELUtil.invokeMethod(ELUtil.java:263)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:494)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:215)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:285)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:65)
    at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:66)
    ... 83 more

2019-10-24T13:44:12.551+0000|Info: INFO [http-thread-pool::http-listener-1(2)] (LifecycleListener.java:26) - END PHASE INVOKE_APPLICATION 5

一种解决方法是在 f:viewParam 上渲染 =“#{not facesContext.postback}”,但我认为这不是正确的行为。

更新:
在 Kukeltje sugestion,我尝试使用普通的 JSF 应用程序,使用 @ViewScoped 并且它以 expected.With 的形式工作,我将继续添加 primefaces 和 spring 以及......我在当前应用程序尝试模拟当前情况。

更新:

它使用纯 jsf 的原因是 h:commandLink 没有发出 ajax 请求。我能够用这些文件重现它:

`

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">

    <h:head>
        <f:facet name="first">
            <meta http-equiv="X-UA-Compatible" content="IE=edge" />
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />         
        </f:facet>      
        <title> Simulator - Counter rest</title>
    </h:head>

    <h:body>    
        <f:metadata>
            <f:viewParam name="id" value="#{counterMB.id}"/>        
        </f:metadata>

        <h:form id="myForm">
            Id is <h:outputText value="#{counterMB.id}" id="outputTxt"/>

            <h:commandButton value="Increment"update="outputTxt">           
                <f:ajax listener="#{counterMB.incrementCounter()}" execute="myForm" render="outputTxt"/> 
            </h:commandButton>
        </h:form>         
    </h:body>
</html>

`
和这个豆子

import java.io.Serializable;

import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named
@ViewScoped
public class CounterMB implements Serializable{

    private static final long serialVersionUID = 1L;

    private Integer id; 

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        System.out.println("Setting the id with:"+id);
        this.id = id;
    }

    public void incrementCounter() {
        id = id +1;
        System.out.println("Id is="+Integer.toString(id));      
    }
}

我搜索了一下,发现了 3 个东西(不确定这是不是答案,甚至根本不确定答案,但输入评论太多了)

  1. 是 'duplicate' (未回答但有一些讨论)
  2. 是一个link似乎与includeViewParams=true converts null model value to empty string in query string
  3. 相关
  4. 是来自 http://showcase.omnifaces.org/components/viewParam 的关于 JSF 2.3 中与 viewParam 和 null 相关的更改的一些信息。

    In JSF 2.3, this has been fixed and has only effect when javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL context param is set to true.

这种组合让我认为你 'were lucky' 它在 2.2 中有效(确定回传中的 id 不是“0”?)老实说,2.3 行为 'feels' 正确。

作为解决方案,您还可以在 setter

中添加 'null' 检查
void setId(Integer id) {
    if (id != null) {
        this.id = id;
    }
}

或者您可以尝试 o:viewParam,看看它是否有所作为。