【问题标题】:JSF (1.2) bug when reRender h:selectOneRadio重新渲染 h:selectOneRadio 时出现 JSF (1.2) 错误
【发布时间】:2013-07-13 05:30:28
【问题描述】:

当我通过 a4j 进行重新渲染时:在 h:selectOneRadio 上支持 Richfaces (3.3.3),我有一个奇怪的行为(不是每次)。

这是我的带有 h:selectOneRadio 的 xhtml 部分:

<h:selectOneRadio id="toto" value="#controller.toto}" >
   <f:selectItem itemLabel="#{bundle['oui']}"
   itemValue="#{true}" />
   <f:selectItem itemLabel="#{bundle['non']}"
   itemValue="#{false}" />
</h:selectOneRadio>
<rich:message for="toto" />

当我进入浏览器时,我得到了这个 html 代码:

<table id="formulaire:toto" >
<tbody>
<tr>
<td>
<input id="formulaire:toto:0" type="radio" onchange="A4J.AJAX.Submit('formulaire',event,{'control':this,'similarityGroupingId':'formulaire:support','parameters':{'ajaxSingle':'formulaire:toto','formulaire:support':'formulaire:support'} } )" value="true" name="formulaire:toto">
<label for="formulaire:toto:0"> oui</label>
</td>
<td>
<input id="formulaire:toto:1" type="radio" onchange="A4J.AJAX.Submit('formulaire',event,{'control':this,'similarityGroupingId':'formulaire:support','parameters':{'ajaxSingle':'formulaire:toto','formulaire:support':'formulaire:support'} } )" value="false" name="formulaire:toto" checked="checked">
<label for="formulaire:toto:1"> non</label>
</td>
</tr>
</tbody>
</table>

一切都很好。

但是当这个被另一个组件重新渲染时,html会发生变化并且第一个radio元素不在表格中:

<input type="radio" onchange="A4J.AJAX.Submit('formulaire',event,{'control':this,'similarityGroupingId':'formulaire:support','parameters':{'ajaxSingle':'formulaire:toto','formulaire:support':'formulaire:support'} } )" value="true" id="formulaire:toto:0" name="formulaire:toto" checked="checked">
<label for="formulaire:toto:0"> oui</label>
<table id="formulaire:toto">
<tbody>
<tr>
<td>
    <input type="radio" onchange="A4J.AJAX.Submit('formulaire',event,{'control':this,'similarityGroupingId':'formulaire:support','parameters':{'ajaxSingle':'formulaire:toto','formulaire:support':'formulaire:support'} } )" value="false" id="formulaire:toto:1" name="formulaire:toto"><label for="formulaire:toto:1"> non</label>
</td>
</tr>
</tbody>
</table>

这是一种非常奇怪的行为,它打破了我的风格。

为什么 reRender 会破坏 html 代码?

谢谢。

【问题讨论】:

  • 您要重新渲染哪个组件?
  • 我从另一个组件重新渲染 h:selectOneRadio,如下所示:

标签: jsf richfaces jsf-1.2


【解决方案1】:

经过多次搜索,bug来自neko。

为了优化性能,我改变了 neko 的 xml 解析(默认很小),这个可以破坏 html。

【讨论】:

  • 用 XML 解析器解析 HTML?不是一个好主意,最好使用 JSoup 之类的东西。
  • 就是jsf生成的xhtml。
  • 我可以确认不使用 Neko 也为我解决了这个问题。许多关于 JSF/RichFaces 性能的文章都建议切换到 Neko,但这样做显然会产生一些副作用。
【解决方案2】:

RichFaces 3 AJAX 响应带有 Content-Type: text/xml 标头。 因此,它们应该是有效的 XML,并由 RichFaces JavaScript 客户端代码在浏览器端进行解析。

另一方面,从模板生成的 HTML 不需要是有效的 XML,因此当 RichFaces 重新渲染模板的片段时,it tidies it up by default with the Tidy parser 以避免客户端解析问题。

通常建议用 NekoHTML 替换 Tidy 解析器以获得更好的性能。 但是,NekoHTML 在整理过程中可能会走得太远,并且会破坏一些 RichFaces/JSF 组件,主要是那些基于 HTML 元素的组件。

例如,从版本 1.9.13 开始,NekoHTML automatically adds TBODY around TR nested directly within TABLE。 降级到 5.5 年前的版本 1.9.12 会恢复此行为并修复组件的重新渲染。 但恕我直言,这不是一个选择,因为过去几年已经解决了很多问题。

根据我的经验,获得性能并避免这些问题的更好方法是:

  1. 确保模板的重新渲染部分是有效的 XML

  2. 删除整理的解析器

对应的web.xml sn-p:

<context-param>
  <param-name>org.ajax4jsf.xmlparser.ORDER</param-name>
  <param-value>NONE</param-value>
</context-param>

为避免一次中断太多,可以使用模式逐页管理过渡:

<context-param>
  <param-name>org.ajax4jsf.xmlparser.ORDER</param-name>
  <param-value>TIDY,NEKO,NONE</param-value>
</context-param>
<context-param>
  <param-name>org.ajax4jsf.xmlparser.TIDY</param-name>
  <param-value>/pages/tidy/.*\.xhtml</param-value>
</context-param>
<context-param>
  <param-name>org.ajax4jsf.xmlparser.NEKO</param-name>
  <param-value>/pages/neko/.*\.xhtml</param-value>
</context-param>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-09
    • 2010-11-12
    • 2011-02-06
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多