简体   繁体   中英

How do I use a java.util.Set with UIData in JSF. Specifically h:datatable?

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)}" ...>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM