覆盖 JSF Primefaces 消息标签

发布于 2025-01-04 21:34:46 字数 189 浏览 2 评论 0原文

我是否可以覆盖 的默认实现,以始终隐式地使用 display="text" 而不是及时完整地输入?

<p:message for="hotelName" display="text" />

Can I override the default implementation of <p:message ../> to always implicitly have display="text" instead of typing out in full in time?

<p:message for="hotelName" display="text" />

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

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

发布评论

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

评论(2

完美的未来在梦里 2025-01-11 21:34:46

您可以为此扩展 PrimeFaces 的 MessageRenderer。只需重写 encodeEnd() 方法,在调用 super 方法之前设置默认属性即可。

这是一个独立的启动示例:

package com.example;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.primefaces.component.message.MessageRenderer;

public class CustomMessageRenderer extends MessageRenderer {

    @Override
    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
        component.getAttributes().put("display", "text"); // You might want to check first if it isn't already set.
        super.encodeEnd(facesContext, component);
    }

}

您需要在 faces-config.xml 中注册,如下所示(不,注释魔法无法覆盖现有的渲染器,这些渲染器本身是由 XML 注册的,所以 XML 是绝对必要的):

<render-kit>
    <renderer>
        <component-family>org.primefaces.component</component-family>
        <renderer-type>org.primefaces.component.MessageRenderer</renderer-type>
        <renderer-class>com.example.CustomMessageRenderer</renderer-class>
    </renderer>
</render-kit>

但更简单的方法是添加一点 CSS 来隐藏图标。

.ui-message-error-icon {
    display: none;
}

按照另一个答案的建议创建复合组件并不完全是微不足道的,因为 for 目标不包含在同一复合材料中。

You could extend the PrimeFaces' MessageRenderer for that. Just override the encodeEnd() method wherein you set the default attribute before calling the super method.

Here's a self-containing kickoff example:

package com.example;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.primefaces.component.message.MessageRenderer;

public class CustomMessageRenderer extends MessageRenderer {

    @Override
    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
        component.getAttributes().put("display", "text"); // You might want to check first if it isn't already set.
        super.encodeEnd(facesContext, component);
    }

}

which you need to register in faces-config.xml as follows (no, annotation magic won't work in overriding the existing renderers which are by itself registered by XML, so the XML is absolutely necessary):

<render-kit>
    <renderer>
        <component-family>org.primefaces.component</component-family>
        <renderer-type>org.primefaces.component.MessageRenderer</renderer-type>
        <renderer-class>com.example.CustomMessageRenderer</renderer-class>
    </renderer>
</render-kit>

But easier would be to just throw in a little bit of CSS to hide the icon.

.ui-message-error-icon {
    display: none;
}

Creating a composite component as suggested by the other answer isn't exactly trivial because the for target of the <p:message> isn't contained within the same composite.

つ低調成傷 2025-01-11 21:34:46

我只需为此创建一个 Facelet 组合组件。

I would simply create a Facelet Composition Component for that.

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