(六)easyUI之对话框窗口
阅读原文时间:2023年07月12日阅读:4

一、拥有HTML的对话框

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path=request.getContextPath(); %>




Insert title here




  
1.拥有HTML的对话框

标题1

标题2

标题3

标题4

</pre>  


结果:

二、远程加载的对话框

  • href只负责加载body里的内容,而script标签和style标签的里的javasc和css是不生效的,content可以处理script标签和style标签的里的javasc和css

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<% String path = request.getContextPath(); %>




Insert title here




  
2.远程加载的对话框,
href只负责加载body里的内容,而script标签和style标签的里的javasc和css是不生效的
content可以处理script标签和style标签的里的javasc和css

</pre>  


include.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


Insert title here


被包含的页面

结果:

三、对话框传值

  • 案例:本例在第四章网格的基础上实现用户信息的修改

  • 数据库:

  • index2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<% String path = request.getContextPath(); %>


Insert title here



1.分页+排序+查询网格


查询用户id:

<table id="dg"></table>  
<!-- 修改用户时的提示框 -->  
<div id="dialog\_div"></div>  


  • 获取网格数据的servlet:@WebServlet("/servlet/getDataGrid")

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");  
    response.setCharacterEncoding("UTF-8");  
    response.setContentType("text/html");  
    //获取查询条件  
    String searchId=request.getParameter("search\_id");  
    PrintWriter out = response.getWriter();  
    Connection conn = null;  
    String sql = "select \* from users where 1=1";  
    // 查询记录总数量的sql语句  
    String countSQL = "select count(\*) from users";

    //

    try {  
        conn = DBUtil.getConn();  
        QueryRunner queryRunner=new QueryRunner();  
        // 设置分页属性,page和rows是从前台传过来的参数,page指第几页,rows表示该页显示几条数据  
        int page=Integer.valueOf(request.getParameter("page"));  
        int rows=Integer.valueOf(request.getParameter("rows"));  
        //如果需要排序,则前台会传需要排序的列名sort和排序方式order。  
        String sortName=request.getParameter("sort");  
        String sortType=request.getParameter("order");  
        int startIndexx=0;  
        if(page==1){  
             startIndexx = 0;  
        }else{  
            startIndexx=0+(page-1)\*rows;  
        }

        int endIndex = rows;  
        // 查询记录总数量  
        int count = getCount(countSQL);

        /\*\*  
         \* 有查询条件时的sql语句  
         \*/

        if(searchId!=null){  
            sql=sql+" and id= "+searchId+"";  
        }

        if(sortName!=null && sortType!=null){  
            //说明需要排序  
            sql = sql+" order by "+sortName+" "+sortType+"  limit " + startIndexx + " , " + endIndex + "   ";

        }else{  
            //不需要排序  
            sql = sql+" limit " + startIndexx + " , " + endIndex + "   ";  
        }

        System.out.println(sql);  
        List<User> userList=queryRunner.query(conn, sql, new BeanListHandler<>(User.class));  
        List<Map<String, String>> gridDataList = new ArrayList<Map<String, String>>();  
        Map<String, Object> gridDataMap = new HashMap<String, Object>();  
        Map<String, String> columnMap = null;  
        for(User user:userList){

            String id = (String.valueOf(user.getId()));  
            String userName = user.getUserName();  
            String passWord = user.getPassWord();

            columnMap = new HashMap<String, String>();  
            columnMap.put("id", id);  
            columnMap.put("userName", userName);  
            columnMap.put("passWord", passWord);  
            gridDataList.add(columnMap);  
        }  
        gridDataMap.put("total", count);  
        gridDataMap.put("rows", gridDataList);  
        Gson gson = new Gson();  
        String str\_gridData = gson.toJson(gridDataMap);  
        System.out.println(str\_gridData);  
        out.print(str\_gridData);

    } catch (Exception e) {  
        e.printStackTrace();  
    }

    out.flush();  
    out.close();

}

/\*\*  
 \* 根据sql查询数据库中的总记录数量  
 \*  
 \* @param countSQL  
 \* @return  
 \*/  
private int getCount(String countSQL) {  
    int res = 0;  
    Connection conn = null;  
    Statement stat = null;  
    ResultSet rs = null;

    try {  
        conn = DBUtil.getConn();  
        stat = conn.createStatement();  
        rs = stat.executeQuery(countSQL);  
        while (rs.next()) {  
            res = rs.getInt("count(\*)");

        }

    } catch (Exception e) {  
        e.printStackTrace();  
    } finally {  
        try {  
            conn.close();  
            rs.close();  
            stat.close();  
        } catch (Exception e2) {  
            e2.printStackTrace();  
        }

    }

    return res;  
}
  • 编辑用户的servlet : @WebServlet("/servlet/editUser")

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");  
    response.setCharacterEncoding("UTF-8");

    /\*\*  
     \* 拿到要修改用户的id  
     \*/  
    String user\_id=request.getParameter("userId");

    String sql="select \* from users where id="+user\_id+"";  
    Connection conn=null;  
    Map<String,Object> userMap=new HashMap<String,Object>();  
    try {  
        conn=DBUtil.getConn();  
        QueryRunner queryRunner=new QueryRunner();  
        userMap=queryRunner.query(conn, sql, new MapHandler());

        request.setAttribute("userMap", userMap);  
    } catch (Exception e) {

        e.printStackTrace();  
    }

    request.getRequestDispatcher("/edit.jsp").forward(request, response);

}
  • 保存修改用户的servlet:@WebServlet("/servlet/saveUser")

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");  
    response.setCharacterEncoding("UTF-8");  
    PrintWriter out=response.getWriter();

    String userId=request.getParameter("userid");  
    String userName=request.getParameter("username");  
    String passWord=request.getParameter("password");

    Connection conn=null;  
    Statement stat=null;  
    String sql=" update users set username='"+userName+"' , password='"+passWord+"' where id='"+userId+"'";  
    Map<String,Object> resMap=new HashMap<String,Object>();  
    try {

        conn=DBUtil.getConn();  
        stat=conn.createStatement();  
        int res=stat.executeUpdate(sql);

        if(res>0){  
            resMap.put("flag", true);  
        }

        Gson gson=new Gson();  
        String res\_json=gson.toJson(resMap);  
        out.print(res\_json);  
        out.flush();  
        out.close();

    } catch (Exception e) {  
        e.printStackTrace();  
        resMap.put("flag",false);  
        resMap.put("errMes", e.getMessage());

    }

}
  • 编辑用户的页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<% String path=request.getContextPath(); %>



Insert title here


用户id: 用户名: 密码:


结果: