【问题标题】:ui:repeat value expression evaluated on every ajax requestui:repeat 对每个 ajax 请求求值的表达式
【发布时间】:2016-06-04 05:58:03
【问题描述】:

我有一个带有非常简单表单的页面,它提交仅针对同一表单中的组件的 ajax 请求。在同一页面中(但在表单之外),还有一个 ui:repeat 迭代从 request scoped 托管 bean 返回的数组(假设是产品类别列表)。此 bean 没有绑定在表单中的属性,并且除了 ui:repeat 标记的 value 属性之外,不能以任何其他方式访问。我不明白为什么 JSF 需要在每个 ajax 回发上重新创建请求范围的 bean,只是 好像 我要求渲染这个外部 ui:repeat(与表单无关)以及表单内的一些组件。

这是一个错误吗?或者这是一个预期的行为?当然,我可以将 bean 注释为 ViewScoped,但我看不出将类别存储在视图范围中的原因,因为它们在回发之间是完全静态的。

我发现的另一个解决方案/解决方法是仅在非 ajax 请求的情况下呈现 ui:repeat:

<ul>
    <ui:repeat value="#{someRequestScopedBean.categories}" var="category" rendered="#{not facesContext.partialViewContext.ajaxRequest}">
        <li>#{category.name}</li>
    </ui:repeat>
</ul>

但我不知道这是否会导致问题并且看起来不太清楚。

测试用例

index.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h1>Test page</h1>
        <p>Random jokes</p>
        <ul>
            <ui:repeat value="#{oneLiners.list}" var="oneliner">
                <li>#{oneliner}</li>
            </ui:repeat>
        </ul>
        <h:form>
            <h:selectOneMenu value="#{backingBean.greeting}" hideNoSelectionOption="true">
                <f:selectItem value="#{null}" itemLabel="Select a greeting..." noSelectionOption="true"/>
                <f:selectItems value="#{backingBean.greetings}"/>
                <f:ajax render="@this btn"/>
            </h:selectOneMenu>
            <h:commandButton id="btn" value="Say Hello!" disabled="#{empty backingBean.greeting}">
                <f:ajax render="otxt"/>
            </h:commandButton>
            <h:outputText id="otxt" value="#{backingBean.greeting}, Maurizio!" style="display: #{empty backingBean.greeting ? 'none' : 'block'}"/>
        </h:form>
    </h:body>
</html>

请求作用域 bean:

package testuirepajax;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author maurizio
 */
@ManagedBean
@RequestScoped
public class OneLiners {
    private String[] list;

    public OneLiners() {
        System.out.println("testuirepajax.OneLiners.<init>()");
        list = new String[] {
            "Life is wonderful. Without it we'd all be dead.",
            "Daddy, why doesn't this magnet pick up this floppy disk?",
            "Daddy, what does FORMATTING DRIVE C mean?",
            "Never forget: 2 + 2 = 5 for extremely large values of 2.",
            "C:\\ is the root of all directories."
        };
    }

    public String[] getList() {
        System.out.println("testuirepajax.OneLiners.getList()");
        return list;
    }
}

表单支持 bean:

package testuirepajax;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author maurizio
 */
@ManagedBean
@ViewScoped
public class BackingBean {

    private String[] greetings;
    private String greeting;

    public BackingBean() {
        System.out.println("testuirepajax.BackingBean.<init>()");
        greetings = new String[] {
          "Hello", "Hi", "Good morning", "Good evening", "Good night"
        };
    }

    public String[] getGreetings() {
        return greetings;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

    public String getGreeting() {
        return greeting;
    }
}

检查容器的输出。使用 Payara 服务器(附带 Mojarra 2.2.12),我看到如下行:

Informazioni:   testuirepajax.OneLiners.<init>()
Informazioni:   testuirepajax.OneLiners.getList()
Informazioni:   testuirepajax.OneLiners.getList()
Informazioni:   testuirepajax.OneLiners.getList()
Informazioni:   testuirepajax.OneLiners.getList()
Informazioni:   testuirepajax.OneLiners.getList()
Informazioni:   testuirepajax.OneLiners.<init>()
Informazioni:   testuirepajax.OneLiners.getList()
Informazioni:   testuirepajax.OneLiners.getList()
Informazioni:   testuirepajax.OneLiners.getList()
Informazioni:   testuirepajax.OneLiners.getList()
Informazioni:   testuirepajax.OneLiners.getList()

从菜单中选择元素或单击“打个招呼!”时按钮。

【问题讨论】:

  • 是否包含输入组件?哪个 JSF 实现/版本? Mojarra 2.2.7 和 2.1.29 中修复了类似的错误。那里有很好的解决方法!如有必要,请为未来的读者添加&lt;!-- comment --&gt;,以免他们被迷惑。
  • @BalusC 1) 该表单包含一对h:selectOneMenu 和一个h:button,但也适用于包含h:commandButton 的表单。 2) 莫哈拉 2.2.12
  • 他们在 ui:repeat?那么,这种副作用是不可避免的,因为 JSF 无论如何都需要重申它,以便在回发期间恢复这些组件的状态。不幸的是,JSF(鸡蛋和所有)不支持“部分恢复”。
  • @BalusC ui:repeat 内没有输入组件。它们仅在表格内。我现在正在写一个测试用例。
  • @BalusC 我不知道是否相关,但我正在使用捆绑 Mojarra 2.2.12 的 Payara 服务器。

