public String intercept(ActionInvocation invocation) throws Exception { // 将一个拦截结果的监听器注册给该拦截器 invocation.addPreResultListener(new MyPreResultListener()); System.out.println("execute方法执行之前的拦截..."); // 调用下一个拦截器,或者Action的执行方法 String result = invocation.invoke(); System.out.println("execute方法执行之后的拦截..."); return result; }
/** * Discovers annotated methods on the action and calls them according to the workflow * * @see * com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation) */ public String intercept(ActionInvocation invocation) throws Exception { final Object action = invocation.getAction(); invocation.addPreResultListener(this); List<Method> methods = new ArrayList<Method>(AnnotationUtils.getAnnotatedMethods(action.getClass(), Before.class)); if (methods.size() > 0) { // methods are only sorted by priority Collections.sort( methods, new Comparator<Method>() { public int compare(Method method1, Method method2) { return comparePriorities( method1.getAnnotation(Before.class).priority(), method2.getAnnotation(Before.class).priority()); } }); for (Method m : methods) { final String resultCode = (String) m.invoke(action, (Object[]) null); if (resultCode != null) { // shortcircuit execution return resultCode; } } } String invocationResult = invocation.invoke(); // invoke any @After methods methods = new ArrayList<Method>(AnnotationUtils.getAnnotatedMethods(action.getClass(), After.class)); if (methods.size() > 0) { // methods are only sorted by priority Collections.sort( methods, new Comparator<Method>() { public int compare(Method method1, Method method2) { return comparePriorities( method1.getAnnotation(After.class).priority(), method2.getAnnotation(After.class).priority()); } }); for (Method m : methods) { m.invoke(action, (Object[]) null); } } return invocationResult; }
@Override public String intercept(ActionInvocation invocation) throws Exception { ActionContext invocationContext = invocation.getInvocationContext(); Map<String, Object> conversionErrors = invocationContext.getConversionErrors(); ValueStack stack = invocationContext.getValueStack(); HashMap<Object, Object> fakie = null; for (Map.Entry<String, Object> entry : conversionErrors.entrySet()) { String propertyName = entry.getKey(); Object value = entry.getValue(); if (shouldAddError(propertyName, value)) { String message = XWorkConverter.getConversionErrorMessage(propertyName, stack); Object action = invocation.getAction(); if (action instanceof ValidationAware) { ValidationAware va = (ValidationAware) action; va.addFieldError(propertyName, message); } if (fakie == null) { fakie = new HashMap<Object, Object>(); } fakie.put(propertyName, getOverrideExpr(invocation, value)); } } if (fakie != null) { // if there were some errors, put the original (fake) values in place right before the result stack.getContext().put(ORIGINAL_PROPERTY_OVERRIDE, fakie); invocation.addPreResultListener( new PreResultListener() { public void beforeResult(ActionInvocation invocation, String resultCode) { Map<Object, Object> fakie = (Map<Object, Object>) invocation.getInvocationContext().get(ORIGINAL_PROPERTY_OVERRIDE); if (fakie != null) { invocation.getStack().setExprOverrides(fakie); } } }); } return invocation.invoke(); }
@Override public String intercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction(); if (action instanceof ModelDriven) { ModelDriven modelDriven = (ModelDriven) action; ValueStack stack = invocation.getStack(); Object model = modelDriven.getModel(); if (model != null) { stack.push(model); } if (refreshModelBeforeResult) { invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model)); } } return invocation.invoke(); }