java - 在 h :outputText into capitalize String? 内转换字符串

标签 java jsf seam

如何在 h:outputText 中转换字符串?这是 h:outputText 的代码:

<h:outputText value="#{item.label} : " />

我试过用这个,

<s:convertStringUtils format="capitalize" trim="true"/>

但它给我错误: “没有为名称定义标签:convertStringUtils”

最佳答案

有几种方法。

  1. 使用 CSS text-transform: capitalize属性(property)。

    <h:outputText value="#{bean.text}" styleClass="capitalized" />
    

    .capitalized {
        text-transform: capitalize;
    }
    
  2. 创建自定义 Converter .

    <h:outputText value="#{bean.text}" converter="capitalizeConverter" />
    

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
        if (modelValue == null || ((String) modelValue).isEmpty()) {
            return null;
        }
    
        String string = (String) modelValue;
        return new StringBuilder()
            .append(Character.toTitleCase(string.charAt(0)))
            .append(string.substring(1))
            .toString();
    }
    
  3. 使用 OmniFaces ' of:capitalize() function .

    <html ... xmlns:of="http://omnifaces.org/ui">
    ...
    <h:outputText value="#{of:capitalize(bean.text)}" />
    

<s:convertStringUtils>您正在尝试的不是来自 Seam。它来自 MyFaces 沙盒。

关于java - 在 h :outputText into capitalize String? 内转换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426854/

相关文章:

java - 按钮点击不起作用 : button does nothing

java - Algolia Historical SDK 的许可证 key

java - 为什么我的 PrimeFaces 上传工具不起作用?

jsf - 如何重置 p :dialog together with the disable-state of the contained components?

java - 如何定制 seam-gen 以支持另一个数据库?

java - 是否可以直接将方法的返回值赋值给变量?

java - 如何使用 WindowBuilder 在内部类中编辑 JPanel

jsf - faces-redirect和返回按钮导致其他链接无法正常工作

java - pom.xml 如何解决接缝预订示例项目中的错误,Eclipse Maven

database - Seam 实体不持久化数据库