• json信息的后台到前台的传输


    公共方法:

    
    /**
     * 实际返回的是 response.setContentType("text/html;charset=utf-8");
     * 
     * @param o
     */
    public void writeJson(Object o) {
    	String json = getJsonString(o);
    	try {
    		response.setContentType("text/html;charset=utf-8");
    		response.getWriter().write(json);
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    }
    
    public String getJsonString(Object o) {
    	ObjectMapper om = new ObjectMapper();
    	StringWriter sw = new StringWriter();
    	try {
    		JsonGenerator jg = new JsonFactory().createJsonGenerator(sw);
    		om.writeValue(jg, o);
    		jg.close();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	return sw.toString();
    }

    Json形式:

    • 形式一:
    {"total":29,"rows":[{"id":1,"brandName":"兄弟","isvalid":"已启用"},{"id":2,"brandName":"西红柿","isvalid":"已启用"}]}

    后台实现:

    List<AuxiliaryBrand> list =  auxiliaryService.getAuxiliaryBrand(paramMap,intPage,number,user);
    
    int count = auxiliaryService.getAuxiliaryBrandCount(paramMap,user);
    
    Map<String, Object> map = new HashMap<String, Object>();
       map.put("total", count);
       map.put("rows", list2);
    
    this.writeJson(map);//顶部公共方法

    前台获取:

     onLoadSuccess: function (data) {
         if (data.total == 0) {
                 //active
         }
    
         var rows = data.rows;
     }
    • 形式二:
    {"success":false,"msg":"品牌名称【兄弟】已存在!请重新输入!","obj":null}

    后台:

     /**
     * 品牌重复性检索
     */
    public void repeatCheckAuxiliaryBrand(){
    	String brandName = request.getParameter("brandName");
    	String actionType = request.getParameter("actionType");
    	int checkFlag=  auxiliaryService.repeatCheckAuxiliaryBrand(brandName,actionType);
    	Json j=new Json();
    	if(checkFlag==2){
    		j.setSuccess(false);
    		j.setMsg("品牌名称【"+brandName+"】已存在!请重新输入!");
    	}else{
    		j.setSuccess(true);
    		j.setMsg(actionType);
    	}
    	this.writeJson(j);//顶部公共方法
    }
    

    Json对象j的数据

    response.getWriter().write(json);后的值

    传到前台接收:

     /**
     * 重复检验
     */
    function repeatCheck(actionType){
    	var brandName = $('#brandNames').textbox('getValue');
    	var url = context_path + '/auxiliary/repeatCheckAuxiliaryBrand.do';
    	$.ajax({
    		url : url,
    		data : {
    			brandName : brandName,
    			actionType :actionType
    		   },
    		dataType : 'json',
    		success : function(result) {
    			if (result.success){
    				var action=result.msg;
    				submitSave(action);
    				return true;
    			}else{
    				$.messager.alert('操作提示', result.msg,'warning');
    				return false;
    			}
    			}
    	});
    }
  • 相关阅读:
    勤于思考,善于总结,积极进取,认识自己
    notepad++中cmd运行中文乱码?
    notpad++使用cmd的快捷键设置
    深刻理解Table以及关于table的插件(1)
    单向链表
    apriori算法
    保存一个班级的学生信息
    测试list列表中append和insert的执行速度
    二分查找
    顺序查找法
  • 原文地址:https://www.cnblogs.com/wjup/p/10576088.html
Copyright © 2020-2023  润新知