@Override protected ActionForward processActionPerform( HttpServletRequest request, HttpServletResponse response, Action action, ActionForm form, ActionMapping mapping) throws IOException, ServletException { // 1,获取要调用的方法 String methodName = "execute"; if (DispatchAction.class.isAssignableFrom(action.getClass())) { methodName = request.getParameter(mapping.getParameter()); if (methodName == null) { methodName = "unspecified"; } } Method method = null; try { method = action.getClass().getDeclaredMethod(methodName, parameterTypes); } catch (NoSuchMethodException e) { throw new ItcastException(e); } catch (Exception e) { throw new ItcastException(e); } // 2,得到这个方法所要求的权限 Privilege privilege = method.getAnnotation(Privilege.class); if (privilege == null) { privilege = action.getClass().getAnnotation(Privilege.class); } if (privilege != null) { // 3,查看用户是否有这个权限 User currentUser = ExecutionContext.get().getCurrentUser(); if (!PrivilegeController.isUserHasPermission( // currentUser, // privilege.resource(), // privilege.action())) { // 4,如果没有权限,抛出异常 String msg = "您没有权限【resource=" + privilege.resource() + ",action=" + privilege.action() // + "】访问方法【" + action.getClass() + "." + methodName + "】"; throw new ItcastPermissionDeniedException(msg); } } return super.processActionPerform(request, response, action, form, mapping); }
/** * Gets the <code>Action</code> specified by the mapping type from a PicoContainer. The action * will be instantiated if necessary, and its dependencies will be injected. The action will be * instantiated via a special PicoContainer that just contains actions. If this container already * exists in the request attribute, this method will use it. If no such container exists, this * method will create a new Pico container and place it in the request. The parent container will * either be the request container, or if that container can not be found the session container, * or if that container can not be found, the application container. If no parent container can be * found, a <code>PicoInitializationException</code> will be thrown. The action path specified in * the mapping is used as the component key for the action. * * @param request the Http servlet request. * @param mapping the Struts mapping object, whose type property tells us what Action class is * required. * @param servlet the Struts <code>ActionServlet</code>. * @return the <code>Action</code> instance. * @throws PicoIntrospectionException if the mapping type does not specify a valid action. * @throws PicoInitializationException if no request, session, or application scoped Pico * container can be found. */ public Action processAction( HttpServletRequest request, ActionMapping mapping, ActionServlet servlet) throws PicoIntrospectionException, PicoInitializationException { Object actionKey = mapping.getPath(); Class actionType = getActionClass(mapping.getType()); Action action = (Action) sf.getService(actionKey); if (action == null) { sf.registerService(actionKey, actionType); action = (Action) sf.getService(actionKey); } action.setServlet(servlet); return action; }
public void setServlet(ActionServlet actionServlet) { super.setServlet(actionServlet); ServletContext servletContext = actionServlet.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); this.petStore = (PetStoreFacade) wac.getBean("petStore"); }
/** * Execute the specified <code>Action</code>, and return the resulting <code>ActionForward</code>. * * @param context The <code>Context</code> for this request * @param action The <code>Action</code> to be executed * @param actionConfig The <code>ActionConfig</code> defining this action * @param actionForm The <code>ActionForm</code> (if any) for this action * @throws Exception if thrown by the <code>Action</code> */ protected ForwardConfig execute( ActionContext context, Action action, ActionConfig actionConfig, ActionForm actionForm) throws Exception { ServletActionContext saContext = (ServletActionContext) context; return (action.execute( (ActionMapping) actionConfig, actionForm, saContext.getRequest(), saContext.getResponse())); }
/** * Initialize the WebApplicationContext for this Action. Invokes onInit after successful * initialization of the context. * * @see #initWebApplicationContext * @see #onInit */ public void setServlet(ActionServlet actionServlet) { super.setServlet(actionServlet); if (actionServlet != null) { this.webApplicationContext = initWebApplicationContext(actionServlet); this.messageSourceAccessor = new MessageSourceAccessor(this.webApplicationContext); onInit(); } else { onDestroy(); } }