jQuery是用ajax进行前后台交互

1.传输格式为text

 function showError () {
            alert("网络繁忙.");
        }
    //保存审批
    function save(){        
            // Ajax请求后端保存方法
             $.ajax({
                    type: "post",
                    url : "${ctx}/nhSalePerformanceAction.do?method=submitExamination",
                    dataType : "text",
                    data: $('#examinationForm').serialize(),
                    success:function(result){
                        //text转换json
                        var cmd = jQuery.parseJSON(result);
                        //alert(cmd);
                        if(cmd.success=="false"){
                           alert(cmd.message)
                           return;
                        }else if(cmd.success=="true"){
                            alert(cmd.message)
                            top.window.jBox.close();
                        }
                    },
                    error:showError
                });    
    };

后台处理:

/**
     * 业绩单审核 by pypua
     * vo 审批记录实体
     * submitExamination
     */
    @SuppressWarnings("javadoc")
    public String submitExamination(@Read final NhSalePerformanceRecord vo){
        return this.filterException("submitExamination", new MethodCallBack() {
            
            @Override
            public String doProcessMethod() throws Exception {
                com.alibaba.fastjson.JSONObject obj = new com.alibaba.fastjson.JSONObject();
                //防止两台PC同时审批
                NhSalePerformance salePerformance = nhSalePerformanceIService.findById(vo.getPerformance());
                if(!NhSalePerformance.AUDITSTATUS_TO_AUDIT.equals(salePerformance.getAuditStatus())){
                    obj.put("success", "false");
                    obj.put("message", "此业绩单已审核");
                    String result = obj.toJSONString();
                    renderText(result);    
                    return null;
                }
                //审核拒绝时 拒绝原因是必填项
                if(NhSalePerformance.AUDITSTATUS_REJECT.equals(vo.getAuditResult())&&
                        AppStringUtils.isEmpty(vo.getAuditComment())){
                        obj.put("success", "false");
                        obj.put("message", "请填写拒绝原因");
                        String result = obj.toJSONString();
                        renderText(result);    
                        return null;
                    
                }
                nhSalePerformanceIService.examinationPerformance(vo);                
                obj.put("success", "true");
                obj.put("message", "审核成功");
                String result = obj.toJSONString();
                renderText(result);                
                return null;
            }
        });
    }

2.传输格式为json

function showPerformanceDate(obj){
    $("#ui_alert_aa").css("display","table");
    var showDate = $('#in_showDate').val();
    $("#idPerformance").val("");
    $(".showPerformance").html("");
    //clearOnClick(obj);
    $.ajax({
        type: "post",
        url : "/ticket/getPerformances?showDate="+showDate,
        dataType : "json",
        success:function(cmd){
            $("#ui_alert_aa").css("display","none");
            //restoreOnClick(obj);
            if(cmd.success=="false"){
                $(".showTimeDate").html("");
                 $("#idPerformance").val("");
                jAlert(cmd.message,"提示");
                return;
            } else if(cmd.success=="true"){
                $(".showPerformance").html("");
                var _html="";
                var perfNum = $(cmd.performanceList).size();
                $(cmd.performanceList).each(function(i,item){
                    var _performanceId = item["performanceId"];
                    var _performanceName = item["startTime"].substring(11, 16);
                    var _style = "class='show_Timea ";
                    if (i == 0) {
                        if(perfNum==1){
                            _style += " show_Time";
                             $("#showTimeDate").val(_performanceName);
                             $("#idPerformance").val(_performanceId);
                        }else if($("#showTimeDate").val()==_performanceName){
                            _style += " show_Time";
                             $("#showTimeDate").val(_performanceName);
                             $("#idPerformance").val(_performanceId);
                        }else if($("#showTimeDate").val()==''){
                            _style += " show_Time";
                             $("#showTimeDate").val(_performanceName);
                             $("#idPerformance").val(_performanceId);
                        }
                    }else{
                        if($("#showTimeDate").val()==_performanceName){
                            _style += " show_Time";
                             $("#showTimeDate").val(_performanceName);
                             $("#idPerformance").val(_performanceId);
                        }
                    } 
                    _html+="<cite "+ _style +"' onclick=\"setPerformanceId('"+_performanceId+"',this)\">"+_performanceName+"</cite>";
                });
                $(".showPerformance").html("<span>演出时间</span><span style='float:right;width:192px;'>"+_html+"</span>");
                if(obj!=true){
                    seek();
                }
            } else{
                jAlert("网络繁忙","提示");
                return;
            }
        },
        error : showError
    });    
}

