No access to nested property in managed bean within p:columns

后端 未结 2 1959
庸人自扰
庸人自扰 2021-01-05 05:21

I have following two simple POJOs:

class Person {
   String name
   Address address;
   //and of course the getter/setter for the attributes
}

class Address         


        
相关标签:
2条回答
  • 2021-01-05 06:04

    In addition to @BalusC answer I had to add a check for PrimeResourceHandler. Otherwise all resolvements of #{resource...} like #{resource['primefaces:spacer/dot_clear.gif']} inside the primefaces.css failed and the output stream of the parsed CSS file gets corrupted.

    public class ExtendedBeanELResolver extends BeanELResolver {
    
        private static final String PRIMEFACES_RESOURCE_PREFIX = "primefaces:";
    
        @Override
        public Object getValue(ELContext context, Object base, Object property) throws NullPointerException,
                PropertyNotFoundException, ELException {
            if (property == null || base == null || base instanceof ResourceBundle || base instanceof Map
                    || base instanceof Collection || base instanceof PrimeResourceHandler) {
                return null;
            }
    
            String propertyString = property.toString();
    
            if (!propertyString.startsWith(PRIMEFACES_RESOURCE_PREFIX) && propertyString.contains(".")) {
                Object value = base;
    
                for (String propertyPart : propertyString.split("\\.")) {
                    value = super.getValue(context, value, propertyPart);
                }
    
                return value;
            } else {
                return super.getValue(context, base, property);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-05 06:09

    Nested bean properties in a brace notation string expression like #{person['address.city']} is by default not supported. You basically need a #{person['address']['city']}.

    You need a custom ELResolver here. Easiest is to extend the existing BeanELResolver.

    Here's a kickoff example:

    public class ExtendedBeanELResolver extends BeanELResolver {
    
        @Override
        public Object getValue(ELContext context, Object base, Object property)
            throws NullPointerException, PropertyNotFoundException, ELException
        {
            if (property == null || base == null || base instanceof ResourceBundle || base instanceof Map || base instanceof Collection) {
                return null;
            }
    
            String propertyString = property.toString();
    
            if (propertyString.contains(".")) {
                Object value = base;
    
                for (String propertyPart : propertyString.split("\\.")) {
                    value = super.getValue(context, value, propertyPart);
                }
    
                return value;
            }
            else {
                return super.getValue(context, base, property);
            }
        }
    
    }
    

    To get it to run, register it as follows in faces-config.xml:

    <application>
        <el-resolver>com.example.ExtendedBeanELResolver</el-resolver>
    </application>
    
    0 讨论(0)
提交回复
热议问题