Layui和Ajax实现内容的替换

Layui和Ajax实现内容的替换,第1张

Layui和Ajax实现内容的替换
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title
    





    
    
        ID
        楼层
        备注
        宿管姓名
         *** 作
    
    





 以json的形式把数据传送到展示页面进行展示

@WebServlet(name = "BuildingServlet", value = "/buildingServlet")
public class BuildingServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String limit=request.getParameter("limit");
    String page=request.getParameter("page");
    BuildingDao bui=new buildingImDao();
    int count=bui.BuildCount();
    List list=bui.AllBuilding(Integer.parseInt(limit),Integer.parseInt(page));
    JSonObject json=new JSonObject();
    json.put("msg","");
    json.put("count",count);
    json.put("code",0);
    json.put("data",JSONObject.toJSON(list));
        System.out.println(json.toString());
        response.getWriter().write(json.toString());
    }

 

 把查询出的内容存储到session作用域中

@WebServlet(name = "UpdBuiServlet", value = "/UpdBui")
public class UpdBuiServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String id=request.getParameter("id");
    request.getSession().setAttribute("id",id);
        TeacherDao te=new teacherImDao();
        List teaList=te.teaAll();
        request.getSession().setAttribute("teaList",teaList);
        response.sendRedirect("UpdBui.jsp");
    }

 

 把内容使用JSTL遍历循环展示到下拉菜单中

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>


    Title
  






 

 修改为选择的内容

@WebServlet(name = "UpdBuiTeacherNameServlet", value = "/updBuiTeacherName")
public class UpdBuiTeacherNameServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String Buiid=request.getParameter("id");
        String teacherid=request.getParameter("teacherid");
        building bu=new building(Integer.parseInt(Buiid),Integer.parseInt(teacherid));
        BuildingDao bui=new buildingImDao();
        int count=bui.UpdTacherName(bu);
        if (count>0){
            response.getWriter().write("1");
        }
    }
}

 

 方法的重写

public class buildingImDao implements BuildingDao {
    @Override
    public List AllBuilding() {
        Connection con= DBHelper.getConn();
        List list=new ArrayList<>();
        building bu=null;
        String sql="select b.Building_ID,b.Building_Name,b.Building_Introduction,t.Teacher_Name from building b LEFT JOIN  teacher t on b.teacherId=t.Teacher_ID";
        try {
            PreparedStatement ps=con.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            while (rs.next()){
                bu=new building(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4));
                list.add(bu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public int BuildCount() {
        Connection con= DBHelper.getConn();
        int count=0;
        String sql="select count(*) from building";
        try {
            PreparedStatement ps=con.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            if (rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return count;
    }

    @Override
    public int UpdTacherName(building b) {
        Connection con= DBHelper.getConn();
        int count=0;
        String sql="update building set teacherId=? where Building_ID=?";
        try {
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setInt(1,b.getTeacherId());
            ps.setInt(2,b.getBuilding_ID());
            count=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return count;
    }

    @Override
    public List AllBuilding(int pageSize, int pageCode) {
        Connection con= DBHelper.getConn();
        List list=new ArrayList<>();
        building bu=null;
        String sql="select b.Building_ID,b.Building_Name,b.Building_Introduction,t.Teacher_Name from building b LEFT JOIN  teacher t on b.teacherId=t.Teacher_ID limit ?,?";
        try {
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setInt(1,(pageCode-1)*pageSize);
            ps.setInt(2,pageSize);
            ResultSet rs=ps.executeQuery();
            while (rs.next()){
                bu=new building(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4));
                list.add(bu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
}

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

原文地址: http://outofmemory.cn/zaji/5703799.html

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

发表评论

登录后才能评论

评论列表(0条)

保存