【问题标题】:Access composite component attributes in JSF in the backing Java class在支持 Java 类中访问 JSF 中的复合组件属性
【发布时间】:2012-10-09 01:33:03
【问题描述】:

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

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...

有没有办法让我可以像第一个 sn-p 一样传递字符串?

【问题讨论】:

  • 您是否有用于委托给StateHelper#put() 的属性的setter 方法?

标签: java jsf jsf-2 el composite-component


【解决方案1】:

我找到了一个解决方法,但我认为我不应该这样做......:

@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-03-10
    • 2011-10-16
    • 2012-06-19
    • 2011-07-24
    • 2012-04-29
    • 2012-09-03
    • 2013-08-22
    • 2011-07-23
    • 2011-01-10
    相关资源
    最近更新 更多