如何在JSF中使用UIData的java.util.Set。具体h:datatable? [英] How do I use a java.util.Set with UIData in JSF. Specifically h:datatable?

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

问题描述

我知道UIData不支持这一点,我明白为什么,但是对于使用JPA和JSF的人来说,这应该是一个常见的问题,因为在映射M2M O2M关系时,Sets是高级集合。



我知道我需要创建一些结构,以便在需要时将我的属性转换为列表,但是在广泛的搜索这个问题之后,我可以找到是为什么它不起作用的原因,只有一点点解决方案



我相信答案是创建一个ELResolver来处理这个问题,但是它们的结构以及它们的工作方式对我来说是令人困惑的,我不明白为什么我会当这是一个常见的问题时,需要写一个这样的人,当然有人写了一个ELResolver来做这个吗?



我发现这篇文章是关于这个问题的,但是我不能复制它,因为较新的JSF似乎不允许:



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



而这个:



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



由于它是预先ELResolver,其中包含已弃用的代码。但是我只是找不到如何实现ELResolver来实现。有人可以指出一些代码可以工作还是至少有类似的代码,这将帮助我了解如何使用ELResolver?

解决方案

DataModel中设置(实际上是整个 Collection 接口) / code>是可用。 nl / jsf-22 /rel =nofollow> JSF 2.2 。它目前已经可以作为快照使用,以便您可以开始开发。






更新:根据评论,似乎与Spring Web Flow一起无缝工作。事实证明,它不是JSF 2.2兼容(最初也不兼容JSF 2.1)。那么一个自定义的 ELResolver 应该是你最好的选择。



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

  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 )){
context.setPropertyResolved(true);
返回新的ArrayList< Object>((Set<?>)base);
}

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

}

如果您注册如下, faces-config.xml

  ;应用> 
< el-resolver> com.example.SetToListELResolver< / el-resolver>
< / application>

那么你可以在#{ bean.set.setToList} 其中 .setToList 是一个特殊属性,将触发转换:

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

它将有效地结束于一个虚构的



< pre class =lang-html prettyprint-override> < h:dataTable value =#{new ArrayList(bean.set)}...>


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.

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?

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

And this:

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

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?

解决方案

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.


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.

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);
    }

}

If you register it as follows in faces-config.xml

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

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天全站免登陆