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

前端 未结 1 1635
渐次进展
渐次进展 2020-12-11 10:12

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 mappi

相关标签:
1条回答
  • 2020-12-11 10:41

    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)}" ...>
    
    0 讨论(0)
提交回复
热议问题