zoukankan      html  css  js  c++  java
  • struts2中iterator标签处理hql返回的不定格式数据

    查询操作处理端:

    String hql= "select w.Sort_Name , sum(w.Gumack)  from WorkList w group by Sort_Name";

    public List<Object[]> list(String hql) {
      return  factory.getCurrentSession().createQuery(hql).list();
     }

    返回的是ArrayList类型的数据不知道其中的索引属性

    Action中的代码:

    public String reportSort(){
      ActionContext.getContext().put("sortGroup", workListService.list("select w.Sort_Name , sum(w.Gumack)  from WorkList w group by Sort_Name"));
      return "reportSort";
     }

    在jsp中可以有这样几种方式处理:

    1.通过status属性迭代

    <s:iterator value="#request.sortGroup" status="sort">
          <tr>
           <td ><s:property value="#request.sortGroup[#sort.index][0]"/></td>
           <td ><s:property value="#request.sortGroup[#sort.index][1]"/></td>
          </tr>
     </s:iterator>

    其中status 迭代元素的索引,#sort.index是迭代每个sort的索引值,[0]中的数字是每个sort数组的下标

    更简单的可以把#request.sortGroup看成一个二维数组,来取值

    刚在别人那里学到的一种更简洁的方式:

    2.通过id属性来迭代

    <s:iterator value="#request.sortGroup" id="element">

    <tr>
           <td ><s:property value="#element[0]"/></td>
           <td ><s:property value="#element[1]"/></td> 
    </tr>

    </s:iterator>

    其中id为指定集合的迭代元素,因为返回的类型是 ArrayList<String[]>的,所以用#element表示迭代的每个数组,再用下标来取数组中的值

    3.单字段迭代(特殊)

    如果迭代的元素类型只为List类型,如:List<String>,其中只有一个字段值,

    也可用更简洁的方式来取值:

    <s:iterator value="#request.sortGroup" >

    <tr>
                 <td ><s:property /></td> 
    </tr>

    </s:iterator>

    其中sortGroup为List<?>类型的(单字段类型)

  • 相关阅读:
    HttpServletResponse对锂
    搭建java Web项目开发环境(一)
    React Native动画-Animated
    React-Navigation redux集成
    react-navigation 使用详解
    echart
    io-输出流
    react-native-lesson
    集合类-HashSet去重
    kafka不同主题使用同一个group.id的坑
  • 原文地址:https://www.cnblogs.com/qixing/p/3045771.html
Copyright © 2011-2022 走看看