一个AJAX+Spring MVC程序的基本原理

程序功能描述:在页面上点击一个“Disable Account”按钮,后台实现Disable某个账号,程序原理如下:

  1. 首先在页面上点击按钮,该按钮的ID是“disableAccountBtn”,找出页面加载的JS文件,在文件中搜索出该ID,发现如下代码:
    $('#disableAccountBtn').click(function() {
    $('#promptWinYes').unbind('click').click(function() {
    $('#promptWin').dialog('close');
    $.getJSON(DISABLE_USER_ACTION, {
    uid : clientId
    }, function(data) {
    switchButtonVisible(false);
    showAlertWin('Warning', DISABLE_USER_CALLBACK_MSG);
    });
    });
    showPromptWin('Warning', DISABLE_USER_MSG);
    }); 
     其中$.getJSON来调用并获取远程的JSON字符串,将其转换为JSON对象,如果成功,则执行回调函数,再从JS中查找DISABLE_USER_ACTION="../disableUser.action. 可见这个调用最终执行了
    ../disableUser.action?uid=userID这个service
  2. 查找后台JAVA代码,在UserManagementController.java中找到如下方法:
    @RequestMapping("/disableUser.action")
    	public ModelAndView disableUser(HttpServletRequest request)
    			throws Exception {
    		return WorkflowController.disableClient(request, clientAction);
    	}
     此处用的是Spring MVC的框架,这个方法用到了clientAction
  3. 再进入WorkflowController.disableClient(request, clientAction)
     public static ModelAndView disableClient(HttpServletRequest httpReq, ClientAction clientAction) {
            String clientId = httpReq.getParameter("uid");
            UserBean sales = resolveSales(httpReq);
    
            String methodId = "{" + sales.getId() + "," + clientId + "," + System.currentTimeMillis() + "}";
            logger.info("/disableUser.action BEGIN " + methodId);
    
            boolean success = true;
            String errorMsg = null;
            try {
                clientAction.disableClient(clientId, sales);
            } catch (Throwable e) {
                logger.error("Failed to disable user " + methodId, e);
                success = false;
                errorMsg = "Server Internal Error. Please contact the system Admin.";
            } finally {
                logger.info("/disableUser.action END " + methodId);
            }
    
            ModelMap model = new ModelMap();
            model.addAttribute("success", success);
            model.addAttribute("errorMsg", errorMsg);
            return new ModelAndView("results", model);
        }
     可见,这个service执行成功后,返回的字符串是
    {"errorMsg":null,"success":true}
  4. 进入clientAction.disableClient(clientId, sales)方法,ClientAction里面有很多方法:
     /**
         * Action to disable an existing client.
         * 
         * @param clientId
         * @param currentUser
         * @throws Exception
         */
        public void disableClient(String clientId, UserBean currentUser) throws Exception {
        	umUserService.createESFDataIfMissing(clientId);
            UserProfile oldUser = umUserService.getUserProfile(clientId);
            disableClient(currentUser, oldUser);
        }
     
  5. 再进入dsiableClient:
     void disableClient(UserBean salesUserBean, UserProfile clientUserProfile) throws Exception {
            if (salesUserBean == null || clientUserProfile == null) {
                return;
            }
            String clientId = clientUserProfile.getUid();
            String clientName = clientUserProfile.getDisplayName();
            UserRequestEntity userEntity = new UserRequestEntity();
            userEntity.setTitle(UMTConstants.USER_PROFILE_TITLE + "(Disable)");
            userEntity.setClientId(clientId);
            userEntity.setClientName(clientName);
            userEntity.setSalesId(salesUserBean.getId());
            userEntity.setSalesName(salesUserBean.getName());
    
            //restore old user profile
            try {
                userEntity.setProfile(JsonUtil.write(clientUserProfile));
            } catch (Exception e) {
                logger.error("Failed to encode user profile into JSON.", e);
            }
    
            umWorkflow.startDisableClientWorkflow(userEntity, UMTConstants.WF_DISABLE_CLIENT);
        } 
  6. 最后调用通用的startDisableClientWorkflow()方法:
     /**
         * Start the workflow to disable an existing client.
         * 
         * @param userEntity the user profile provided by front-end, cannot be null
         */
        public void startDisableClientWorkflow(UserRequestEntity userEntity, String workflowName) throws Exception {
            //1) save records into request tables
            UserRequestEntityHelper.validatePV(userEntity);
            UMRequestEntity umEntity = requestTracker.save(userEntity);
            Long requestId = umEntity.getId();
            logger.info("Saved request entity of [disable] for {" + userEntity.getClientId() + "} as " + requestId);
    
            //2) update request status
            umEntity.setStatus(UMRequestEntity.Status.ESF_DISABLE);
            startWorkflow(requestId, workflowName, umEntity);
        }
     startWorkflow():
     /**
         * Start workflow for request.
         * 
         * @param requestId
         * @param workflowName
         * @param umEntity
         * @throws Exception
         */
        public void startWorkflow(Long requestId, String workflowName, UMRequestEntity umEntity) throws Exception {
            logger.info("Start workflow " + workflowName + " for request [" + requestId + "]");
    
            //1) get workflow and start the process instance
            UMWorkflowBean workflowBean = workflowRepository.findUMWorkflowByName(workflowName);
            String processId = workflowService.startProcessInstance(workflowBean.getProcessName(), new HashMap<String, String>());
    
            //2) update request status
            if (umEntity == null) {
                umEntity = new UMRequestEntity();
                umEntity.setStatus(UserRequestEntity.Status.PROCESSING);
                umEntity.setStatusFlag(0);
            }
            umEntity.setId(requestId);
            umEntity.setWorkflowId(workflowBean.getWorkflowId());
            umEntity.setProcessId(processId);
            requestTracker.update(umEntity);
            logger.info("Started workflow for request [" + requestId + "]");
    
            //3) transit to first state
            Set<String> nextStates = workflowService.findActiveStates(processId);
            StateHandler nextStateHandler = null;
            for (String nextState : nextStates) {
                nextStateHandler = handlerRepository.getByState(nextState);
                if (nextStateHandler != null) {
                    break;
                }
            }
            if (nextStateHandler != null) {
                nextStateHandler.execute(requestId);   
            }
        }
    }
  7.  找到WorkFlowService的实现类对应的方法:
     /**
         * Start the process instance.
         * 
         */
        public String startProcessInstance(String processKey, Map<String, String> variables) {
            ProcessInstance processInstance = executionService.startProcessInstanceByKey(processKey, variables);
            return processInstance.getId();
        }
     ExecutionService这个类属于JBPM,全称是Java Business Process Management,是一种基于J2EE的轻量级工作流管理系统。

