Doc Holiday
Doc Holiday

Reputation: 10254

JSTL need a comma separated array to dispay data on JSP

its me again. I got my results to display from the bean list, but ran into a issue when we did some rollups on the database package. As you can see the lines that are rolled up on show as memory locations, I believe is what it's called. I was advised that I can use something like "varstatus" and iterate over to make a comma separated array to display the actual data?

Does this sound right?

The headings are fine...its the data Im referring to

Thanx

enter image description here

If the picture is too small...the last 3 columns are popuated with this: "[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>

ANSWER:

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

Upvotes: 1

Views: 7566

Answers (1)

Jigar Joshi
Jigar Joshi

Reputation: 240908

Here is the basic idea,

Your collection

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

On JSP

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

Upvotes: 15

Related Questions