JSF 2.0和JSTL使用c:set标记来存储一些临时数据 [英] JSF 2.0 and JSTL use of c:set tag to store some temporary data

查看:147
本文介绍了JSF 2.0和JSTL使用c:set标记来存储一些临时数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用JSF 2.0(mojarra)+ primefaces开发一个Web应用程序.过去,我成功地使用了jstl库的[c:set]标签来存储一些临时数据或从其他标签输出.

I'm developing a web application with JSF 2.0 (mojarra) + primefaces. In the past I successfully used the [c:set] tag of jstl library to store some temporary data or output form other tags.

在我目前的情况下,我想再次使用它,但是它不能正常工作,我也不知道为什么.在下面的示例中,它特别有效.为什么案例2无法正常工作?

In my current case I want to use that again but it doesn't work properly and I have no idea why. In the follow example it works but particularly. Why does the case 2 not work properly?

<h:form id="userAdministration">
  <p:messages id="messages" showDetail="true" />
  <p:dataTable id="userTable" selectionMode="single" var="user" value="#{users}">
    <p:column>
      <f:facet name="header">
        <h:outputText value="#{message.user_table_header_id_column}" />
      </f:facet>
      <h:outputText value="#{user.id}" />
    </p:column>

    <p:column>
      <f:facet name="header">
        <h:outputText value="#{message.global_table_header_action_column}" />
      </f:facet>
      <p:commandButton type="push" onclick="#{user.loginname}DeleteConfirmation.show()" value="#{message.global_table_action_delete}" image="ui-icon-trash">
        <f:setPropertyActionListener value="#{user}" target="#{userAdministrationController.selectedUser}" />
      </p:commandButton>
      <!-- 1. WORKS FINE, STORED VALUE IS "loginname" -->
      <c:set var="deleteConfirmationMessage" value="#{user.loginname}"></c:set>
      <!-- 2. VALUE IS "!!!" AND NOT "loginname !!!" -->
      <c:set var="deleteConfirmationMessage2">
        <h:outputText value="#{user.loginname}" />!!!
      </c:set>
      <!-- 3. WORKS FINE (OUTPUT "loginname") -->
      <h:outputText value="#{user.loginname}" />
      <p:confirmDialog  message="#{deleteConfirmationMessage}" header="#{message.user_dialog_delete_confirmation_title}" severity="alert" widgetVar="#{user.loginname}DeleteConfirmation">
        <p:commandButton value="#{message.user_dialog_delete_confirmation_no}" onclick="#{user.loginname}DeleteConfirmation.hide()" update="@form" type="button" /> 
      </p:confirmDialog>
    </p:column>

推荐答案

为什么情况2无法正常工作?

 <!-- 2. VALUE IS "!!!" AND NOT "loginname !!!" -->
 <c:set var="deleteConfirmationMessage2">
   <h:outputText value="#{user.loginname}" />!!!
 </c:set>

这是因为标记处理程序和UI组件不能同时运行. JSTL标记是标记处理程序,它们在构建视图期间运行(将XHTML文件转换为JSF组件树时). JSF <h:xxx>标签是UI组件,它们在呈现视图期间运行(当JSF组件树被转换/呈现为HTML代码时).另请参见 JSF2 Facelets中的JSTL ...有意义吗?

That's because taghandlers and UI components doesn't run at the same time. JSTL tags are taghandlers and they run during building the view (when the XHTML file is converted to JSF component tree). JSF <h:xxx> tags are UI components and they run during rendering the view (when the JSF component tree is converted/rendered to HTML code). See also JSTL in JSF2 Facelets... makes sense?

因此,当<c:set>运行时,<h:outputText>根本没有运行.

So, when the <c:set> runs, the <h:outputText> hasn't run at all.

但是在这个特定的结构中,您实际上根本不需要<h:outputText>.除了将其设置为<c:set>value之外,您还可以内联模板文本中的原始EL表达式(请注意,这在将JSF与Facelets一起使用时有效,而不是在JSP与JSP一起使用时有效;重新使用PrimeFaces,您肯定是在使用Facelets,因为PrimeFaces根本没有JSP taglib.

But in this particular construct you actually don't need the <h:outputText> at all. Apart from setting it as value of <c:set>, you could also just inline the EL expression raw in the template text (note that this works when using JSF with Facelets, not when using JSF with JSP; given the fact that you're using PrimeFaces, you're definitely using Facelets as PrimeFaces doesn't have a JSP taglib at all).

 <c:set var="deleteConfirmationMessage2">
   #{user.loginname}!!!
 </c:set>

或者也许您不知道可以在类似这样的属性中混合使用EL和纯字符串

or perhaps you weren't aware that you can mix EL and plain strings in an attribute like

 <c:set var="deleteConfirmationMessage2" value="#{user.loginname}!!!" />

<p:confirmDialog  message="#{user.loginname}!!!">

根据具体的功能要求(问题尚不清楚),另一种替代方法可能是使用 OmniFaces 的nofollow noreferrer> <o:cache> 组件.如果您打算为更广泛的范围而不是像<c:set>示例中的页面/请求范围那样缓存值,这可能会更有用.

Depending on the concrete functional requirement, which isn't exactly clear from the question, a different alternative may be to use the <o:cache> component of OmniFaces. This may be more useful if you intend to cache the value for a broader scope than just the page/request scope like as in your <c:set> examples.

这篇关于JSF 2.0和JSTL使用c:set标记来存储一些临时数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