JavaWeb(十七)

EL表达式:

EL表达式中的常量包括布尔常量、整型常量、浮点数常量、字符串常量和NULL常量。

布尔常量,用于区分事务的正反面,即true或false。

整型常量,与Java中定义的整型常量相同。

浮点数常量,与Java中定义的浮点数常数相同

字符串常量,是用单引号或双引号引起来的一连串字符。

NULL常量,用于表示引用的对象为空。页面什么都不会输出。


JSTL标签:

标签(Tag)
    · 标签是一种XML元素,通过标签可以使ISP网页变得简洁并且易于维护,还可以方便地实现同        一个JSP文件支持多种语言版本。由于标签是XML元素,所以它的名称和属性都是大小写敏感        的

标签库(Tag library)

    . 由一系列功能相似、逻辑上互相联系的标签构成的集合称为标签库

标签库描述文件(Tag Library Descriptor)
    · 标签库描述文件是一个XML文件,这个文件提供了标签库中类和SP中对标签引用的映射关              系。它是一个配置文件,和web.xml是类似的,一般以.tld作为文件的扩展名。

标签处理类(Tag Handle Class)
    . 标签处理类是严个Java类,这个类继承了TagSupport或著扩展SimpleTag接口道过这个类可以实        现自定义JSP标签的具体功能

分页:

dao层:

/**
     *分页查询所有管理员
     * @param pageSize 每页的条数
     * @param pageCode 页数
     * @return 当前页的数据
     */
    List<Store> findStoreAll(int pageSize,int pageCode);

    /**
     *查询所有的数据的个数
     * @return 条数/个数
     */
    int getCount();

 daoImpl层:

 @Override
    public List<Store> findStoreAll(int pageSize, int pageCode) {
        List<Store> storeList=new ArrayList<Store>();
        //连接数据库
        Connection conn = DBHelper.getconn();
        //定义SQL语句
        String sql="select s.*,u.username from store s inner join userinfo u on s.managerid=u.id  limit ?,?";
        //预编译SQL语句
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            //得到prepareStatement对象
            ps=conn.prepareStatement(sql);
            ps.setInt(1,(pageCode-1)*pageSize);
            ps.setInt(2,pageSize);
            //得到结果集
            rs=ps.executeQuery();
            //从结果集里读取数据
            while (rs.next()){
                Store store = new Store(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getString(5));
                /*store.setId(rs.getInt("id"));
                store.setName(rs.getString("sname"));
                store.setAddress(rs.getString("address"));
                store.setManager(rs.getString("username"));*/
                storeList.add(store);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            DBHelper.colse(conn,ps,rs);
        }
        return storeList;
    }

    @Override
    public int getCount() {
        int count=0;
        //连接数据库
        Connection conn = DBHelper.getconn();
        //定义SQL语句
        String sql="select count(*)from store";
        //预编译SQL语句
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            if (rs.next()){
                count=rs.getInt(1);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            DBHelper.colse(conn,ps,rs);
        }
        return count;
    }

Servlet层:

@WebServlet("/store")
public class StoreServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/hml;charset=utf-8");
        StoreDao storeDao=new StoreDaoImpl();
        //1.定义:每页的个数,页数,总页数,总条数
        int pageSize=3;int pafeCode=0;int totalCode=0;int totalCount=0;
        //2.得到总条数
        totalCount=storeDao.getCount();
        //3.计算总页数
        if(totalCount%pageSize==0){
            totalCode=totalCount/pageSize;
        }else{
            totalCode=totalCount/pageSize+1;
        }
        //4.判断是否是第一次访问
        if(req.getParameter("pafeCode")==null|| req.getParameter("pafeCode")==""){
            pafeCode=1;
        }else{
            //不是第一访问
            if(Integer.parseInt(req.getParameter("pafeCode"))>totalCode){
                //超过总页数,一直在尾页
                pafeCode=totalCode;
            }else{
                //没有超过总页数,当前的页数
                pafeCode=Integer.parseInt(req.getParameter("pafeCode"));
            }
        }
        req.getSession().setAttribute("pafeCode",pafeCode);
        req.getSession().setAttribute("totalCode",totalCode);
        req.getSession().setAttribute("totalCount",totalCount);
        List<Store> storeList=storeDao.findStoreAll(pageSize,(Integer)req.getSession().getAttribute("pafeCode"));
        if(storeList!=null){
            req.getSession().setAttribute("storeList",storeList);
            resp.sendRedirect("index.jsp");
        }else{
            System.out.println("查询失败");
            resp.sendRedirect("login.jsp");
        }
    }
}

JSP页面:

<table align="ccenter" >
      <tr>
        <td>名称</td>
        <td>地址</td>
        <td>管理员</td>
        <td>操作</td>
      </tr>
      <tr>
       <c:forEach items="${storeList }" var="s">
        <td>${s.name}</td>
        <td>${s.address}</td>
        <td>${s.manager}</td>
        <td><a href="storeById?id=${s.id}"><input type="button" value="修改"></a><a href="deleteStore?id=${s.id}"><input type="button" value="删除"></a></td>
      </tr>
        </c:forEach>
        <tr>
            <%--首页--%>
            <td colspan="4"><a href="store?pafeCode=1"><input type="button" value="首页"></a>
                <%--判断是否超过首页--%>
                <c:if test="${pafeCode==1}" var="flag"></c:if>
                <%--超过首页--%>
                <c:if test="${flag}">
                    <a href="store?pafeCode=1"><input type="button" value="上一页"></a>
                </c:if>
                <%--没有超过首页--%>
                <c:if test="${!flag}">
                    <a href="store?pafeCode=${pafeCode-1}"><input type="button" value="上一页"></a>
                </c:if>
                <%--当前页数/总页数--%>
                <input type="button" value="${pafeCode}/${totalCode}">
                <%--下一页--%>
                <a href="store?pafeCode=${pafeCode+1}"><input type="button" value="下一页"></a>
                <%--尾页--%>
                <a href="store?pafeCode=${totalCode}"><input type="button" value="尾页"></a>
            </td>
        </tr>
    </table>
    <a href="user"><input type="button" value="添加新仓库"></a>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值