JavaWeb(十七)

JavaWeb(十七),第1张

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层:

    List findStoreAll(int pageSize,int pageCode);

    
    int getCount();

 daoImpl层:

 @Override
    public List findStoreAll(int pageSize, int pageCode) {
        List storeList=new ArrayList();
        //连接数据库
        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));
                
                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 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页面:

      
        名称
        地址
        管理员
         *** 作
      
      
       
        ${s.name}
        ${s.address}
        ${s.manager}
        
      
        
        
            <%--首页--%>
            
                <%--判断是否超过首页--%>
                
                <%--超过首页--%>
                
                    
                
                <%--没有超过首页--%>
                
                    
                
                <%--当前页数/总页数--%>
                
                <%--下一页--%>
                
                <%--尾页--%>
                
            
        
    
    

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5683946.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存