下拉--实战

一、实体类

二、UserDeptDao 接口

三、UserDeptDaoImpl
public class UserDeptDaoImpl extends BaseDao implements UserDeptDao {

  • //查询部门
        @Override
        public List<Dept> findAllDept() {
            List<Dept> list = new ArrayList<>();
            Dept dept =null;
            rs=super.executeQuery("select * from t_dept",null);
            try {
                while (rs.next()){
                    dept=new Dept();
                    dept.setId(rs.getInt("id"));
                    dept.setName(rs.getString("name"));
                    list.add(dept);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                super.closeConn();
            }
            return list;
        }
    //通过下拉获取用户信息方法
        @Override
        public List<User> findByDeptId(Integer deptid) {
            List<User> list = new ArrayList<>();
            StringBuffer sql=new StringBuffer("SELECT u.id as id,u.`name` as name,\n" +
                    "u.pwd as pwd,\n" +
                    "u.deptId as deptid,\n" +
                    "d.`name` as deptName \n" +
                    "FROM t_dept d,t_user u \n" +
                    "where u.deptId=d.id");
            Object[] params = new Object[]{};
            if(deptid!=null && deptid!=-1){
                sql.append(" and u.deptId=?");
                params=new Object[]{deptid};
            }else {
                sql.append("");
                params=null;
            }
            rs= super.executeQuery(sql.toString(), params);
            try {
                while (rs.next()){
                    User user=new User();
                    user.setId(rs.getInt("id"));
                    user.setName(rs.getString("name"));
                    user.setPwd(rs.getString("pwd"));
                    user.setDeptId(rs.getInt("deptid"));
                    user.setDeptNmae(rs.getString("deptName"));
                    list.add(user);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                super.closeConn();
            }
            return list;
        }
    }

四、UserDeptService接口

五、UserDeptServiceImpl

六、UserDeptServlet

public class UserDeptServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        UserDeptServiceImpl userDeptService = new UserDeptServiceImpl();
        String action = request.getParameter("action");
        List<User> userList=null;
        //需求2 通过下拉获取用户对象的方法
        if(action.equals("findByDeptId")){
            String deptid = request.getParameter("deptid");
            if(deptid!=null && !"".equals(deptid)){
                userList= userDeptService.findByDeptId(Integer.parseInt(deptid));
            }else{
                userList=userDeptService.findByDeptId(-1);
            }
            String s = JSON.toJSONString(userList);
            response.getWriter().write(s);
        //需求1 查询部门
        }else if(action.equals("findAllDept")){
            List<Dept> deptList = userDeptService.findAllDept();
            String s = JSON.toJSONString(deptList);
            response.getWriter().write(s);
        }
    }
}

七、 list.jsp页面

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

<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(function () {
            ajaxDate();
        });
        function ajaxDate() {
            var deptid=$("#deptid").val();
            $.ajax({
                url:"http://localhost:8080/UserDeptServlet?action=findAllDept",
                type:"get",
                async:false,
                data:{},
                dataType:"json",
                success:function (obj) {
                    var str="";
                    $(".remove").remove();
                    if(deptid!=null && deptid!='' && deptid!='-1'){
                        $("#deptid").val(deptid);
                    }else {
                        str +="<option value='-1' class='remove'>---请选择部门---</option>";
                    }

                    $.each(obj,function (i) {
                        //***** class前面加空格*****
                        str +="<option value="+obj[i].id+" class='remove'>"+obj[i].name+"</option>";
                    });
                    $("#deptid").append(str);
                },
                error:function () {}
            });
            $.ajax({
                url:"http://localhost:8080/UserDeptServlet?action=findByDeptId",
                type:"get",
                async:false,
                data:{"deptid":deptid},
                dataType:"json",

                success:function (obj) {
                    var str="";
                    $("tr.remove").remove();
                    $.each(obj,function(i){
                        str+=" <tr class='remove'>";
                        str+="     <td>"+obj[i].id+"</td>";
                        str+="     <td>"+obj[i].name+"</td>";
                        str+="     <td>"+obj[i].pwd+"</td>";
                        str+="     <td>"+obj[i].deptNmae+"</td>";
                        str+=" </tr>";
                    });
                    $("#deptid").append(deptid);
                    $("#tbody").append(str);
                },
                error:function () {}
            });
        }
    </script>
</head>
<body>
    <div id="div1" align="center">
        <div>
            按部门查询<select name="deptid" id="deptid">
            <%--<option>XX部门</option>--%>
            <%--<option>XX部门</option>--%>
            <%--<option>XX部门</option>--%>
                </select>
            <button οnclick="ajaxDate()">查询</button>>

        </div>
        <table border="1px solid" cellpadding="0" cellspacing="0">
            <tr>用户列表</tr>
            <thead>
                <td>编码</td>
                <td>姓名</td>
                <td>密码</td>
                <td>部门名称</td>
            </thead>

            <tbody id="tbody">
            <%--<tr class="remove">--%>
                <%--<td>"+obj1[i].id+"</td>--%>
                <%--<td>"+obj1[i].name+"</td>--%>
                <%--<td>"+obj1[i].pwd+"</td>--%>
                <%--<td>"+obj1[i].deptNmae+"</td>--%>
            <%--</tr>--%>
            </tbody>
        </table>
    </div>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值