Struts2拦截器----AliasInterceptor分析(一)

2014-11-24 08:14:27 · 作者: · 浏览: 3

AliasInterceptor拦截器是Struts2默认拦截器中的第二个拦截器。它的作用是:给参数起一个别名,可用于在action链中以不同的名字共享同一个参数,也可用于把http请求参数以不同的名字映射到action里。也许你现在还有点疑惑,我们后面会给个例子程序。

配置

  
          
   #{'t_id':'s_id'}
         
   
     /Student/StudentAddSuccess.jsp 
   

  

你只要关注#{'t_id':'s_id'}。这就是设置别名共享同一个参数值。t_id为原名,s_id为别名。

下面以实现在action链中以不同的名字共享同一个参数为例

struts.xml:

  
        	 
   #{'t_id':'s_id'}
	 
   
     /Student/StudentAddSuccess.jsp 
   

  

  
        	
   
    Student
   

  
public class StudentAction extends ActionSupport{
	private int s_id;
	public int getS_id() {
		return s_id;
	}

	public void setS_id(int sId) {
		s_id = sId;
	}

	public String Add(){
		return SUCCESS;
	}
	
	public String Delete(){
		return SUCCESS;
	}
	
	public String execute()
	{
		System.out.println(s_id);
		return SUCCESS;
	}
}


public class TeacherAction extends ActionSupport {
	
	private int t_id;
	public int getT_id() {
		return t_id;
	}
	public void setT_id(int tId) {
		t_id = tId;
	}
	public String execute()
	{
		return SUCCESS;
	}
}

/Student/StudentAddSuccess.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s"  %>


  
    
  
  
  
   Student Add Success!! 

url:http://localhost:8080/structs2-HelloWorld01/Teacher.action t_id=21

结果:控制台输出21,页面也是21,在值栈中同时存在t_id和s_id。这就是别名的作用,共享同一个参数值。

源码分析:

com.opensymphony.xwork2.interceptor.AliasInterceptor:

@Override public String intercept(ActionInvocation invocation) throws Exception {

        ActionConfig config = invocation.getProxy().getConfig();
        ActionContext ac = invocation.getInvocationContext();
        Object action = invocation.getAction();

        // get the action's parameters
        final Map
  
    parameters = config.getParams();

        if (parameters.containsKey(aliasesKey)) {

            String aliasExpression = parameters.get(aliasesKey);
            ValueStack stack = ac.getValueStack();
            Object obj = stack.findValue(aliasExpression);

            if (obj != null && obj instanceof Map) {
                //get secure stack
                ValueStack newStack = valueStackFactory.createva lueStack(stack);
                boolean clearableStack = newStack instanceof Clearableva lueStack;
                if (clearableStack) {
                    //if the stack's context can be cleared, do that to prevent OGNL
                    //from having access to objects in the stack, see XW-641
                    ((Clearableva lueStack)newStack).clearContextValues();
                    Map
   
     context = newStack.getContext(); ReflectionContextState.setCreatingNullObjects(context, true); ReflectionContextState.setDenyMethodExecution(context, true); ReflectionContextState.setReportingConversionErrors(context, true); //keep locale from original context context.put(ActionContext.LOCALE, stack.getContext().get(ActionContext.LOCALE)); } // override Map aliases = (Map) obj; for (Object o : aliases.entrySet()) { Map.Entry entry = (Map.Entry) o; String name = entry.getKey().toString(); String alias = (String) entry.getValue(); Object value = stack.findValue(name); if (null == value) { // workaround Map
    
      contextParameters = ActionContext.getContext().getParameters(); if (null != contextParameters) { value = contextParameters.get(name); } } if (null != value) { try { newStack.setValue(alias, value); } catch (RuntimeException e) { if (devMode) { String developerNotification = LocalizedTextUtil.findText(ParametersInterceptor.class, "devmode.notification", ActionContext.getContext().getLocale(), "Developer Notification:\n{0}", new Object[]{ "Unexpected Exception caught setting '" + entry.getKey() + "' on '" + action.getClass() + ": " + e.getMessage() }); LOG.e