ActionMappingParametersInteceptor拦截器处于defaultStack第十四的位置,该拦截器继承自ParametersInterceptor,也是用于把参数设置到ValueStack中,只不过该拦截器的参数是来源于ActionMapping中,而不是来自请求参数。往ValueStack中设置值的逻辑与ParametersInterceptor一模一样,正是这个原因才把ParametersInterceptor拦截器放在了该拦截器之前讲解。大家可以发现该拦截器只是覆盖了ParametersInterceptor的两个方法:
@Override protected Map<String, Object> retrieveParameters(ActionContext ac) { //获取AcitonMapping对象 ActionMapping mapping = (ActionMapping) ac.get(ServletActionContext.ACTION_MAPPING); if (mapping != null) { return mapping.getParams();//获取AcitonMapping参数 } else { return Collections.emptyMap(); } } @Override//将参数设置到ActionContext的parameters Map中 protected void addParametersToContext(ActionContext ac, Map newParams) { //获取请求参数 Map previousParams = ac.getParameters(); Map combinedParams = null; if (previousParams != null) { combinedParams = new TreeMap(previousParams); } else { combinedParams = new TreeMap(); } //newParams为ActionMapping中的经过了合法性检查后的合法参数 combinedParams.putAll(newParams); //将请求参数与合法参数一起设置到ActionContext的parameters Map中 ac.setParameters(combinedParams); }
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { //省略... ActionMapping mapping = prepare.findActionMapping(request, response, true);//调用prepare对象的findActionMapping //省略... }
public ActionMapping findActionMapping(HttpServletRequest request, HttpServletResponse response, boolean forceLookup) { ActionMapping mapping = (ActionMapping) request.getAttribute(STRUTS_ACTION_MAPPING_KEY); if (mapping == null || forceLookup) { try { //去容器中找ActionMapper对象,并调用ActionMapper对象的getMapping方法返回ActionMapping对象 mapping = dispatcher.getContainer().getInstance(ActionMapper.class).getMapping(request, dispatcher.getConfigurationManager()); if (mapping != null) { request.setAttribute(STRUTS_ACTION_MAPPING_KEY, mapping); } } catch (Exception ex) { dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex); } } return mapping; }
public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) { ActionMapping mapping = new ActionMapping();//新建一个ActionMapping对象 String uri = getUri(request);//获取URI int indexOfSemicolon = uri.indexOf(";"); uri = (indexOfSemicolon > -1) ? uri.substring(0, indexOfSemicolon) : uri;//处理 URI uri = dropExtension(uri, mapping);//去掉URI后缀 if (uri == null) { return null; } //解析Action的namespace与name parseNameAndNamespace(uri, mapping, configManager); //处理特殊参数 handleSpecialParameters(request, mapping); if (mapping.getName() == null) { return null; } //解析ActionName,因为struts2可以支持动态方法调用,如:xxxAction!login parseActionName(mapping); return mapping; }