我现在有10张数字的图片 分别对应着论坛显示 0楼 1楼 2楼 .....9楼
根据数据库分页显示,假如我有 totalPage 条数据 当显示50楼的时候 图片应该是 5.gif + 0.gif 
求助前辈们我应该如何实现这个功能.是不是应该将我获取数据的总条数int转换成String,然后分别获取他们的位数显示出来想知道具体应该怎么做 怎么写 先谢谢了!!

解决方案 »

  1.   

    我的思路是把楼层存成 char 数组,再在页面上显示,下面是参考代码:hello.jsp
    <%@ page contentType="text/html; charset=utf-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>first Test</title>
      </head>
      <style type="text/css">
      table.t {
        margin-bottom: 20px;
        border-left: 1px solid #000000;
        border-top: 1px solid #000000;
      }
      table.t th {
        width: 200px;
        height: 30px;
        text-align: left;
      }
      
      table.t td, table.t th {
        border-right: 1px solid #000000;
        border-bottom: 1px solid #000000;
      }
      </style>
      <body>
        <%-- 楼层迭代 --%>
        <c:forEach items="${replies}" var="reply">
        <table class="t" cellspacing="0">
          <thead>
          <tr>      
            <th>
            <%-- 楼层图片迭代 --%>
            <c:forEach items="${reply.floor}" var="f"><img src="img/${f}.gif"></c:forEach>
             楼
             &nbsp;&nbsp;
            ${reply.username}</th>
          </tr>
          </thead>
          <tbody>
          <tr>      
           <td colspan="2">${reply.content}</td>
          </tr>
          </tbody>
        </table>
        </c:forEach>
      </body>
    </html>HelloServlet.java
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import com.bean.ReplyBean;public class HelloServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            doPost(request, response);
        }
        
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            List<ReplyBean> replies = new ArrayList<ReplyBean>();
            for(int i = 0; i < 50; i++) {
                ReplyBean reply = new ReplyBean();
                reply.setUsername("Name " + i);
                reply.setContent("hello.");
                // 将楼层转为 char 数组
                 reply.setFloor((i+"").toCharArray());
                replies.add(reply);
            }
            request.setAttribute("replies", replies);
            getServletContext().getRequestDispatcher("/hello.jsp").forward(request, response);        
        }
    }ReplyBean.java
    public class ReplyBean {
        private char[] floor;
        private String username;
        private String content;
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public char[] getFloor() {
            return floor;
        }
        public void setFloor(char[] floor) {
            this.floor = floor;
        }
    }
      

  2.   

    如果楼层在页面上处理的话,我只能想到在 JSP 中调用 Java 程序。<%@ page contentType="text/html; charset=utf-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>first Test</title>
      </head>
      <style type="text/css">
      table.t {
        margin-bottom: 20px;
        border-left: 1px solid #000000;
        border-top: 1px solid #000000;
      }
      table.t th {
        width: 200px;
        height: 30px;
        text-align: left;
      }
      
      table.t td, table.t th {
        border-right: 1px solid #000000;
        border-bottom: 1px solid #000000;
      }
      </style>
      <body>
        <c:forEach items="${replies}" var="reply" varStatus="status">
        <table class="t" cellspacing="0">
          <thead>
          <tr>      
            <th>
            <c:set var="floor" value="${status.count-1}"/>
            <%
              String str = ((Long)(pageContext.getAttribute("floor"))).toString();
              char[] c = str.toCharArray();
              pageContext.setAttribute("floor", c);
            %>
            <c:forEach items="${floor}" var="f"><img src="img/${f}.gif"></c:forEach>
            楼
             &nbsp;&nbsp;
            ${reply.username}</th>
          </tr>
          </thead>
          <tbody>
          <tr>      
           <td colspan="2">${reply.content}</td>
          </tr>
          </tbody>
        </table>
        </c:forEach>
      </body>
    </html>