springmvc modelview 返回数据与页面交互处理

来源:互联网 发布:ember.js pdf 编辑:程序博客网 时间:2024/06/05 11:43

springmvc controller层在与前端页面进行数据交互时,根据页面处理需要对modelview响应数据做处理,
举例说明:

@RequestMapping(params = "action=jumpEduproduct")    public void getEduProduct(HttpServletRequest request, HttpServletResponse response) throws IOException {        logger.info("action=jumpEduproduct=====>");        Map<String,String> paramsMap = new HashMap<String, String>();        String jsonStr = "";        JSONObject jsonObject =  new JSONObject();        response.setHeader("Content-type", "text/html;charset=UTF-8");        response.setCharacterEncoding("UTF-8");//  ====处理方式1:         if (StringUtils.isBlank(userId)) {                    logger.error("error userid!");                    jsonObject.put("ret",-1);                    jsonObject.put("msg", "用户ID不能为空!");                    response.getWriter().print(jsonObject.toString());                    return;         }        if (resq.getRet() != 0){                jsonObject.put("msg","处理异常, 返回错误数据!");                jsonObject.put("ret",resq.getRet());                jsonObject.put("msg", resq.getMsg());                response.getWriter().print(jsonObject.toString());                return;        }........// ====处理方式2:    YdEduResp resq = JSON.parseObject(jsonStr, YdEduResp.class);            if ((appFlag == 0 || appFlag == 1)){                if (resq.getRet() == 0){                    directUrl = resq.getCallbackurl() + "&accesstoken=" + resq.getAccesstoken() + "&random=" + Math.random();                    System.out.println("success direct:" + directUrl);                    response.getWriter().write("<script>window.location.href=\'" + directUrl + "\';</script>");//              response.getWriter().write("<script>window.open(\'" + directUrl + "\','_blank')</script>");                    return;                }else{                    directUrl="http://www.baidu.com";                    System.out.println("==>fail direct2:" + directUrl+"__fail reason:"+resq.getMsg());                    response.getWriter().write("<script>window.alert(\'接口响应异常,返回XXX首页!\');window.location.href=\'" + directUrl + "\';</script>");                    return;                }

对应方式1 对应某页面page发起请求,响应json对象数据到page页面,page进行解析处理:

 page页面先发起ajax请求:
   $('.govdiv').click(function(){//          $.getScript(  $(this).attr('data-url') );            $.ajax({                url : $(this).attr('data-url'),                type : "POST",                data : JSON.stringify(data), //转JSON字符串                dataType: 'json',                contentType:'application/json;charset=UTF-8',                success : function(result) {                    dispatch(result);                }            });

result为controller的响应数据,click()提交请求响应成功后对响应数据进行解析:

function dispatch(data){        if(data.ret!=0)            alert(data.msg);        else{            if (data.accessToken != null && data.accessToken != ""){//              window.location.href = data.callbackurl +"&accesstoken=" + data.accesstoken+"&tmp="+Math.random();                window.open(data.callbackurl +"&accesstoken=" + data.accessToken+"&tmp="+Math.random());            }else{//              window.location.href = data.callbackurl;                window.open(data.callbackurl);            }        }

对应方式2:
直接打开新窗口或重定向到某页面,不与页面进行交互,一般用于异常场景,响应数据不需要前端解析直接对用户进行提示。

原创粉丝点击