2012-05-23 8 views
6

Ho seguito due semplici POJO:Nessun accesso alla proprietà nidificato in bean gestito all'interno di p: colonne

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

class Address { 
    String city; 
    //also getter/setter for this attribute 
} 

E un backing bean:

@ManagedBean 
@RequestScoped 
class PersonController { 

    private List persons; 
    private List<String> columns = Arrays.toList("name", "address.city"); 
    //of course getter/setter 
} 

Ora voglio creare un DataTable.

<p:dataTable var="person" value="#{personController.persons}" columnIndexVar="index"> 
    <p:columns var="column" value="#{personController.columns}"> 
     <h:outputText value="#{person[column]}"/> 
    <p:columms> 
</p:dataTable> 

Quando eseguo questo ottengo un ServletException:

La persona di classe non ha la proprietà 'address.city'.

Ma se una prova per accedere alla città di proprietà come questa all'interno di P: colonne:

<h:outputText value="#{person.address.city}"/> 

tutto bene.

Perché non riesco ad accedere a una proprietà nidificata come quella #{person['address.city']}? E come posso accedervi entro p:columns?

+0

Il tag '' non esiste. Non state generando eccessivamente il tag '' PrimeFaces? – BalusC

+0

Sì. Scusa, lo cambierò. –

risposta

8

Le proprietà dei bean nidificati in un'espressione di stringa di notazione brace come #{person['address.city']} non sono supportate per impostazione predefinita. Fondamentalmente hai bisogno di un #{person['address']['city']}.

Hai bisogno di una personalizzata ELResolver qui. Il modo più semplice è estendere l'esistente BeanELResolver.

Ecco un esempio kickoff:

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

} 

Per farlo funzionare, registrarlo come segue in faces-config.xml:

<application> 
    <el-resolver>com.example.ExtendedBeanELResolver</el-resolver> 
</application> 
+0

Grazie per la tua pronta risposta. Lo proverò! –

+0

Funziona bene. Grazie molto. –

+0

Prego. – BalusC

1

Oltre a @BalusC risposta che ho dovuto aggiungere un assegno di PrimeResourceHandler . Altrimenti tutte le risoluzioni di # {risorsa ...} come # {resource ['primefaces: spacer/dot_clear.gif']} all'interno di primefaces.css non sono riuscite e il flusso di output del file CSS analizzato viene danneggiato.

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