如何编写和配置自定义 Facelet 来覆盖 h:messages?

发布于 2024-11-28 17:56:01 字数 371 浏览 4 评论 0原文

我正在将最初用 Weblogic Beehive 编写的站点重写为 JSF 2.0,并且遇到了一个问题,我需要将新站点生成的消息转换为完全 匹配现有站点的输出。

我尝试使用很好的标签,但是现有消息框架的样式和逻辑不太适合此输出。

JSF 中是否有选项允许我创建一个自定义标记,该标记不仅会输出消息,而且还会对它们执行条件逻辑?

例如,我一直在创建带有摘要和详细信息字符串的 FacesMessages,但旧站点的格式仅显示最高摘要。例如,我添加 4 条消息,每条消息都有相同的摘要消息和不同的详细信息。我只想显示严重性最高的摘要。

另外,我需要将消息显示为框并设置样式,而不是每条消息和一些详细信息都需要有条件 CSS 类。

I am in the middle of rewriting a site that was originally written in Weblogic Beehive to JSF 2.0, and I'm running into an issue where I need to get the messages that the new site produces into a format that EXACTLY matches the output of the existing site.

I have tried using the tag which is nice, but the style and logic of the existing messages framework does not fit nicely into this output.

Are there options in JSF that would allow me to create a custom tag that will not only output the messages, but perform conditional logic on them as well?

For example, I have been creating FacesMessages with a summary and detail String, but the format of the old site only displays the highest summary. For example I add 4 messages, each has the same summary message, and different details. I only want to display the summary with the highest severity.

Also, I need the messages displayed and styled as a box, not each message and some of the details need to have conditional CSS classes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月棠 2024-12-05 17:56:01

如果您使用的是 JSF 2.x,则可以迭代 FacesContext#getMessageList() 在循环中。每个项目都是一个 FacesMessage< /a> 又具有多个吸气剂。

<ui:repeat value="#{facesContext.messageList}" var="facesMessage">
    Severity: #{facesMessage.severity}<br />
    Summary: #{facesMessage.summary}<br />
    Detail: #{facesMessage.detail}<br />
    <br />
</ui:repeat>

这允许在消息周围进行更细粒度的 HTML 标记。

如果您仍在使用 JSF 1.x,在 FacesContext 中缺少此方法,那么您需要收集 FacesContext#getMessages() 在首先在一些实用程序 bean 甚至 EL 函数的帮助下 List

List<FacesMessage> messageList = new ArrayList<FacesMessage>();
Iterator<FacesMessage> messages = FacesContext.getCurrentInstance().getMessages();

while (messages.hasNext()) {
    messageList.add(messages.next());
}

然后您可以使用 在视图中迭代此 messageList ;同样的方式。

If you're using JSF 2.x, then you can iterate over FacesContext#getMessageList() in a loop. Each item is a FacesMessage which in turn has several getters.

<ui:repeat value="#{facesContext.messageList}" var="facesMessage">
    Severity: #{facesMessage.severity}<br />
    Summary: #{facesMessage.summary}<br />
    Detail: #{facesMessage.detail}<br />
    <br />
</ui:repeat>

This allows for more fine-grained HTML markup around the messages.

If you're still on JSF 1.x which lacks this method in FacesContext, then you need to collect FacesContext#getMessages() in a List<FacesMessage> first with help of some utility bean or even an EL function:

List<FacesMessage> messageList = new ArrayList<FacesMessage>();
Iterator<FacesMessage> messages = FacesContext.getCurrentInstance().getMessages();

while (messages.hasNext()) {
    messageList.add(messages.next());
}

You can then iterate over this messageList in your view using <ui:repeat> the same way.

萌逼全场 2024-12-05 17:56:01

