JSTL需要一个逗号分隔的数组来显示JSP上的数据

时间:2012-02-14 20:33:33

标签: java jstl

再次,我。我从bean列表中显示了我的结果,但是当我们对数据库包进行了一些汇总时遇到了问题。正如您所看到的那些被展开的行显示为内存位置,我相信它就是所谓的内存。我被告知我可以使用类似“varstatus”的东西并迭代生成逗号分隔数组以显示实际数据?

这听起来不错吗?

标题很好......我的数据是指

感谢名单

enter image description here

如果图片太小...最后3列用这个填充:“[Ljava.lang.String; @ 11396ec”

BEAN:

public class DetResults
{
private List<String> headings;
private List<Class<?>> types;
private List<Object[]> data;

public DetResults() {}

public List<String> getHeadings() { return this.headings; }
public String getHeading(int which) { return this.headings.get(which); }

public List<Class<?>> getTypes() { return this.types; }
public Class<?> getType(int which) { return this.types.get(which); }

public List<Object[]> getData( ) { return this.data; }
public Object[] getDataAtRow( int row ) { return this.data.get(row); }


public void setHeadings( List<String> val ) { this.headings = val; }
public void setHeadings( String[] val ) { this.headings = Arrays.asList(val); }
public void addHeading( String val ) 
{ 
    if( this.headings == null ) this.headings = new ArrayList<String>();
    this.headings.add(val); 
}

public void setTypes( List<Class<?>> val ) { this.types = val; }
public void setTypes( Class<?> val[] ) { this.types = Arrays.asList(val); }
public void addType( Class<?> val ) 
{
    if( this.types == null ) this.types = new ArrayList<Class<?>>();
    this.types.add(val); 
}


public void setData( List<Object[]> val ) { this.data = val; }

// allow NPE to get thrown
public void setDataAtRow( Object[] val, int row ) { this.data.set(row, val); }

public void appendDataRow( Object[] val ) 
{
    if( data == null ) data = new ArrayList<Object[]>(); 
    this.data.add(val); 
}

public int getColumnCount() { return this.headings!=null?this.headings.size():0; }

}

JSP:

<tr>
        <th>
            <span onclick="toggleDiv('resultSet', 'resultImg')" style="cursor: hand;">Results&nbsp;<img name="resultImg" src="../images/minus.gif" /></span>
        </th>
    </tr>
    <tr>
        <td>
            <div id="resultSet" style="display:block; background-color:#ffffff;">

            <!--Begin Search Results -->
            <c:choose>
            <c:when test="${not empty results.columnCount}">

                <ctl:vertScroll height="300" headerStyleClass="data_table_scroll" bodyStyleClass="data_table_scroll" enabled="${user.scrollTables}">
              <!--  <ctl:sortableTblHdrSetup topTotal="false" href="show.whatif_like_item_search"/> -->
                <table align="center" class="data_table vert_scroll_table" >
                    <tr>
                    <c:forEach var="heading" items="${results.headings}">    
                      <th class="narrow">${heading}</th>
                      </c:forEach> 
                      </tr>
                      <c:forEach var="row" items="${results.data}">   
                        <tr>
                        <c:forEach var="cell" items="${row}">   
                        <td>${cell}</td>
                        </c:forEach> 
                        </tr>
                        </c:forEach> 
                </table>
                </ctl:vertScroll>

            </c:when>
            <c:otherwise>
                <!--  SHOW NOTHING -->
            </c:otherwise>
            </c:choose>

            <!--End Search Results -->



            </div>
        </td>
    </tr>

解答:

<table align="center" class="data_table vert_scroll_table" >
                <tr>
                    <c:forEach var="heading" items="${results.headings}">   
                        <th class="narrow">${heading}</th>
                    </c:forEach>
                </tr>
                <c:forEach var="row" items="${results.data}">
                    <tr>
                        <c:forEach var="cell" items="${row}" varStatus="rowStatus">
                            <td style="width:200px;">
                                <c:choose>
                                    <c:when test="${results.types[rowStatus.index].array}">
                                        <c:forEach var="elem" items="${cell}" varStatus="cellStatus">
                                            ${elem}<c:if test="${!cellStatus.last}">,&nbsp;</c:if>
                                        </c:forEach>
                                    </c:when>
                                    <c:otherwise>
                                            ${cell}
                                    </c:otherwise>
                                </c:choose>
                            </td>
                        </c:forEach>
                    </tr>
                </c:forEach>
                </table>

1 个答案:

答案 0 :(得分:13)

这是基本想法,

您的收藏

List<String> fruits = new ArrayList<String>();
fruits.add("Apple");
fruits.add("Mango");
fruits.add("Grapes");

关于JSP

<c:forEach items="${fruits}" var="fruit" varStatus="status">
         <c:out value="${fruit}"/>
         <c:if test="${!status.last}">,</c:if>
</c:forEach>