如何在 JSF 中使用带有 UIData 的 java.util.Set.特别是 h:datatable? [英] How do I use a java.util.Set with UIData in JSF. Specifically h:datatable?

查看:14
本文介绍了如何在 JSF 中使用带有 UIData 的 java.util.Set.特别是 h:datatable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这在 UIData 中不受支持,我也理解为什么,但这对于使用 JPA 和 JSF 的人来说应该是一个常见问题,因为在映射 M2M O2M 关系时,集合是最好的集合.

I know this is not supported in UIData and I understand why, but this should be a common issue for people using JPA and JSF since Sets are the superior collection when mapping M2M O2M relationships.

我知道我需要创建一些结构来在需要时将我的属性转换为列表,但是在广泛搜索这个问题之后,我只能找到它不起作用的原因,并且只有轻微的解决方案提示.

I know I need to create some structure to convert my property to a list when it's needed but after extensively googling this problem all I can find is reasons why it doesn't work, and only slight hints of a solution.

我相信答案是创建一个 ELResolver 来处理这个问题,但它们的结构和它们的工作方式让我感到困惑,我不明白为什么当这是一个常见问题时我需要成为一个写这个的人,肯定有人写了一个 ELResolver 来做到这一点?

I believe the answer is to create an ELResolver to handle this but the structure of them and how they work is baffling to me and I don't see why I would need to be the one writing this when it's a common issue, Surely someone has written an ELResolver to do this?

我找到了关于该主题的这篇文章,但我无法复制它,因为较新的 JSF 似乎不允许这样做:

I have found this article on the subject but I can't replicate it because the newer JSF doesn't seem to allow it:

http://techblog.bozho.net/?p=28&cpage=1#comment-13700

还有这个:

http://www.jroller.com/mert/entry/settolistpropresolver_for_jsf_el

其中充满了已弃用的代码,因为它是 ELResolver 之前的版本.但我就是找不到如何实现 ELResolver 来做到这一点.有人能给我指出一些有效的代码或至少类似的东西来帮助我了解如何使用 ELResolver 吗?

Which is full of deprecated code because it's pre ELResolver. But I just can't find how to implement an ELResolver to do it. Can someone point me to some code that works or at least something similar that will help me fathom out how to use an ELResolver?

推荐答案

更简单的事情,支持 DataModel 中的 Set(实际上是整个 Collection 接口)可用jsf-22/" rel="noreferrer">JSF 2.2.它目前已作为 snapshot 提供,这样您就可以开始开发了.它将在第二季度左右发布.

Something easier, support for Set (actually, the entire Collection interface) in DataModel is available in JSF 2.2. It's currently already available as snapshot so that you can just start developing. It will be released around Q2.

更新:根据评论,它似乎无法与 Spring Web Flow 无缝协作.事实证明它与 JSF 2.2 不兼容(最初也不兼容 JSF 2.1).好吧,自定义 ELResolver 应该是您最好的选择.

Update: as per the comments, it didn't seem to quite work seamlessly together with Spring Web Flow. It turns out that it's not JSF 2.2 compatible (and initially also not JSF 2.1 compatible). Well, a custom ELResolver should be your best bet.

最简单的就是让它扩展ListELResolver如下:

Easiest is to let it extend ListELResolver as follows:

public class SetToListELResolver extends ListELResolver {

    public static final String KEY_PROPERTY = "setToList";

    @Override
    public Object getValue(ELContext context, Object base, Object property) {
        if (base instanceof Set<?> && KEY_PROPERTY.equals(property)) {
            context.setPropertyResolved(true);
            return new ArrayList<Object>((Set<?>) base);
        }

        return super.getValue(context, base, property);
    }

}

如果在faces-config.xml

<application>
    <el-resolver>com.example.SetToListELResolver</el-resolver>
</application>

然后你就可以在 #{bean.set.setToList} 的语法中使用它,其中 .setToList 是一个特殊的属性,它会触发转换:

then you'll be able to use it in the syntax of #{bean.set.setToList} wherein the .setToList is a special property which will trigger the conversion:

<h:dataTable value="#{bean.set.setToList}" ...>

它实际上最终会变成一个虚构的

It will effectively end up in a fictive

<h:dataTable value="#{new ArrayList(bean.set)}" ...>

这篇关于如何在 JSF 中使用带有 UIData 的 java.util.Set.特别是 h:datatable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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