zoukankan      html  css  js  c++  java
  • Jsp页面遍历后台传过来的List

    Jsp页面遍历后台传过来的List

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

    首先引用jstl标签

    [html] view plaincopy
     
    1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  


    然后使用foreach标签

    [html] view plaincopy
     
    1. <c:forEach items="${list}" var="user" varStatus="vs">  
    2.         <tr>  
    3.              <td align = "center">${user.PId}</td>  
    4.              <td align = "center">${user.PLoginname}</td>  
    5.              <td align = "center">${user.PUserName}</td>  
    6.              <td align = "center">${user.PEmail}</td>  
    7.              <td align = "center"><html:department pdeptid="${user.PDeptid}"></html:department></td<!-- 自定义标签 -->  
    8.          </tr>  
    9. </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中

    [html] view plaincopy
     
    1. request.getSession().setAttribute(<span style="color:#ff0000;">Data.ALLNEWSLIST</span>, list);  

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

    [html] view plaincopy
     
    1. public static final String ALLNEWSLIST="ALLNEWSLIST";  



     

    jsp代码:(注意标红的代码不要忘记引入对应的类)

    [html] view plaincopy
     
    1. <%@ page language="java" import="java.util.*,<span style="color:#ff0000;">com.xiami.onlineshop.common.*,com.xiami.onlineshop.data.*" </span>pageEncoding="GBK"%>  
    2.   
    3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    4. <html>  
    5.   <head>  
    6.      
    7.   </head>  
    8.     
    9.   <body>  
    10.     <%     
    11.         String ntype=null;  
    12.         int pagenum=1;  
    13.         if(request.getParameter("pagecurrent")!=null){   
    14.         pagenum=Integer.parseInt(request.getParameter("pagecurrent"));   
    15.     }   
    16.         List list=null;  
    17.         if(session.getAttribute(Data.ALLNEWSLIST)!=null){  
    18.             list = (List)session.getAttribute(Data.ALLNEWSLIST);  
    19.             int l = list.size();  
    20.             %>  
    21.         <table border=width="100%">  
    22.         <tr bgcolor="#8E8E8E">>>首页>商城动态</tr>  
    23.         </table>  
    24.               
    25.                   
    26.             <br><br>    </font></center>  
    27.             <font color=#272727>第<%=pagenum %>页<Br><br></font>  
    28.                 <table>  
    29.             <%  
    30.             for(int i=0;i<l;i++){  
    31.                 News news =(News)list.get(i);  
    32.                 ntype=news.getNtype();  
    33.                   
    34.                 %>  
    35.                     <tr bgcolor="#93FF93">  
    36.                         <td bgcolor="#6C6C6C"><%=news.getNid() %></td>  
    37.                         <td bgcolor="#ADADAD"><href="servlet/ShowDetailNews?nid=<%=news.getNid() %>"><%=news.getNtitle() %></a></td>  
    38.                           
    39.                     </tr>  
    40.                 <%  
    41.             }  
    42.             %>  
    43.               
    44.                 </table><br>  
    45.                 <href="servlet/ShowAllNews?page=1&type=<%=ntype %>">首页</a>   
    46.                 <href="servlet/ShowAllNews?page=<%=pagenum-1 %>&type=<%=ntype %>">上一页</a>   
    47.                 <href="servlet/ShowAllNews?page=<%=pagenum+1 %>&type=<%=ntype %>">下一页</a>   
    48.                 <href="servlet/ShowNewsEndPage?type=<%=ntype %>">尾页</a>  
    49.                   
    50.             <%  
    51.         }  
    52.      %>  
    53.   </body>  
    54. </html>  

    3:使用Struts标签

    [html] view plaincopy
     
    1. <%@ taglib prefix="s" uri="/struts-tags"%>  
    [html] view plaincopy
     
      1. <s:iterator value="#request.userList" status="stat" id="sd">  
      2.                             <tr align="center">  
      3.                                 <td>  
      4.                                     <s:property value="#sd[6]" />  
      5.                                 </td>  
      6.                                 <td>  
      7.                                     <s:property value="#sd[1]" />                             <span style="WHITE-SPACE: pre">                         </span></td>  
      8.                                 <td>  
      9.                                     <s:property value="#sd[2]"></s:property>  
      10.                                 </td>  
      11.                                 <td>  
      12.                                     <s:property value="#sd[4]"></s:property>  
      13.                                 </td>  
      14.                                 <td>  
      15.                                     <s:property value="#sd[5]"></s:property>  
      16.                                 </td>  
      17.                             </tr>  
      18. </s:iterator>  
      19. 转自:http://blog.csdn.net/kalision/article/details/8512700
  • 相关阅读:
    Linux中杀不死的进程
    SQL语句 不支持日语 韩语 泰国语等的解决办法
    很长时间没写,重新开始每天进步一点点
    c#使用access数据库时 模糊查询 like 通配符的写法
    每天进步一点点之找工作的心路历程
    每天进步一点点之工作前三天
    Ajax实现原理
    java动态代理的原理
    css定位机制总结
    迷宫,较为高效的C++代码 BFS实现
  • 原文地址:https://www.cnblogs.com/harry335/p/4898502.html
Copyright © 2011-2022 走看看