zoukankan      html  css  js  c++  java
  • (十)分类展示中

    问题:
    完成之后 ,我们发现只有在访问首页的时候才能把分类列表展示出来,怎么办????? 要想让所有的页面上都有分类,只需要将 页面上 logo和菜单部分包含进来.怎么去查询分类信息呢????

    分析
    只需要在页面加载成功之后 发送一个ajax请求 异步查询所有的分类信息即可

     技术:
     json
      包含
      ajax

     步骤分析:
      1.编写一个 CategorySerlvet
      2.findAll方法用来查询所有
      list通过json返回到页面上
      3.在head.jsp加载成功之后发送一个ajax请求
      $.get(url,params,function(data){},"json");

    前端

    新添加head.jsp,删除index.jsp,login.jsp相同的部分

    head.jsp添加ajax

    有头信息的界面直接添加head.jsp即可

    新建categoryservlet

      將indexServlet的處理過程複製過來

        /*
         * 查询所有分类
         * */
        public  String  findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1、调用categoryservice查询所有的分类,返回list
            CategoryService categoryService = new CategoryServiceImpl();
            List<Category> clist =null;
            try {
                clist = categoryService.findAll();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //2、将返回值轉化成json格式返回
            /*request.setAttribute("clist", clist);*/
            String json = JsonUtil.list2json(clist);
            
            //3寫回去
            response.setContentType("text/html;charset=utf-8"); //處理响应乱码
            response.getWriter().println(json);
            return null;
        }

    導入json包

      見:http://pan.baidu.com/s/1pLQbDSf

    導入json工具類

    package com.louis.utils;
    
    import java.util.List;
    import java.util.Map;
    
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    import net.sf.json.JsonConfig;
    import net.sf.json.util.CycleDetectionStrategy;
    import net.sf.json.xml.XMLSerializer;
    
    /**
     * 处理json数据格式的工具类
     * 
     * @Date 2013-3-31
     * @version 1.0
     */
    public class JsonUtil {
        /**
         * 将数组转换成String类型的JSON数据格式
         * 
         * @param objects
         * @return
         */
        public static String array2json(Object[] objects){
            
            JSONArray jsonArray = JSONArray.fromObject(objects);
            return jsonArray.toString();
            
        }
        
        /**
         * 将list集合转换成String类型的JSON数据格式
         * 
         * @param list
         * @return
         */
        public static String list2json(List list){
            
            JSONArray jsonArray = JSONArray.fromObject(list);
            return jsonArray.toString();
            
        }
        
        /**
         * 将map集合转换成String类型的JSON数据格式
         * 
         * @param map
         * @return
         */
        public static String map2json(Map map){
            
            JSONObject jsonObject = JSONObject.fromObject(map);
            return jsonObject.toString();
            
        }
        
        /**
         * 将Object对象转换成String类型的JSON数据格式
         * 
         * @param object
         * @return
         */
        public static String object2json(Object object){
            
            JSONObject jsonObject = JSONObject.fromObject(object);
            return jsonObject.toString();
            
        }
        
        /**
         * 将XML数据格式转换成String类型的JSON数据格式
         * 
         * @param xml
         * @return
         */
        public static String xml2json(String xml){
            
            JSONArray jsonArray = (JSONArray) new XMLSerializer().read(xml);
            return jsonArray.toString();
            
        }
        
        /**
          * 除去不想生成的字段(特别适合去掉级联的对象)
          *
          * @param excludes
          * @return
        */
        public static JsonConfig configJson(String[] excludes) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setExcludes(excludes);
            jsonConfig.setIgnoreDefaultExcludes(true);
            jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
            return jsonConfig;
        }
        
    }

    在前端遍历加入后台返回的值

    效果

    每个都有了head.jsp

    问题

    ajax基础

    http://www.cnblogs.com/Michael2397/p/7643534.html

    包含的区别

    json工具類

  • 相关阅读:
    css技巧和经验列表
    CSS3嵌入字体@font-face调用字体
    新闻列表单行滚动(多行显示)简洁向上滚动js效果
    打破构图的平衡!增强设计感染力
    何以双十(补昨天)
    MySQL5.7 基于二进制包的安装
    Nginx Log日志统计分析常用命令
    MySQL错误代码大全
    MVC4中的Display Mode简介
    ReadOnly关键字修饰的变量可以修改,只是不能重新分配
  • 原文地址:https://www.cnblogs.com/Michael2397/p/7640189.html
Copyright © 2011-2022 走看看