0

我以这种方式访问​​我的复合组件的属性:

enum PropertyKeys {comments, currentUserUsername}

@SuppressWarnings("unchecked")
private <T> T getAttribute(PropertyKeys propertyKey){
    return (T) getStateHelper().eval(propertyKey);
}

private <T> void setAttribute(PropertyKeys propertyKey, T value){
    getStateHelper().put(propertyKey, value);
}

private List<Comment> getComments() {
    return getAttribute(PropertyKeys.comments);
}

private String getCurrentUserUsername() {
    return getAttribute(PropertyKeys.currentUserUsername);
}

当我以这种方式传递用户名时,这适用于列表(评论)但不适用于字符串(CurrentUserUsername):

<tr:commentBox comments="#{main.comments}" currentUserUsername="Hans" [...] />

但是当我这样通过时:

<tr:commentBox comments="#{main.comments}" currentUserUsername="#{'Hans'}" [...] />

有用。

我不希望任何将使用我的组件的人都必须将字符串作为 EL...

有没有解决方案,所以我可以像第一个片段一样传递字符串?

4

1 回答 1

0

我找到了一个workarround,但我认为我不应该这样做..:

@SuppressWarnings("unchecked")
private <T> T getAttribute(PropertyKeys propertyKey){
    T result = (T) getStateHelper().eval(propertyKey);

    if (result == null) {
        result = (T) this.getAttributes().get(propertyKey + "");
    }

    return result;
}
于 2012-10-18T16:40:03.450 回答