• 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;
    			}
    			}
    	});
    }
  • 相关阅读:
    HTML5学习记录
    CSS学习记录
    HTML扩展(thead,tbody,tfoot标签的使用)
    测删除功能
    jmeter-连接数据库
    jmeter-正则表达式提取器
    jmeter常用函数
    java基础(二)
    git基本使用
    波特的钻石模型
  • 原文地址:https://www.cnblogs.com/wjup/p/10576088.html
Copyright © 2020-2023  润新知