1

我正在尝试更改 JSF 1.1 页面以有条件地隐藏页面的某些部分。该页面是使用混合的原始 HTML 和标签构建的。具体来说,我有以下内容:

<table>
  <tr>
    <td>Foo</td>
    <td><h:inputText ... /></td>
  </tr>
  <tr>
    <td>Bar</td>
    <td><h:inputText ... /></td>
  </tr>
  <!-- more stuff, including <h:dataTable>
</table>

我想将其包装在有条件地隐藏整个表格的标签中,但我似乎无法弄清楚。这是我尝试过的:

  • 将标记包裹在<h:panelGroup rendered="...">. 虽然这个正确显示/隐藏了标记,但所有原始 HTML 都从生成的 HTML 中剥离。
  • 将标记包裹在<f:verbatim>. 这不起作用,因为逐字标记在 JSF 1.1 中没有渲染属性
  • 将整个东西包裹在一个<h:panelGroup rendered="..."><f:verbatim>组合中。这与第一次尝试的效果相同。
  • 我也尝试过<f:view><f:subview>但无济于事。

我知道可以在 JSF 页面中包含 JSTL 标记并使用<c:if>,但我想避免这种情况。有任何想法吗?

注意:我意识到(至少在某些人看来)混合 HTML 和 JSF 被认为是不好的做法,但是这个页面是由其他人创建的,我只需要修改它(它的页面有点大,上面的 HTML 只是一个它的小片段)..

4

1 回答 1

1

要么替换<table><h:panelGrid>.

<h:panelGrid columns="2">
  <h:outputText value="Foo" />
  <h:inputText ... />

  <h:outputText value="Bar" />
  <h:inputText ... />

  <!-- more stuff, including <h:dataTable>
</h:panelGrid>

或者使用 CSS display:none/block:

<table style="display: ${some condition ? 'none' : 'block'};">

或者只是升级到 JSF 1.2。从技术上讲,JSF 1.1 Web 应用程序可以轻松升级到 JSF 1.2,而无需更改任何代码。只需更新 JAR 并更改faces-config.xml根声明以将 JSF 1.1 DTD 替换为 JSF 1.2 XSD。JSF 1.2 附带了一个改进的视图处理程序,它消除了<f:verbatim>噩梦(即不再需要它)。它还附带了许多错误修复和性能增强,您会非常感谢。


Unrelated to the concrete problem, as to your statement that mixing HTML and JSF is a bad practice, this isn't necessarily true. At least not since JSF 1.2 anymore. On JSF 1.0/1.1 you'd need to use <f:verbatim> which is in turn indeed a pain to develop/maintain. This caused the wrong myth that mixing JSF/HTML is "bad". See also What are the main disadvantages of Java Server Faces 2.0? for a bit of history on that.

于 2012-09-07T11:33:06.417 回答