1. 程式人生 > >【SSM】【4】前端後端資料流轉

【SSM】【4】前端後端資料流轉

前後端資料流轉圖:

業務流轉圖

  1. 前端控制器接受使用者請求響應
    1.         doJsonRequest("/ursuser/login.json", json, function (data) {
                  if (getUrlParam("session") == 1) {
                      history.back();
                  } else {
                      if (data.msgCode == 10007) {
                          openDiv("password_edit.html", "edit", function () {
                              $("#alert").append("初次登入請修改密碼");
                          });
                      }
                      if (data.msgCode == 10004) {
                          window.location.href = "index.html"
                      }
                  }
              }, "POST");

       

  2. 根據url查詢controller(handle):由註解@RequestMapping指定url
  3.  @RequestMapping(value = "/login.json", method = {RequestMethod.POST,
                RequestMethod.GET}, produces = "application/json;charset=UTF-8")
        public
        @ResponseBody
        Object login(@RequestBody UrsUserQuery ursUserQuery, HttpServletRequest request, HttpServletResponse httpServletResponse, HttpSession session) throws Exception {
            //update-20180614-LuoLiang-新增查詢所屬機構
            String sql = "select uu.*,ur.role_id,ur.role_desc,regexp_replace(tt.comname,'^\\d+','') as comname from t_urs_user uu " +
                    "left join t_urs_user_role uur on uur.user_id=uu.user_id " +
                    "left join t_urs_role ur on ur.role_id=uur.role_id " +
                    "left join v_pub_company tt on substr(uu.comcode,0,8) = substr(tt.comcode,0,8) " +
                    "where uu.enable='1' and ur.enable='1' and uu.user_id='%s' and uu.user_password='%s'";
            sql = String.format(sql, ursUserQuery.getUserId(), MD5Util.MD5(ursUserQuery.getUserPassword(), ""));
            Object object = commonServiceImp.find(sql);
            if (object != null) {
                Map<String, Object> ursUser = (Map<String, Object>) object;
                Object obj = ursUser.get("COMCODE");
                if (obj != null) {
                    String comcode = String.valueOf(obj);
                    if (comcode.length() > 6) {
                        ursUser.put("COMCODE", comcode.substring(0, 6));
                    }
                }
                //ursUser.put("M_PASSPORT", CognosUtil.getPassportId());
                String ursUserStr = JsonUtil.toJson(ursUser);
                LOGGER.info("{}登入成功", ursUserStr);
                logLoginServiceImp.loginlog(ursUserQuery.getUserId(), request.getRemoteAddr());
                ursUserStr = URLEncoder.encode(ursUserStr, CharEncoding.UTF_8);
                CookieUtil.addCookie(httpServletResponse, CookieKeyConstant.USER_KEY, ursUserStr);
                session.setAttribute("user_session", ursUser);
                if (ursUser.get("DESCRIPTION") == null) {
                    return new RestBean(MsgCodeConstant.MUST_EDITPASSWORD);
                }
                return new RestBean(MsgCodeConstant.LOGIN_SUCCESS);
            } else {
                throw new RestException(-1, "賬戶名或密碼錯誤");
            }
        }

    返回json:@ResponseBody會返回一個json串,將返回的數值直接返回給HTTP Response Body,通過Ajax非同步傳值,前臺會的到返回的HTTP Response Body  也就是就送串