• 前后台数据交换,printwriter、jsonobject、jsonarray、ajax请求,数据交换


      后台代码:

    public void findByIDEquipment() {
            getResponse().setCharacterEncoding("UTF-8");
            getResponse().setContentType("text/html;charset=UTF-8");
            PrintWriter out;
            JSONObject obj = new JSONObject();
            JSONArray jarr = new JSONArray();
            ArrayList<Equipment> eq_array_list = new ArrayList<Equipment>();
            SimpleDateFormat sdf = new SimpleDateFormat();
            try {
                out = getResponse().getWriter();
    
                System.out.println(eq_id);
    
                long id = Long.valueOf(getRequest().getParameter("eq_id"));
                System.out.println("id is" + id);
                Equipment eq = eq_service.findEquipmentById(id);
                Equipment eq1 = eq;
                eq_array_list.add(eq);
                if (eq != null) {
                    /*
                     * getSession().setAttribute("eq_name", eq.getName());
                     * getSession().setAttribute("eq_comment", eq.getComment());
                     * getSession().setAttribute("eq_pn", eq.getPn());
                     * getSession().setAttribute("eq_sn", eq.getSn());
                     * getSession().setAttribute("eq_inDate", eq.getInDate());
                     * getSession().setAttribute("eq_alarmDay",
                     * eq.getAlarmTimeDay());
                     * getSession().setAttribute("eq_overTime", eq.getOverTime());
                     * getSession().setAttribute("eq_username",
                     * eq.getEq_user().getUsername());
                     */
                    obj.put("eq1", eq1);
                    obj.put("indate", sdf.format(eq1.getInDate()));
                    obj.put("overdate", sdf.format(eq1.getOverTime()));
                    jarr.add(obj);
    
                    out.print(jarr);
                    out.flush();
    
                } else {
                    out.println(false);
                }
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }

    前台代码:

    /* 详情动作 */
    	$('.action_div')
    			.click(
    					function() {
    						var click_num = $('.action_div').index(this)
    						console.log(click_num)
    						var eq_id = $('.action_div').eq(click_num).attr('org')
    						console.log("select item id is" + eq_id)
    
    						$.ajax({
    							type : "POST",
    							url : "findByIDEquipmentAction?fresh="
    									+ Math.random(),
    							data : {
    								"eq_id" : eq_id
    							},
    							cache : false,
    							async : true,
    							dataType : "json",
    							success : function(res) {
    								console.log(res)
    								console.log(res[0].eq1.name)
    
    								if (res) {
    
    									$('.getname').text(res[0].eq1.name)
    									$('.getcomment').text(res[0].eq1.comment)
    									$('.getpn').text(res[0].eq1.pn)
    									$('.getsn').text(res[0].eq1.sn)
    									$('.getindate').text(
    											res[0].indate.toString().substring(
    													0, 8))
    									$('.getalarmday').text(
    											res[0].eq1.alarmTimeDay)
    									$('.getoverdate').text(
    											res[0].overdate.toString()
    													.substring(0, 8))
    									$('.getusername').text(
    											res[0].eq1.eq_user.username)
    
    								} else {
    									alert("请求异常1");
    								}
    
    							},
    							error : function(json) {
    								alert("请求异常2");
    							}
    						})
    
    						$('#mask2')
    								.css(
    										{
    											"opacity" : "1",
    											"z-index" : "100",
    											'-webkit-transition' : 'opacity 2s ease-in,z-index 1s ease-in',
    											'-moz-transition' : 'opacity 2s ease-in,z-index 1s ease-in',
    											'-ms-transition' : 'opacity 2s ease-in,z-index 1s ease-in',
    											'-o-transition' : 'opacity 2s ease-in,z-index 1s ease-in',
    											'transition' : 'opacity 2s ease-in,z-index 1s ease-in'
    
    										})
    
    					})
    

      

    public void findByIDEquipment() {getResponse().setCharacterEncoding("UTF-8");getResponse().setContentType("text/html;charset=UTF-8");PrintWriter out;JSONObject obj = new JSONObject();JSONArray jarr = new JSONArray();ArrayList<Equipment> eq_array_list = new ArrayList<Equipment>();SimpleDateFormat sdf = new SimpleDateFormat();try {out = getResponse().getWriter();
    System.out.println(eq_id);
    long id = Long.valueOf(getRequest().getParameter("eq_id"));System.out.println("id is" + id);Equipment eq = eq_service.findEquipmentById(id);Equipment eq1 = eq;eq_array_list.add(eq);if (eq != null) {/* * getSession().setAttribute("eq_name", eq.getName()); * getSession().setAttribute("eq_comment", eq.getComment()); * getSession().setAttribute("eq_pn", eq.getPn()); * getSession().setAttribute("eq_sn", eq.getSn()); * getSession().setAttribute("eq_inDate", eq.getInDate()); * getSession().setAttribute("eq_alarmDay", * eq.getAlarmTimeDay()); * getSession().setAttribute("eq_overTime", eq.getOverTime()); * getSession().setAttribute("eq_username", * eq.getEq_user().getUsername()); */obj.put("eq1", eq1);obj.put("indate", sdf.format(eq1.getInDate()));obj.put("overdate", sdf.format(eq1.getOverTime()));jarr.add(obj);
    out.print(jarr);out.flush();
    } else {out.println(false);}
    } catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
    }

  • 相关阅读:
    snmp安装zabbix
    〖Demo〗-- 用Django实现Video页面分类查询
    〖Python〗-- Django的ORM性能优化建议
    〖Web〗-- 新特性之WebSocket
    〖Python〗-- 数据结构
    〖缓存〗-- Memcached 与 Redis
    〖算法〗-- NB二人组:堆排序、归并排序
    〖算法〗-- 快速排序 、希尔排序、计数排序
    〖算法〗-- 排序lowB三人组:冒泡排序、选择排序、 插入排序
    〖算法〗-- 递归、二分查找、列表查找
  • 原文地址:https://www.cnblogs.com/dingjiuping/p/printwriter_jsonobject_jsonarray_ajax_data_switch.html
Copyright © 2020-2023  润新知