如何在Facelets页面中避免重复/使用常量? [英] How to avoid repetitions / use constants in Facelets page?

查看:78
本文介绍了如何在Facelets页面中避免重复/使用常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Facelets页面中,我具有各种<h:inputText><h:outputText>组件,它们都需要相同的转换器.

In a Facelets page, I have various <h:inputText> and <h:outputText> components, which all need the same converter.

我想避免重复使用所有参数的转换器,如下所示:

I'd like to avoid repeating the converter with all its parameters, like this:

<h:inputText id="bla" value="#{mybean.val}" >
  <f:convertNumber locale="en" maxFractionDigits="3" minFractionDigits="3"/>
</h:inputText>
[...]
<h:outputText id="bla2" value="#{mybean.val2}" >
  <f:convertNumber locale="en" maxFractionDigits="3" minFractionDigits="3"/>
</h:outputText>
[...]
<h:inputText id="bla3" value="#{mybean.val3}" >
  <f:convertNumber locale="en" maxFractionDigits="3" minFractionDigits="3"/>
</h:inputText>

避免这些重复的最佳方法是什么?

What is the best way to avoid these repetitions?

我想我可以使用<ui:include>,但这意味着我只需要为一行写一个单独的文件,这似乎有点愚蠢.有其他选择吗?

I think I could use <ui:include>, but that would mean I'd have to have a separate file just for a single line, which seems a bit silly. Is there an alternative?

推荐答案

子类化转换器,从而在构造函数中设置默认值.

Subclass the converter whereby you set the defaults in the constructor.

@FacesConverter("defaultNumberConverter")
public class DefaultNumberConverter extends NumberConverter {

    public DefaultNumberConverter() {
        setLocale(Locale.ENGLISH);
        setMinFractionDigits(3);
        setMaxFractionDigits(3);
    }

}

并按如下所示使用它:

<h:inputText id="bla" value="#{mybean.val}" converter="defaultNumberConverter" />
[...]
<h:outputText id="bla2" value="#{mybean.val2}" converter="defaultNumberConverter" />
[...]
<h:inputText id="bla3" value="#{mybean.val3}" converter="defaultNumberConverter" />

要更进一步,请

这篇关于如何在Facelets页面中避免重复/使用常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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