【问题标题】:showing the Non property of java bean in Display tag在 Display 标记中显示 java bean 的 Non 属性
【发布时间】:2012-12-28 05:56:09
【问题描述】:

假设..我有以下 java bean。

案例1:(学生豆)

Integer id;

String name;

ArrayList<String> subjectNameList;

并且使用上面的case1结构,我可以用这样的方式在显示标签中显示。

<displaytag:table class="displayTable" id="studentList" name="studentist">
<displaytag:column property="id" title="id"/>
<displaytag:column property="name" title="name"/>
<displaytag:column property="subjectNameList" title="subjectNameList"/>
</displaytag:table>

但现在由于变化,学生豆变成了这样。

案例2:(学生豆)

 Integer id;

 String name;

 ArrayList<Integer> subjectIdList;

所以,在显示标签表中,我知道我不能再直接显示主题名称列表,因为那不再是学生 bean 的属性。

我的问题是.. 有没有办法在显示标签中显示主题名称列表,例如 Case1 中的显示标签(可以通过 Action 类获取并传递给每个学生 bean 的显示标签)?因为在 Case2 中,列表更改为 ID(整数)列表。 我想在显示标签的jsp页面中保持相同的外观。

【问题讨论】:

    标签: jsp displaytag struts2


    【解决方案1】:

    您可以扩展 TableDecorator 来处理案例 2,就像处理案例 1。有关 Decorators 的更多信息

    上述案例的 TableDecorator 实现示例:

    public class StudentBeanDecorator extends TableDecorator {
    
        public String subjectNames()
        {
            StudentBean studentBean = (StudentBean)getCurrentRowObject();
            ArrayList<Integer> subjectIdList = studentBean.getSubjectIdList();
    
            // make a service call or as you like
            ArrayList<String> subjectNameList = studentService.getSubjectNameList(subjectIdList);
    
            // format the data as you want; here for sample, just doing comma separated string
            return Arrays.toString(subjectNameList.toArray());;
        }
    }
    

    并将装饰器映射到 display:table 标记

    <displaytag:table class="displayTable" id="studentList" name="studentist" decorator="com.sample.student.StudentBeanDecorator">
        <displaytag:column property="id" title="id"/>
        <displaytag:column property="name" title="name"/>
        <displaytag:column property="subjectNames" title="subjectNames"/>
    </displaytag:table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-29
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 2013-09-03
      • 2010-09-20
      • 2023-03-23
      相关资源
      最近更新 更多