我最终在我的 JSF 页面 (xhtml) 中得到了这个:

            <c:set var="messageList" value="#{facesContext.messageList}" />
            <c:set var="maxSev" value="#{facesContext.maximumSeverity.ordinal}" />
            <ui:repeat value="${messageList}" var="fm" >
                <c:choose>
                     <c:when test="${fm.severity.ordinal eq 0}">
                         <c:set var="summaryStyleClass" value="outcome"/>
                         <c:set var="detailStyleClass" value="outcome_details"/>
                     </c:when>
                     <c:when test="${fm.severity.ordinal eq 1}">
                         <c:set var="summaryStyleClass" value="warning_title"/>
                         <c:set var="detailStyleClass" value="warning"/>
                     </c:when>
                     <c:when test="${fm.severity.ordinal eq 2}">
                         <c:set var="summaryStyleClass" value="error_title"/>
                         <c:set var="detailStyleClass" value="error"/>
                     </c:when>
                     <c:when test="${fm.severity.ordinal eq 3}">
                         <c:set var="summaryStyleClass" value="fatal"/>
                         <c:set var="detailStyleClass" value="fatal_details"/>
                     </c:when>
                </c:choose>
            </ui:repeat>
            <div class="${detailStyleClass}" >
                 <div class="${summaryStyleClass}">
                        <ui:repeat value="${messageList}" var="fm" >
                             <c:choose>
                                 <c:when test="${fm.severity.ordinal eq maxSev}">
                                     ${fm.summary}
                                 </c:when>
                             </c:choose>
                        </ui:repeat>
                 </div>
                  <ul>
                     <ui:repeat value="${messageList}" var="msg">
                         <li><h:outputText value="${msg.detail}"/></li>
                     </ui:repeat>
                </ul>
            </div>

我确信我在这里打破了一个模式,但是该部分似乎无法在可以解析summaryStyleClass和detailStyleClass变量的地方工作。我正在迭代它们,因为我需要根据页面的最高严重性消息来设置消息样式。这是我在浏览器页面中获得的渲染输出:

            <div>
                 <div>Warning:</div>
                 <ul>
                      <li>Warning message.</li>
                </ul>
            </div>

注意没有样式...

I ended up with this in my JSF page (xhtml):

            <c:set var="messageList" value="#{facesContext.messageList}" />
            <c:set var="maxSev" value="#{facesContext.maximumSeverity.ordinal}" />
            <ui:repeat value="${messageList}" var="fm" >
                <c:choose>
                     <c:when test="${fm.severity.ordinal eq 0}">
                         <c:set var="summaryStyleClass" value="outcome"/>
                         <c:set var="detailStyleClass" value="outcome_details"/>
                     </c:when>
                     <c:when test="${fm.severity.ordinal eq 1}">
                         <c:set var="summaryStyleClass" value="warning_title"/>
                         <c:set var="detailStyleClass" value="warning"/>
                     </c:when>
                     <c:when test="${fm.severity.ordinal eq 2}">
                         <c:set var="summaryStyleClass" value="error_title"/>
                         <c:set var="detailStyleClass" value="error"/>
                     </c:when>
                     <c:when test="${fm.severity.ordinal eq 3}">
                         <c:set var="summaryStyleClass" value="fatal"/>
                         <c:set var="detailStyleClass" value="fatal_details"/>
                     </c:when>
                </c:choose>
            </ui:repeat>
            <div class="${detailStyleClass}" >
                 <div class="${summaryStyleClass}">
                        <ui:repeat value="${messageList}" var="fm" >
                             <c:choose>
                                 <c:when test="${fm.severity.ordinal eq maxSev}">
                                     ${fm.summary}
                                 </c:when>
                             </c:choose>
                        </ui:repeat>
                 </div>
                  <ul>
                     <ui:repeat value="${messageList}" var="msg">
                         <li><h:outputText value="${msg.detail}"/></li>
                     </ui:repeat>
                </ul>
            </div>

I'm sure I'm breaking a pattern here, but the section doesn't seem to be working where it can resolve summaryStyleClass and detailStyleClass vars. I'm iterating through them because I need to style the message depending on what the highest severity message is for the page. This is the rendered output that I'm getting in the page in the browser:

            <div>
                 <div>Warning:</div>
                 <ul>
                      <li>Warning message.</li>
                </ul>
            </div>

Notice no styles...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文