• Jsp遍历后台传过来的List


    1:使用jstl标签 (可以和自定义标签配合使用)

    首先引用jstl标签

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

    然后使用foreach标签

    <c:forEach items="${list}" var="user" varStatus="vs">
            <tr>
                
                 <td>
                    <s:property value="#vs.index+1"/>
                 </td>
                 <td align = "center">${user.PId}</td>
                 <td align = "center">${user.PLoginname}</td>
                 <td align = "center">${user.PUserName}</td>
                 <td align = "center">${user.PEmail}</td>
                 <td align = "center"><html:department pdeptid="${user.PDeptid}"></html:department></td> <!-- 自定义标签 -->
             </tr>
    </c:forEach>

    可以用<c:if test="${not empty list}"></c:if>   和 <c:if test="${not empty list}"></c:if> 来处理是否为空的情况。如果不为空,显示值,为空的话,显示无记录等。

    后台可以把list放到值栈或者放到request.例如:request.setAttribute("list", XXXXlist);

    2:使用jsp内嵌Java代码遍历List (在后台把List放到session中,如果是大数据量,不应使用此方法)

    首先在后台把list放入到session中

    request.getSession().setAttribute(<span style="color:#ff0000;">Data.ALLNEWSLIST</span>, list);

    红色标记的Data.ALLNEWSLIST 为常量 在com.xiami.onlineshop.common包下的Data类中定义

    public static final String ALLNEWSLIST="ALLNEWSLIST";

    jsp代码:

    <%@ page language="java" import="java.util.*,<span style="color:#ff0000;">com.xiami.onlineshop.common.*,com.xiami.onlineshop.data.*" </span>pageEncoding="GBK"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
       
      </head>
      
      <body>
        <%    
            String ntype=null;
            int pagenum=1;
            if(request.getParameter("pagecurrent")!=null){ 
              pagenum=Integer.parseInt(request.getParameter("pagecurrent")); 
          } 
            List list=null;
            if(session.getAttribute(Data.ALLNEWSLIST)!=null){
                list = (List)session.getAttribute(Data.ALLNEWSLIST);
                int l = list.size();
                %>
            <table border=1 width="100%">
             <tr bgcolor="#8E8E8E">>>首页>商城动态</tr>
            </table>
                
                    
                <br><br>    </font></center>
                <font color=#272727>第<%=pagenum %>页<Br><br></font>
                    <table>
                <%
                for(int i=0;i<l;i++){
                    News news =(News)list.get(i);
                    ntype=news.getNtype();
                    
                    %>
                        <tr bgcolor="#93FF93">
                            <td bgcolor="#6C6C6C"><%=news.getNid() %></td>
                            <td bgcolor="#ADADAD"><a href="servlet/ShowDetailNews?nid=<%=news.getNid() %>"><%=news.getNtitle() %></a></td>
                            
                        </tr>
                    <%
                }
                %>
                
                    </table><br>
                    <a href="servlet/ShowAllNews?page=1&type=<%=ntype %>">首页</a> 
                    <a href="servlet/ShowAllNews?page=<%=pagenum-1 %>&type=<%=ntype %>">上一页</a> 
                    <a href="servlet/ShowAllNews?page=<%=pagenum+1 %>&type=<%=ntype %>">下一页</a> 
                    <a href="servlet/ShowNewsEndPage?type=<%=ntype %>">尾页</a>
                    
                <%
            }
         %>
      </body>
    </html>

    3:使用Struts标签

    <%@ taglib prefix="s" uri="/struts-tags"%>
    <s:iterator value="#request.userList" status="stat" id="sd">
                                <tr align="center">
                                    <td>
                                        <s:property value="#sd[6]" />
                                    </td>
                                    <td>
                                        <s:property value="#sd[1]" />                                <span style="WHITE-SPACE: pre">                            </span></td>
                                    <td>
                                        <s:property value="#sd[2]"></s:property>
                                    </td>
                                    <td>
                                        <s:property value="#sd[4]"></s:property>
                                    </td>
                                    <td>
                                        <s:property value="#sd[5]"></s:property>
                                    </td>
                                </tr>
    </s:iterator>
  • 相关阅读:
    android broadcast小结
    初学设计模式【7】单例模式——sington
    android之Service总结
    现代软件开发实现六面体
    牛顿迭代法求根
    实现顺序表的各种基本运算
    公共的分页类,包含jsp页面
    多项式拟合+高斯消元解方程组
    差商表及牛顿插值
    C#.NET 获取拨号连接 宽带连接
  • 原文地址:https://www.cnblogs.com/wdpnodecodes/p/7413070.html
Copyright © 2020-2023  润新知