• 案例42-使用ajax获取crm中的客户列表


    1webcontent部分

    1 修改menu.jsp代码

    2 jsp/customer/list.jsp代码

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <TITLE>客户列表</TITLE> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <LINK href="${pageContext.request.contextPath }/css/Style.css" type=text/css rel=stylesheet>
    <LINK href="${pageContext.request.contextPath }/css/Manage.css" type=text/css
        rel=stylesheet>
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
    <SCRIPT language=javascript>
        function to_page(page){
            if(page){
                $("#page").val(page);
            }
            document.customerForm.submit();
        }
        $(function(){
            $.post(
                "${pageContext.request.contextPath}/customer",
                {"method":"findAllCustomerList"},
                function(data){
                    /*
                     * [
                     * {"cust_id":2,"cust_name":"谷歌有限公司"},
                     * {"cust_id":6,"cust_name":"张三","cust_source":"阿里巴巴","cust_level":"vip","cust_linkman":"李四","cust_phone":"1234","cust_mobile":"4321"},
                     * {"cust_id":7,"cust_name":"新浪科技","cust_level":"贵宾","cust_linkman":"李四"},
                     * {"cust_id":8,"cust_name":"联想公司"},
                     * {"cust_id":9,"cust_name":"永安会计事务所","cust_source":"国际事务所成员","cust_level":"vip","cust_linkman":"马大强","cust_phone":"789632541","cust_mobile":"123456789"}
                     * ]
                     */
                     var content="<TR style='FONT-WEIGHT: bold; FONT-STYLE: normal; BACKGROUND-COLOR: #eeeeee; TEXT-DECORATION: none'>"+
                        "<TD>客户名称</TD>"+
                        "<TD>客户级别</TD>"+
                        "<TD>客户来源</TD>"+
                        "<TD>联系人</TD>"+
                        "<TD>电话</TD>"+
                        "<TD>手机</TD>"+
                        "<TD>操作</TD>"+
                    "</TR>";
                    
                    for(var i=0;i<data.length;++i){
                        content+="<TR style='FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none'>"+
                        "<TD>"+data[i].cust_name+"</TD>"+
                        "<TD>"+data[i].cust_level+"</TD>"+
                        "<TD>"+data[i].cust_source+"</TD>"+
                        "<TD>"+data[i].cust_linkman+"</TD>"+
                        "<TD>"+data[i].cust_phone+"</TD>"+
                        "<TD>"+data[i].cust_mobile+"</TD>"+
                        "<TD>"+
                        "<a href='${pageContext.request.contextPath }/customerServlet?method=edit&custId=${customer.custId}'>修改</a>"+
                        "&nbsp;&nbsp;"+
                        "<a href='${pageContext.request.contextPath }/customerServlet?method=delete&custId=${customer.custId}'>删除</a>"+
                        "</TD>"+
                    "</TR>";
                    
                    }
                    $("#customerListId").html(content);
                     
                },
                "json"
            );
        });
    </SCRIPT>
    
    <META content="MSHTML 6.00.2900.3492" name=GENERATOR>
    </HEAD>
    <BODY>
        <FORM id="customerForm" name="customerForm"
            action="${pageContext.request.contextPath }/customerServlet?method=list"
            method=post>
            
            <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
                <TBODY>
                    <TR>
                        <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_019.jpg"
                            border=0></TD>
                        <TD width="100%" background="${pageContext.request.contextPath }/images/new_020.jpg"
                            height=20></TD>
                        <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_021.jpg"
                            border=0></TD>
                    </TR>
                </TBODY>
            </TABLE>
            <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
                <TBODY>
                    <TR>
                        <TD width=15 background=${pageContext.request.contextPath }/images/new_022.jpg><IMG
                            src="${pageContext.request.contextPath }/images/new_022.jpg" border=0></TD>
                        <TD vAlign=top width="100%" bgColor=#ffffff>
                            <TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
                                <TR>
                                    <TD class=manageHead>当前位置:客户管理 &gt; 客户列表</TD>
                                </TR>
                                <TR>
                                    <TD height=2></TD>
                                </TR>
                            </TABLE>
                            <TABLE borderColor=#cccccc cellSpacing=0 cellPadding=0
                                width="100%" align=center border=0>
                                <TBODY>
                                    <TR>
                                        <TD height=25>
                                            <TABLE cellSpacing=0 cellPadding=2 border=0>
                                                <TBODY>
                                                    <TR>
                                                        <TD>客户名称:</TD>
                                                        <TD><INPUT class=textbox id=sChannel2
                                                            style="WIDTH: 80px" maxLength=50 name="custName"></TD>
                                                        
                                                        <TD><INPUT class=button id=sButton2 type=submit
                                                            value=" 筛选 " name=sButton2></TD>
                                                    </TR>
                                                </TBODY>
                                            </TABLE>
                                        </TD>
                                    </TR>
                                    
                                    <TR>
                                        <TD>
                                            <TABLE id=grid
                                                style="BORDER-TOP-WIDTH: 0px; FONT-WEIGHT: normal; BORDER-LEFT-WIDTH: 0px; BORDER-LEFT-COLOR: #cccccc; BORDER-BOTTOM-WIDTH: 0px; BORDER-BOTTOM-COLOR: #cccccc; WIDTH: 100%; BORDER-TOP-COLOR: #cccccc; FONT-STYLE: normal; BACKGROUND-COLOR: #cccccc; BORDER-RIGHT-WIDTH: 0px; TEXT-DECORATION: none; BORDER-RIGHT-COLOR: #cccccc"
                                                cellSpacing=1 cellPadding=2 rules=all border=0>
                                                <TBODY id="customerListId">
                                                    <!-- <TR
                                                        style="FONT-WEIGHT: bold; FONT-STYLE: normal; BACKGROUND-COLOR: #eeeeee; TEXT-DECORATION: none">
                                                        <TD>客户名称</TD>
                                                        <TD>客户级别</TD>
                                                        <TD>客户来源</TD>
                                                        <TD>联系人</TD>
                                                        <TD>电话</TD>
                                                        <TD>手机</TD>
                                                        <TD>操作</TD>
                                                    </TR> -->
                                                    
                                                    <%-- <TR style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
                                                        <TD>${customer.custName }</TD>
                                                        <TD>${customer.custLevel }</TD>
                                                        <TD>${customer.custSource }</TD>
                                                        <TD>${customer.custLinkman }</TD>
                                                        <TD>${customer.custPhone }</TD>
                                                        <TD>${customer.custMobile }</TD>
                                                        <TD>
                                                        <a href="${pageContext.request.contextPath }/customerServlet?method=edit&custId=${customer.custId}">修改</a>
                                                        &nbsp;&nbsp;
                                                        <a href="${pageContext.request.contextPath }/customerServlet?method=delete&custId=${customer.custId}">删除</a>
                                                        </TD>
                                                    </TR> --%>
                                                    
                                                    
                                                </TBODY>
                                            </TABLE>
                                        </TD>
                                    </TR>
                                    
                                    <TR>
                                        <TD><SPAN id=pagelink>
                                                <DIV
                                                    style="LINE-HEIGHT: 20px; HEIGHT: 20px; TEXT-ALIGN: right">
                                                    共[<B>${total}</B>]条记录,[<B>${totalPage}</B>]页
                                                    ,每页显示
                                                    <select name="pageSize">
                                                    
                                                    <option value="15" <c:if test="${pageSize==1 }">selected</c:if>>1</option>
                                                    <option value="30" <c:if test="${pageSize==30 }">selected</c:if>>30</option>
                                                    </select>
                                                    条
                                                    [<A href="javascript:to_page(${page-1})">前一页</A>]
                                                    <B>${page}</B>
                                                    [<A href="javascript:to_page(${page+1})">后一页</A>] 
                                                    到
                                                    <input type="text" size="3" id="page" name="page" /><input type="button" value="Go" onclick="to_page()"/>
                                                </DIV>
                                        </SPAN></TD>
                                    </TR>
                                </TBODY>
                            </TABLE>
                        </TD>
                        <TD width=15 background="${pageContext.request.contextPath }/images/new_023.jpg"><IMG
                            src="${pageContext.request.contextPath }/images/new_023.jpg" border=0></TD>
                    </TR>
                </TBODY>
            </TABLE>
            <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
                <TBODY>
                    <TR>
                        <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_024.jpg"
                            border=0></TD>
                        <TD align=middle width="100%"
                            background="${pageContext.request.contextPath }/images/new_025.jpg" height=15></TD>
                        <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_026.jpg"
                            border=0></TD>
                    </TR>
                </TBODY>
            </TABLE>
        </FORM>
    </BODY>
    </HTML>
    View Code

    2 web层

    1 CustomerServlet->findAllCustomerList方法

    //2获取客户列表findAllCustomerList
    public void findAllCustomerList(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        //解决乱码问题通过GenericEncodingFilter
        List<Customer> customerList = service.findAllCustomerList();
        
        //转换成json
        Gson gson = new Gson();
        String json = gson.toJson(customerList);
        
        //System.out.println(json);
        /*
         * [
         * {"cust_id":2,"cust_name":"谷歌有限公司"},
         * {"cust_id":6,"cust_name":"张三","cust_source":"阿里巴巴","cust_level":"vip","cust_linkman":"李四","cust_phone":"1234","cust_mobile":"4321"},
         * {"cust_id":7,"cust_name":"新浪科技","cust_level":"贵宾","cust_linkman":"李四"},
         * {"cust_id":8,"cust_name":"联想公司"},
         * {"cust_id":9,"cust_name":"永安会计事务所","cust_source":"国际事务所成员","cust_level":"vip","cust_linkman":"马大强","cust_phone":"789632541","cust_mobile":"123456789"}
         * ]
         */
        //回写
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(json);
        
    }

    3 service层

    1 CustomerServie接口

    package www.test.service;
    
    import java.util.List;
    
    import www.test.domain.Customer;
    
    public interface CustomerService {
    
        //保存客户
        void save(Customer customer);
    
        //获取客户列表
        List<Customer> findAllCustomerList();
    
    }

    2 CustomerServiceImpl实现类

    @Override
    //获取客户列表,这里实际上不需要事务控制,只是为了练习而已
    public List<Customer> findAllCustomerList() {
        //获得session
        Session session = HibernateUtils.getCurrentSession();
        //控制事务
        Transaction transaction = session.beginTransaction();
        List<Customer> customerList =null;
        try {
            //调用dao层获取客户列表
            customerList = dao.findAllCustomerList();
        } catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
        }finally{
            transaction.commit();
        }

    4 dao层

    1 CustomerDao接口

    package www.test.dao;
    
    import java.util.List;
    
    import www.test.domain.Customer;
    
    public interface CustomerDao {
    
        //保存客户
        void save(Customer customer);
    
        //获取客户列表
        List<Customer> findAllCustomerList();
    
    }

    2 CustomerDaoImpl实现类

    @Override
    //获取客户列表
    public List<Customer> findAllCustomerList() {
        //获得session
        Session session = HibernateUtils.getCurrentSession();
        //创建执行者对象
        Criteria criteria = session.createCriteria(Customer.class);
        //执行操作
        List<Customer> list = criteria.list();
        
        return list;
    }

    5 再次复习记忆

    1 BaseServlet

    package www.test.web.servlet;
    
    import java.io.IOException;
    import java.lang.reflect.Method;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    @SuppressWarnings("all")
    public class BaseServlet extends HttpServlet {
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //解决乱码问题
            resp.setContentType("text/html;charset=UTF-8");
            
            try {
                // 1 获得请求的method方法
                String methodName = req.getParameter("method");
                // 2获得当前被访问的对象的字节码对象
                Class clazz = this.getClass(); //ProductServlet.class --- UserServlet.class
                // 3 获取当前字节码对象中指定的方法
                Method method = clazz.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
                // 4 执行相应的方法
                method.invoke(this,req,resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
        
    }

    2 BeanFactory工具类

    package www.test.utils;
    
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    @SuppressWarnings("all")
    public class BeanFactory {
    
        public static Object getBean(String id){
            //生成对象--根据清单生产--配置文件--将每一个bean对象的生产细节配置到配置文件中
            
            //使用dom4j的xml解析技术 导入两个jar包
            // dom4j-1.6.1.jar 和 jaxen-1.1-beta-6.jar
            
            try {
                // 1 创建解析器
                SAXReader reader = new SAXReader();
                // 2 解析文档--bean.xml 在src下面
                String path = BeanFactory.class.getClassLoader().getResource("bean.xml").getPath();
                  //读取
                Document doc = reader.read(path);
                
                
                // 3 获得元素--参数是xpath规则
                Element element = (Element) doc.selectSingleNode("//bean[@id='"+id+"']");
                //<bean id="adminService" class="www.test.service.impl.AdminServiceImpl"></bean>
                String className = element.attributeValue("class"); 
                //www.test.service.impl.AdminServiceImpl
                
                //使用反射创建对象
                Class clazz = Class.forName(className);
                Object object = clazz.getDeclaredConstructor().newInstance();
                
                return object;
                
            } catch (Exception e) {
                
                e.printStackTrace();
            }
            
            return null;
        }
    }
  • 相关阅读:
    Java学习——HashMap
    git 常用命令
    java 正则表达式
    java 读写文件
    python Shapely 使用指南
    java 删除目录、 文件
    Java maven安装GDAL
    postgresql运维
    (转)媒体格式分析之flv -- 基于FFMPEG
    (转)rtmp协议简单解析以及用其发送h264的flv文件
  • 原文地址:https://www.cnblogs.com/jepson6669/p/8493166.html
Copyright © 2020-2023  润新知