表ly szfnzsnfs中的bg颜色 [英] tables bg colors in alternative ly szfnzsnfs

查看:94
本文介绍了表ly szfnzsnfs中的bg颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

» JSP Standard Tag Library » JSTL Looping and Iteration Actions
JSTL Looping and Iteration Actions

One of the most common tasks you have to deal with in JSP is outputting a sets of data by using Java for and while loop. By doing so, you create a very unreadability JSP page with opening and closing curly brace. In addition, if something wrong happens, it is difficult to detect whether the problem is. Thankfully, JSTL provides you two useful actions for looping and iteration: for general data and for string of tokens.
The <c:forEach> action

The <c:forEach> action is very useful. You can loop over a collection or you can iterate number of times. There are two usages of <c:forEach> action. Let's take a look at the first one which you can use <c:forEach> for loop over a collection.

<c:forEach(var = "var"

        items="collection"

        varStatus="varStatusName">
<%-- processing of each item here --%>
</c:forEach>

The first two attributes are mandatory. You sepcify a collection in items attribute and each of item in collection in the var attribute. The varStatus attribute is optional. the varStatus attribute is an instance of class which implements interface LoopTagStatus. varStatus provides a set of useful properties to work with such as begin, end, current, index, count.... Let's take a look at an example:

<%@page contentType="text/html"

        pageEncoding="UTF-8"

        import="com.jsptutorial.*,java.util.*"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"

          prefix="c" %>

<html>
    <head>
        <title>Looping with c:forEach</title>
    </head>
    <body>
        <%
            ArrayList<Person> list = new ArrayList<Person>();
            Person p1 = new Person("Abel", "William");
            Person p2 = new Person("Sarah", "Palin");
            Person p3 = new Person("David", "Nguyen");
            list.add(p1);
            list.add(p2);
            list.add(p3);
            request.setAttribute("list", list);
        %>

        <table border="1">
            <thead>
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach var="person" items="${requestScope.list}">
                <tr>
                    <td><c:out value="${person.firstName}"  /></td>
                    <td><c:out value="${person.lastName}" /></td>
                </tr>
                 </c:forEach>
            </tbody>
        </table>
    </body>
</html>

In the code example above we create a list which contain three people and put that list object into the request object of the page. Then we loop over the list of people and print out their first name and last name by using <c:forEach> action.

If you want the table to have alternative row background color, you can use the varStatus to do so. Let's take a look at the modified version of the example above.

First we declare background color for odd and even rows by using CSS class.

<style type="text/css">
  .odd{ background-color:#fff}
  .even{background-color:#ccc}
</style>

Then we use varStatus attribute to access the LoopTagStatus object. Based on the current row of the iteration , we determine the odd and even row. Be noted that the current row of the iteration can be accessible via count property of the LoopTagStatus object.

<table border="1">
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
        </tr>
    </thead>
    <tbody>
    <c:forEach var="person"

               items="${requestScope.list}"

               varStatus ="row">

        <c:choose>
            <c:when test="${row.count % 2 == 0}">
                <c:set var="rowStyle" value="odd"  />
            </c:when>
            <c:otherwise>
                <c:set var="rowStyle" value="even"  />
            </c:otherwise>
        </c:choose>

        <tr class="${rowStyle}">
            <td><c:out value="${person.firstName}"  /></td>
            <td><c:out value="${person.lastName}" /></td>
        </tr>
    </c:forEach>
</tbody>
</table>

推荐答案

{requestScope.list}" < tr > < td > < c:out ="
{requestScope.list}"> <tr> <td><c:out value="


{person.firstName }" > < /td > < td > < c:out ="
{person.firstName}" /></td> <td><c:out value="


{person.lastName }" / < /td > < /tr > < /c:forEach > < /tbody > < /table > < /body > < /html > 在上面的代码示例中,我们创建了一个包含三个人的列表,并将该列表对象放入页面的请求对象中.然后,我们遍历人员列表,并使用< c:forEach 打印出他们的名字和姓氏 > 操作. 如果希望表具有其他行背景色,则可以使用varStatus来实现.让我们看一下上面示例的修改版本. 首先,我们使用CSS类为奇数和偶数行声明背景色. < 样式 =" text/css" < /style > 然后,我们使用varStatus属性访问LoopTagStatus对象.根据迭代的当前行,我们确定奇数行和偶数行.请注意,可以通过LoopTagStatus对象的count属性访问迭代的当前行. < =" 1" < thead > < tr > < > 名字< /th > < > 姓氏< /th > < /tr > < /thead > < 正文 > < c:forEach var =" 项目 ="
{person.lastName}" /></td> </tr> </c:forEach> </tbody> </table> </body> </html> In the code example above we create a list which contain three people and put that list object into the request object of the page. Then we loop over the list of people and print out their first name and last name by using <c:forEach> action. If you want the table to have alternative row background color, you can use the varStatus to do so. Let's take a look at the modified version of the example above. First we declare background color for odd and even rows by using CSS class. <style type="text/css"> .odd{ background-color:#fff} .even{background-color:#ccc} </style> Then we use varStatus attribute to access the LoopTagStatus object. Based on the current row of the iteration , we determine the odd and even row. Be noted that the current row of the iteration can be accessible via count property of the LoopTagStatus object. <table border="1"> <thead> <tr> <th>First Name</th> <th>Last Name</th> </tr> </thead> <tbody> <c:forEach var="person" items="


这篇关于表ly szfnzsnfs中的bg颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