在JSF应用程序中通过反射读取托管bean的内容 [英] Reading contents of a managed bean with reflection in a JSF application

查看:175
本文介绍了在JSF应用程序中通过反射读取托管bean的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以自动生成的方式打印出支持bean的内容.因此,所有内容都显示在JSP上.这有可能吗?

I want to print out the contents of a backing bean in an auto-generated way. So all the contents appear on a JSP. Is this possible anyhow?

预先感谢, 丹尼尔

推荐答案

一种方法是使用 WEB-INF/tld/beans.tld:

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>

实施:

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

此标记库随后导入到JSP头中:

This tag library is then imported in the JSP header:

<?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">

使用以下功能对dataTable进行采样:

Sample dataTable using the function:

<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>

有关如何操作的信息,请参见 JavaBean规范.提供本地化的属性名称等.

See the JavaBean spec for info on how to provide localized property names, etc.

这篇关于在JSF应用程序中通过反射读取托管bean的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