JSTL多行多列顯示

如圖用循環輸出多行多列:

代碼:假設從Servlet中傳入一個集合list:

則在jsp頁面的代碼:如下

<table width="700" border="1" align="center">
    <tr>
      <td colspan="2" align="center" bgcolor="#FFCCCC"><span class="STYLE1">用戶後臺權限設置</span></td>
    </tr>
    <tr>
      <td>添加用戶名:</td>
      <td><label>
        <input type="text" name="textfield" />
      </label></td>
    </tr>
    <tr>
      <td>給予權限:</td>
     
      <td>
      <table border="1">
      <tr>
     
<c:forEach items="${list}" var="p" varStatus="num">
      <td>

      <c:if test="${num.index%4==0&&num.index!=0}">
      <tr> </tr><td>
      </c:if>
        <input type="checkbox" name="checkbox" value="checkbox" />
     <span> ${p.powertype }</span>
      </td>
    </c:forEach>
      </tr>
        </table>
      </td>
    
    </tr>
    <tr>
      <td><label>
        <input type="submit" name="Submit" value="提交" />
      </label></td>
      <td><label>
        <input name="reset" type="submit" id="reset" value="重置" />
      </label></td>
    </tr>
</table>

===================

jstl中的varStatus

和 var 屬性一樣, varStatus 用於創建限定了作用域的變量。不過,由 varStatus 屬性命名的變量並不存儲當前索引值或當前元素,而是賦予 javax.servlet.jsp.jstl.core.LoopTagStatus 類的實例。該類定義了一組特性,它們描述了迭代的當前狀態,下面列出了這些特性:

特性         Getter                      描述
current    getCurrent()          當前這次迭代的(集合中的)項
index       getIndex()               當前這次迭代從 0 開始的迭代索引
count      getCount()             當前這次迭代從 1 開始的迭代計數
first          isFirst()                  用來表明當前這輪迭代是否爲第一次迭代的標誌
last          isLast()                  用來表明當前這輪迭代是否爲最後一次迭代的標誌
begin      getBegin()             begin 屬性值
end         getEnd()                 end 屬性值
step        getStep()                step 屬性值

 

<c:foreach var="showBspSubTop" varstatus="i" begin="1" step="1" items="${ showBspSubTops }"></c:foreach>

==============則struts中也有類似的參數<logic:iterate>

通過length屬性來指定輸出元素的個數。如下面的代碼:
程序代碼<logic:iterate id="show" name="test" length="2" offset="1">
<bean:write name="show"/>
</logic:iterate>
通過length屬性來指定輸出元素的個數。如下面的代碼:程序代碼

<logic:iterate id="show" name="test" length="2" offset="1">
<bean:write name="show"/>
</logic:iterate>

還有一個indexId屬性,它指定一個變量存放當前集合中正被訪問的元素的序號,如:
程序代碼<logic:iterate id="show" name="test" length="2" offset="1" indexId="number">
<bean:write name="number"/>:<bean:write name="show"/>
</logic:iterate>
其顯示結果爲:
1:str2
2:str3

2、對HashMap進行循環遍歷
程序代碼<%
HashMap countries=new HashMap();
countries.put("country1","中國");
countries.put("country2","美國");
countries.put("country3","英國");
countries.put("country4","法國");
countries.put("country5","德國");
pageContext.setAttribute("countries",countries);
%>
<logic:iterate id="country" name="countries">
<bean:write name="country" property="key"/>:
<bean:write name="country" property="value"/>
</logic:iterate>
  在bean:write中通過property的key和value分別獲得HaspMap對象的鍵和值。其顯示結果爲:
country5:德國
country3:英國
country2:美國
country4:法國
country1:中國
  由結果可看出,它並未按添加的順序將其顯示出來。這是因爲HaspMap是無序存放的。

3、嵌套遍歷
程序代碼<%
String[] colors={"red","green","blue"};
String[] countries1={"中國","美國","法國"};
String[] persons={"喬丹","布什","克林頓"};
ArrayList list2=new ArrayList();
list2.add(colors);
list2.add(countries1);
list2.add(persons);
pageContext.setAttribute("list2",list2);
%>
<logic:iterate id="first" name="list2" indexId="numberfirst">
<bean:write name="numberfirst"/>
<logic:iterate id="second" name="first">
<bean:write name="second"/>
</logic:iterate>
<br>
</logic:iterate>
  運行效果:
0 red green blue
1 中國 美國 法國
2 喬丹 布什 克林頓

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章