day33 解耦 后台异步展示

2017-11-02  本文已影响0人  路人爱早茶

----------------解耦

-------------xml中配置,id为寻找,class为所要创建的实际地址
<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <!-- 配置AdminServiceImpl的清单 -->
    <bean id="adminService" class="com.itheima.service.impl.AdminServiceImpl"></bean>
    <!-- <bean id="adminService" class="com.itheima.service.impl.AdminServiceImpl2"></bean> -->

    <bean id="adminDao" class="com.itheima.dao.impl.AdminDaoImplMySql"></bean>
    <!-- <bean id="adminDao" class="com.itheima.dao.impl.AdminDaoImplOracle"></bean> -->

</beans>
----------------------使用
//用解耦合的方式进行编码----解web层与service层的耦合
        //使用工厂+反射+配置文件
        AdminService service = (AdminService) BeanFactory.getBean("adminService");
-------------------------BeanFactory
public static Object getBean(String id){
        
        //生产对象---根据清单生产----配置文件----将每一个bean对象的生产的细节配到配置文件中
        //使用dom4j的xml解析技术
        
        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="com.itheima.service.impl.AdminServiceImpl"></bean>
            String className = element.attributeValue("class");
            //com.itheima.service.impl.AdminServiceImpl
            //使用反射创建对象
            Class clazz = Class.forName(className);
            Object object = clazz.newInstance();
            
            return object;
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
Paste_Image.png Paste_Image.png

----------------后台

    //根据订单id查询订单项和商品信息
    public void findOrderInfoByOid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        //获得oid
        String oid = request.getParameter("oid");
        
        //用解耦合的方式进行编码----解web层与service层的耦合
        //使用工厂+反射+配置文件
        AdminService service = (AdminService) BeanFactory.getBean("adminService");
        
        List<Map<String,Object>> mapList = service.findOrderInfoByOid(oid);
        
        Gson gson = new Gson();
        String json = gson.toJson(mapList);
        System.out.println(json);
        /*[
         *  {"shop_price":4499.0,"count":2,"pname":"联想(Lenovo)小新V3000经典版","pimage":"products/1/c_0034.jpg","subtotal":8998.0},
         *  {"shop_price":2599.0,"count":1,"pname":"华为 Ascend Mate7","pimage":"products/1/c_0010.jpg","subtotal":2599.0}
         *]*/
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(json);
        }
---------------------------
function findOrderInfoByOid(oid){
                //清理上一次显示的内容覆盖
                $("#showDivTab").html("");
                $("#shodDivOid").html("");
                $("#loading").css("display","block");
    //ajax异步访问数据
                $.post(
                    "${pageContext.request.contextPath }/admin?method=findOrderInfoByOid",
                    {"oid":oid},
                    function(data){
            //隐藏加载图片
                        $("#loading").css("display","none");
                    /*[
                         *  {"shop_price":4499.0,"count":2,"pname":"联想(Lenovo)小新V3000经典版","pimage":"products/1/c_0034.jpg","subtotal":8998.0},
                         *  {"shop_price":2599.0,"count":1,"pname":"华为 Ascend Mate7","pimage":"products/1/c_0010.jpg","subtotal":2599.0}
                         *]*/
                        var content = "<tr id='showTableTitle'><th width='20%'>图片</th><th width='25%'>商品</th><th width='20%'>价格</th><th width='15%'>数量</th><th width='20%'>小计</th></tr>";
                        for(var i=0;i<data.length;i++){
                            content+="<tr style='text-align: center;'>"+
                            "<td>"+
                                "![](${pageContext.request.contextPath }/"+data[i].pimage+")"+
                            "</td>"+
                            "<td><a target='_blank'>"+data[i].pname+"</a></td>"+
                            "<td>¥"+data[i].shop_price+"</td>"+
                            "<td>"+data[i].count+"</td>"+
                            "<td><span class='subtotal'>¥"+data[i].subtotal+"</span></td>"+
                            "</tr>";
                        }                                                                       $("#showDivTab").html(content);
                        //订单编号
                        $("#shodDivOid").html(oid);
                                        },
                    "json"  );  }
Paste_Image.png
上一篇 下一篇

猜你喜欢

热点阅读