Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 用ajax文章突出显示JSF字段?_Jsf 2_Ajax4jsf - Fatal编程技术网

Jsf 2 用ajax文章突出显示JSF字段?

Jsf 2 用ajax文章突出显示JSF字段?,jsf-2,ajax4jsf,Jsf 2,Ajax4jsf,我将在此基础上重点介绍JSF中的字段。我的计划是输出一个带有ID的JSON数组,然后调用一个方法来处理这个数组。当我不使用时,这个很好用 以下是我的阶段侦听器解决方案: public void beforePhase(PhaseEvent event) { FacesContext facesContext = event.getFacesContext(); List<String> highlightFields = new ArrayList<String

我将在此基础上重点介绍JSF中的字段。我的计划是输出一个带有ID的JSON数组,然后调用一个方法来处理这个数组。当我不使用时,这个很好用

以下是我的阶段侦听器解决方案:

public void beforePhase(PhaseEvent event) {
    FacesContext facesContext = event.getFacesContext();
    List<String> highlightFields = new ArrayList<String>();

    Iterator<String> highlightFieldsItr = facesContext
            .getClientIdsWithMessages();
    while (highlightFieldsItr.hasNext()) {
        StringBuilder sb = new StringBuilder();
        sb.append("#");
        sb.append(highlightFieldsItr.next().replaceAll(":", "\\\\:"));
        highlightFields.add(sb.toString());
    }
    JSONArray jsonHighlightFields = new JSONArray(highlightFields);

    facesContext.getExternalContext().getRequestMap()
            .put("errorFields", jsonHighlightFields.toString());
}
然而,我需要在函数中以某种方式获得这个列表,该函数在ajax帖子成功时被调用

现在f:ajax确实有onevent属性,并且这个函数确实被调用了,但是我不确定它到底传递了什么。我如何能够以某种方式将无效ID从阶段侦听器传递到此函数?似乎传递了一个表示HTMLInputElement的对象

  <f:ajax event="change" onevent="test" render="test test_msg" immediate="true" />

很高兴听到其他建议或想法。我们的目标基本上是聚焦和突出显示字段,这些字段不仅在完整的回帖中无效,而且在使用f:ajax时也是无效的


谢谢

那篇文章更侧重于JSF1.x。JSF 2.x现在提供了许多优势,其中以下优势有助于满足您的特定需求:

  • 您可以通过
    {component}
    引用EL中的当前组件。对于输入组件,这是具有
    isValid()
    方法的。这可以在
    styleClass
    属性中使用
  • 您可以使用
    重新渲染部分视图,也可以使用
    元素
1+1是

<h:inputText id="input1" value="#{bean.input1}" required="true" styleClass="#{component.valid ? '' : 'error'}">
    <f:ajax render="@this input1message focus" />
</h:inputText> 
<h:message id="input1message" for="input1" />
...
<h:inputText id="input2" value="#{bean.input2}" required="true" styleClass="#{component.valid ? '' : 'error'}">
    <f:ajax render="@this input2message focus" />
</h:inputText> 
<h:message id="input2message" for="input2" />
...
<h:panelGroup id="focus"><script>jQuery(":input.error:first").focus();</script></h:panelGroup>

...
...
jQuery(“:input.error:first”).focus();
不再需要
PhaseListener
。如有必要,您可以将输入标记样板包装在或中



回到您关于
onevent
属性的具体问题,检查以下答案:

我看到来自ajax帖子的XML响应有一个刚刚执行的标记。我怎样才能在响应中插入一些javascript?似乎规范支持它,但不知道我将如何实现它。当使用复合组件时,如果组件的
cc:implementation
之外存在
panelGroup
,则无法找到
focus
id属性。如果有一种方法可以让复合组件引用id属性,那么举个例子来支持这个答案将非常有用。
  <f:ajax event="change" onevent="test" render="test test_msg" immediate="true" />
<h:inputText id="input1" value="#{bean.input1}" required="true" styleClass="#{component.valid ? '' : 'error'}">
    <f:ajax render="@this input1message focus" />
</h:inputText> 
<h:message id="input1message" for="input1" />
...
<h:inputText id="input2" value="#{bean.input2}" required="true" styleClass="#{component.valid ? '' : 'error'}">
    <f:ajax render="@this input2message focus" />
</h:inputText> 
<h:message id="input2message" for="input2" />
...
<h:panelGroup id="focus"><script>jQuery(":input.error:first").focus();</script></h:panelGroup>