后台处理:

@RequestMapping(value = "/ticket/getPerformances", method = {RequestMethod.GET,RequestMethod.POST})
    public void getPerformances(HttpServletRequest request,
                                 HttpServletResponse response,
                                 @RequestParam(value = "idCompany", required = false) String idCompany,
                                 @RequestParam(value = "showDate", required = false) String showDate,
                                 @RequestParam(value = "isHotel", required = false) String isHotel
                                 ) throws Exception {
        JSONObject obj = new JSONObject();
        String currentDate = "";
        if (StringUtil.isNotEmpty(showDate)) {
            currentDate = showDate;
        } else {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            currentDate = PropertyLoader.instance().getSellLimitDate(idCompany,StringUtil.isNotEmpty(isHotel));
            
        }
        
        Date endDate = DateUtil.toDate(currentDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(endDate);
        cal.add(Calendar.MONTH, 1);
        String endDateStr = DateUtil.formatDate(cal.getTime());
        if (StringUtil.isEmpty(idCompany)) {
            idCompany = (String) request.getSession().getAttribute(SessionKeyContent.CRS_B2C_ID_COMPANY);
            //idCompany = (String) request.getSession().getAttribute(SessionKeyContent.CRS_B2C_ID_COMPANY_TICKET);
        }
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new NameValuePair("idCompany", idCompany));
        String sourceCode=DictConstants.ORDER_SOURCE_WEBSITE;//默认来源是官网
        /*if(null!=getSession(request, SessionKeyContent.CRS_SOURCE_CODE)&&"wchat".equals(getSession(request, SessionKeyContent.CRS_SOURCE_CODE).toString())){
            sourceCode=DictConstants.ORDER_SOURCE_WECHAT;
        }*/
        params.add(new NameValuePair("idClient", sourceCode));
        params.add(new NameValuePair("startTime", currentDate));
        params.add(new NameValuePair("endTime", endDateStr));
        List<PerformanceBean> formances = null;
        try {
            String result = Config.doSend(params, PropertyLoader.instance().getQueryPerformancesGateway());
            JSONObject o = JSON.parseObject(result);
            formances = JSON.parseArray(JSON.toJSONString(o.get("data")), PerformanceBean.class);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        List<PerformanceBean> formancess = new ArrayList<PerformanceBean>();
        if (formances != null) {
            for (int i=0;i<formances.size();i++) {
                PerformanceBean bean = formances.get(i);
                if (bean.getStartTime().contains(showDate)) {
                    formancess.add(bean);
                }
            }
        }
        if (formancess.size() == 0) {
            obj.put("success", "false");
            obj.put("message", "当前演出日期没有场次");
            response.getWriter().print(obj);
            return;
        }
        if (formancess != null) {
            
            Collections.sort(formancess, new Comparator<PerformanceBean>() {

                @Override
                public int compare(PerformanceBean o1, PerformanceBean o2) {
                    if (o1.getStartTime().compareTo(o2.getStartTime()) < 0) {
                        return -1;
                    } else if (o1.getStartTime().compareTo(o2.getStartTime()) > 0) {
                        return 1;
                    }

                    return 0;
                }
                
            });
        }
        
        obj.put("success", "true");
        obj.put("performanceList", formancess);
        response.getWriter().print(obj);
        return;
    }

 

posted @ 2018-08-02 18:04  十月围城小童鞋  阅读(491)  评论(0编辑  收藏  举报