标签: ajax jsf uirepeat


【解决方案1】:

我在getList() 方法上放置了一个断点,并在回发期间“不必要地”命中调用堆栈时检查了调用堆栈,以了解谁和为什么:

Daemon Thread [http-nio-8088-exec-5] (Suspended (breakpoint at line 23 in OneLiners))   
    owns: NioChannel  (id=83)   
    OneLiners.getList() line: 23    
    NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
    NativeMethodAccessorImpl.invoke(Object, Object[]) line: 62  
    DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43  
    Method.invoke(Object, Object...) line: 497  
    BeanELResolver.getValue(ELContext, Object, Object) line: 97 
    DemuxCompositeELResolver._getValue(int, ELResolver[], ELContext, Object, Object) line: 176  
    DemuxCompositeELResolver.getValue(ELContext, Object, Object) line: 203  
    AstValue.getValue(EvaluationContext) line: 169  
    ValueExpressionImpl.getValue(ELContext) line: 184   
    TagValueExpression.getValue(ELContext) line: 109    
    UIRepeat.getValue() line: 279   
    UIRepeat.getDataModel() line: 255   
    UIRepeat.visitTree(VisitContext, VisitCallback) line: 727   
    HtmlBody(UIComponent).visitTree(VisitContext, VisitCallback) line: 1700 
    UIViewRoot(UIComponent).visitTree(VisitContext, VisitCallback) line: 1700   
    PartialViewContextImpl.processComponents(UIComponent, PhaseId, Collection<String>, FacesContext) line: 403  
    PartialViewContextImpl.processPartial(PhaseId) line: 266    
    UIViewRoot.processDecodes(FacesContext) line: 927   
    ApplyRequestValuesPhase.execute(FacesContext) line: 78  
    ApplyRequestValuesPhase(Phase).doPhase(FacesContext, Lifecycle, ListIterator<PhaseListener>) line: 101  
    LifecycleImpl.execute(FacesContext) line: 198   
    FacesServlet.service(ServletRequest, ServletResponse) line: 658 
    ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 291  
    ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 206  
    WsFilter.doFilter(ServletRequest, ServletResponse, FilterChain) line: 52    
    ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 239  
    ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 206  
    StandardWrapperValve.invoke(Request, Response) line: 212    
    StandardContextValve.invoke(Request, Response) line: 106    
    FormAuthenticator(AuthenticatorBase).invoke(Request, Response) line: 502    
    StandardHostValve.invoke(Request, Response) line: 141   
    ErrorReportValve.invoke(Request, Response) line: 79 
    AccessLogValve(AbstractAccessLogValve).invoke(Request, Response) line: 616  
    StandardEngineValve.invoke(Request, Response) line: 88  
    CoyoteAdapter.service(Request, Response) line: 521  
    Http11NioProcessor(AbstractHttp11Processor<S>).process(SocketWrapper<S>) line: 1096 
    Http11NioProtocol$Http11ConnectionHandler(AbstractProtocol$AbstractConnectionHandler<S,P>).process(SocketWrapper<S>, SocketStatus) line: 674    
    NioEndpoint$SocketProcessor.doRun() line: 1500  
    NioEndpoint$SocketProcessor.run() line: 1456    
    ThreadPoolExecutor(ThreadPoolExecutor).runWorker(ThreadPoolExecutor$Worker) line: 1142  
    ThreadPoolExecutor$Worker.run() line: 617   
    TaskThread$WrappingRunnable.run() line: 61  
    TaskThread(Thread).run() line: 745  

有趣的行是FacesServlet 上面的行。类/方法名称已经不言自明了。

它因此发生在应用请求值阶段,此时部分请求需要处理组件的解码。访问组件树是为了找到由&lt;f:ajax execute&gt;(默认为@this)中指定的客户端ID 标识的组件。由于&lt;ui:repeat&gt; 位于感兴趣的组件之前,因此首先对其进行检查。 visitTree() 触发完全迭代,因为感兴趣的客户端 ID 仅在迭代期间可用。

当我将&lt;ui:repeat&gt; 移动到&lt;h:form&gt; 下方时,它不再被调用。那时所有感兴趣的组件都已找到。

不幸的是,这种行为是“设计使然”。你的工作是一个很好的。更好的是检查 #{not facesContext.postback},因为这也包括非 ajax 回发。

【讨论】:

  • 谢谢巴鲁斯。看看我制作的其他 facelets,我也在考虑同样的想法(移动 ui:repeat 以查看位置是否相关)。现在一切都一清二楚了。
猜你喜欢
  • 2013-03-30
  • 2015-03-29
  • 2019-03-24
  • 2018-12-22
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
相关资源
最近更新 更多