2009-05-06 5 views

Répondre

1

Une façon de le faire serait d'utiliser le JavaBean API et custom tag function.

WEB-INF/tld/beans.tld:

<?xml version="1.0" encoding="UTF-8" ?> 

<taglib xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 
    version="2.1"> 
    <description>Bean inspector.</description> 
    <display-name>Bean inspector utils</display-name> 
    <tlib-version>1.2</tlib-version> 
    <short-name>beans</short-name> 
    <uri>http://acme.demo</uri> 
    <function> 
     <name>inspect</name> 
     <function-class>props.Inspector</function-class> 
     <function-signature> 
      java.util.List inspect(java.lang.Object) 
     </function-signature> 
    </function> 
</taglib> 

Mise en œuvre:

public class Inspector { 

    public static List<Map.Entry<String, Object>> inspect(
     Object bean) { 
    Map<String, Object> props = new LinkedHashMap<String, Object>(); 

    try { 
     BeanInfo info = Introspector.getBeanInfo(bean 
      .getClass(), Object.class); 
     for (PropertyDescriptor propertyDesc : info 
      .getPropertyDescriptors()) { 
     String name = propertyDesc.getDisplayName(); 
     Method reader = propertyDesc.getReadMethod(); 
     Object value = reader.invoke(bean); 
     props.put(name, value == null ? "" : value); 
     } 
    } catch (IntrospectionException e) { 
     throw new RuntimeException(e); 
    } catch (InvocationTargetException e) { 
     throw new RuntimeException(e); 
    } catch (IllegalAccessException e) { 
     throw new RuntimeException(e); 
    } 

    return new ArrayList<Map.Entry<String, Object>>(props 
     .entrySet()); 
    } 
} 

Cette bibliothèque de balises est ensuite importé dans l'en-tête JSP:

<?xml version="1.0" encoding="UTF-8" ?> 
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" xmlns:beans="http://acme.demo"> 

échantillon dataTable en utilisant la fonction:

<h:dataTable border="1" value="#{beans:inspect(demoPropsBean)}" var="entry"> 
    <h:column id="column1"> 
     <f:facet name="header"> 
      <h:outputText value="property" /> 
     </f:facet> 
     <h:outputText value="#{entry.key}" /> 
    </h:column> 
    <h:column id="column2"> 
     <f:facet name="header"> 
      <h:outputText value="value" /> 
     </f:facet> 
     <h:outputText value="#{entry.value}" /> 
    </h:column> 
</h:dataTable> 

Voir la JavaBean spec pour plus d'informations sur la façon de fournir les noms de propriété localisée, etc.

+0

Merci pour la réponse! J'ai essayé ceci, et j'obtiens l'erreur: javax.servlet.ServletException: Problèmes appelant la fonction «beans: inspect» avec une cause première de java.lang.NullPointerException. Probablement je passe un mauvais paramètre. .. Est-ce faux? –

+0

Je n'ai jamais utilisé jsp: useBean avec JSF, donc je ne suis pas sûr du problème. Il vaudrait mieux définir le bean géré dans votre fichier WEB-INF/faces-config.xml: http://java.sun.com/javaee/5/docs/tutorial/doc/bnapl.html#bnaqc – McDowell

+0

sans l'utilisationBean: \ Btw c'est aussi défini dans faces-config, la raison pour laquelle j'utilise jsp: useBean est que je peux le passer en paramètre comme ceci: <% method (myBean); %>, car ainsi le bean est "visible" depuis le code Java de jsp. Je suis confus comment d'autre pourrais-je faire cela. Merci, vous êtes vraiment utile, Daniel –

Questions connexes