使用p元素而不是span渲染JSF h:message [英] Render JSF h:message with p element instead of span

查看:153
本文介绍了使用p元素而不是span渲染JSF h:message的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义消息渲染器,将h:message渲染为"p" html元素,而不是"span"元素.它涉及以下消息标记:

I would like to create a custom message renderer to renders h:message as a 'p' html element instead of as a 'span' element. It concerns the following message tag:

<h:message id="firstNameErrorMsg" for="firstname" class="error-msg"  />

我已经在下面编写了代码,但这只是呈现一个空的'p'元素.我想我必须复制原始组件中的所有属性和文本并将其写入编写器.但是,我不知道在哪里可以找到所有内容,仅替换标签就需要大量工作.

I've written to code underneath, but that's only rendering an empty 'p' element. I suppose I have to copy all attributes and text from the original component and write it to the writer. However, I don't know where to find everything and it seems to be a lot of work for just a replacement of a tag.

是否有更好的方法来将h:message标签呈现为'p'元素?

Is there a better way to get an h:message tag rendered as a 'p' element?

代码:

@FacesRenderer(componentFamily = "javax.faces.Message", rendererType = "javax.faces.Message")
public class FoutmeldingRenderer extends Renderer {

    @Override
    public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {

        ResponseWriter writer = context.getResponseWriter();
        writer.startElement("p", component);
        writer.endElement("p");

    }
}

推荐答案

这并不是很多工作".基本上,这是从标准JSF消息呈示器扩展而来,复制粘贴其大约200行的encodeEnd()方法,然后仅编辑2行以用"p"替换"span"的问题.不到一分钟就可以完成.

It isn't exactly "a lot of work". It's basically a matter of extending from the standard JSF messages renderer, copypasting its encodeEnd() method consisting about 200 lines then editing only 2 lines to replace "span" by "p". It's doable in less than a minute.

但是,是的,我同意这是一种明显的丑陋方法.

But yes, I agree that this is a plain ugly approach.

您可以考虑以下替代方案,这些替代方案不一定更容易,但至少更干净:

You can consider the following alternatives which are not necessarily more easy, but at least more clean:

  1. 首先,使用<p>代替的语义值是什么?在这种特定情况下<span>的含义?老实说,我没有看到任何语义上的价值.因此,我建议将其保留为<span>.如果唯一的功能要求是让它像<p>一样出现 ,则只需放入一些CSS.例如

  1. First of all, what's the semantic value of using a <p> instead of a <span> in this specific case? To be honest, I'm not seeing any semantic value for this. So, I suggest to just keep it a <span>. If the sole functional requirement is to let it appear like a <p>, then just throw in some CSS. E.g.

.error-msg {
    display: block;
    margin: 1em 0;
}


  • 您可以直接在EL中获取特定客户ID的所有消息,如下所示,假设父表单的ID为formId:

    #{facesContext.getMessageList('formId:firstName')}
    

    因此,要打印第一条消息的摘要和详细信息,只需执行以下操作:

    So, to print the summary and detail of the first message, just do:

    <c:set var="message" value="#{facesContext.getMessageList('formId:firstName')[0]}" />
    <p title="#{message.detail}">#{message.summary}</p>
    

    您始终可以将其隐藏在自定义标签文件中,如下所示:

    You can always hide it away into a custom tag file like so:

    <my:message id="firstNameErrorMsg" for="firstname" class="error-msg" />
    


  • 使用 OmniFaces <o:messages> .指定var属性后,就可以像<ui:repeat>一样使用它.


  • Use OmniFaces <o:messages>. When the var attribute is specified, then you can use it like an <ui:repeat>.

    <o:messages for="firstNameErrorMsg" var="message">
        <p title="#{message.detail}">#{message.summary}</p>
    </o:messages>
    

  • 这篇关于使用p元素而不是span渲染JSF h:message的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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