总结:这类AJAX+Spring MVC的程序基本原理就是前台JS代码去调用后台的Service.测试的时候打开Fidder或者Firebug就能捕捉到具体的service已经传递的参数

接下去是真证的Disable逻辑:

  1. 在ESFServiceImpl里有一个disableUser()方法:
    public void disableUser(String operator, String userId) throws Exception {
    		//		Map<String, String> userESFAttrMap = queryUserESFInfo(userId);
    		//		if (userESFAttrMap == null) {
    		//			throw new Exception("Disable user error. Can not find user: "
    		//					+ userId);
    		//		}
    		//		if (ESFConstans.STATUS_ENABLED.equals(userESFAttrMap
    		//				.get(ESFConstans.CLIENT_USERS_ATTR.STATUS))) {
    		//			changeUserStatus(operator, userId);
    		//		}
    		Connection con = null;
    		CallableStatement st = null;
    		ResultSet rs = null;
    		try {
    			con = getConnection();
    			st = con.prepareCall(ESFConstans.CALL_DISABLE_USER);
    			st.setString(1, userId);
    			st.setString(2, operator);
    			st.execute();
    		} catch (SQLException e) {
    			throw e;
    		} catch (Exception e) {
    			throw e;
    		} finally {
    			closeAllDBResources(rs, st, con);
    		}
    	}
     其中使用CallableStatement调用了存储过程ESFConstans.CALL_DISABLE_USER:
    public static final String CALL_DISABLE_USER = "call "
    		+ schema
    		+ "cdc_rules_gmo_serve."
    		+ profix
    		+ "set_status_v2(?,0,?)";
     

猜你喜欢

转载自lijingshou.iteye.com/blog/1964186