Struts2中的ValueStack详解

 

Struts2中的ValueStack详解

707人阅读  评论(0)  收藏  举报
  分类:

目录(?)[+]

ValueStack简述

valueStack是一个接口,在struts2中使用OGNL表达式实际上是使用实现了ValueStack接口的类OgnlValueStack.它是ValueStack的默认实现类. 首先看一下,这个接口是怎么定义的。

[java]  view plain  copy
  1. package com.opensymphony.xwork2.util;  
  2.   
  3. import java.util.Map;  
  4. public interface ValueStack {  
  5.   
  6.     public static final String VALUE_STACK = "com.opensymphony.xwork2.util.ValueStack.ValueStack";  
  7.   
  8.     public static final String REPORT_ERRORS_ON_NO_PROP = "com.opensymphony.xwork2.util.ValueStack.ReportErrorsOnNoProp";  
  9.   
  10.     public abstract Map<String, Object> getContext();  
  11.   
  12.     /** 
  13.      * Sets the default type to convert to if no type is provided when getting a value. 
  14.      * 
  15.      * @param defaultType the new default type 
  16.      */  
  17.     public abstract void setDefaultType(Class defaultType);  
  18.   
  19.     /** 
  20.      * Set a override map containing <code>key -> values</code> that takes precedent when doing find operations on the ValueStack. 
  21.      * <p/> 
  22.      * See the unit test for ValueStackTest for examples. 
  23.      * 
  24.      * @param overrides  overrides map. 
  25.      */  
  26.     public abstract void setExprOverrides(Map<Object, Object> overrides);  
  27.   
  28.     /** 
  29.      * Gets the override map if anyone exists. 
  30.      * 
  31.      * @return the override map, <tt>null</tt> if not set. 
  32.      */  
  33.     public abstract Map<Object, Object> getExprOverrides();  
  34.   
  35.     /** 
  36.      * Get the CompoundRoot which holds the objects pushed onto the stack 
  37.      * 
  38.      * @return the root 
  39.      */  
  40.     public abstract CompoundRoot getRoot();  
  41.   
  42.     /** 
  43.      * Attempts to set a property on a bean in the stack with the given expression using the default search order. 
  44.      * 
  45.      * @param expr  the expression defining the path to the property to be set. 
  46.      * @param value the value to be set into the named property 
  47.      */  
  48.     public abstract void setValue(String expr, Object value);  
  49.   
  50.     /** 
  51.      * Attempts to set a property on a bean in the stack with the given expression using the default search order. 
  52.      * N.B.: unlike #setValue(String,Object) it doesn't allow eval expression. 
  53.      * @param expr  the expression defining the path to the property to be set. 
  54.      * @param value the value to be set into the named property 
  55.      */  
  56.     void setParameter(String expr, Object value);  
  57.   
  58.     /** 
  59.      * 根据顺序查找到表达式对应的值并修改 
  60.      * 
  61.      * @param expr                    表达式 
  62.      * @param value                   值 
  63.      * @param throwExceptionOnFailure 如果没有找到抛出的异常 
  64.      */  
  65.     public abstract void setValue(String expr, Object value, boolean throwExceptionOnFailure);  
  66.   
  67.     public abstract String findString(String expr);  
  68.     public abstract String findString(String expr, boolean throwExceptionOnFailure);  
  69.   
  70.     /** 
  71.      * 按照默认的顺序根据表达式获取值 
  72.      */  
  73.     public abstract Object findValue(String expr);  
  74.     public abstract Object findValue(String expr, boolean throwExceptionOnFailure);  
  75.     public abstract Object findValue(String expr, Class asType);  
  76.     public abstract Object findValue(String expr, Class asType,  boolean throwExceptionOnFailure);  
  77.   
  78.     /** 
  79.      * 获取栈顶元素,不改变栈 
  80.      */  
  81.     public abstract Object peek();  
  82.   
  83.     /** 
  84.      * 获取栈顶元素并将它移除出栈 
  85.      */  
  86.     public abstract Object pop();  
  87.   
  88.     /** 
  89.      * 将这个值放到栈顶 
  90.      */  
  91.     public abstract void push(Object o);  
  92.   
  93.     /** 
  94.      * 修改值栈中键为key的值 
  95.      */  
  96.     public abstract void set(String key, Object o);  
  97.   
  98.     /** 
  99.      * 获取栈中的对象的数量 
  100.      */  
  101.     public abstract int size();  
  102.   
  103. }  

可以看到,这个接口定义了许多的抽象方法以及方法。就是对值栈中的数据进行相应的操作。

它的默认实现类是OgnlValueStack,查看一下OgnlValueStack的源代码:

[java]  view plain  copy
  1. package com.opensymphony.xwork2.ognl;  
  2.   
  3. import com.opensymphony.xwork2.ActionContext;  
  4. import com.opensymphony.xwork2.TextProvider;  
  5. import com.opensymphony.xwork2.XWorkConstants;  
  6. import com.opensymphony.xwork2.XWorkException;  
  7. import com.opensymphony.xwork2.conversion.impl.XWorkConverter;  
  8. import com.opensymphony.xwork2.inject.Container;  
  9. import com.opensymphony.xwork2.inject.Inject;  
  10. import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;  
  11. import com.opensymphony.xwork2.util.ClearableValueStack;  
  12. import com.opensymphony.xwork2.util.CompoundRoot;  
  13. import com.opensymphony.xwork2.util.MemberAccessValueStack;  
  14. import com.opensymphony.xwork2.util.ValueStack;  
  15. import com.opensymphony.xwork2.util.logging.Logger;  
  16. import com.opensymphony.xwork2.util.logging.LoggerFactory;  
  17. import com.opensymphony.xwork2.util.logging.LoggerUtils;  
  18. import com.opensymphony.xwork2.util.reflection.ReflectionContextState;  
  19. import ognl.*;  
  20.   
  21. import java.io.Serializable;  
  22. import java.util.HashMap;  
  23. import java.util.Map;  
  24. import java.util.Set;  
  25. import java.util.regex.Pattern;  
  26.   
  27. public class OgnlValueStack implements Serializable, ValueStack, ClearableValueStack, MemberAccessValueStack {  
  28.   
  29.     public static final String THROW_EXCEPTION_ON_FAILURE = OgnlValueStack.class.getName() + ".throwExceptionOnFailure";  
  30.   
  31.     private static final long serialVersionUID = 370737852934925530L;  
  32.   
  33.     private static final String MAP_IDENTIFIER_KEY = "com.opensymphony.xwork2.util.OgnlValueStack.MAP_IDENTIFIER_KEY";  
  34.     private static final Logger LOG = LoggerFactory.getLogger(OgnlValueStack.class);  
  35.       
  36.     //CompoundRoot继承了ArraryList,提供了额外的方法:push()和pop()方法,所以它相当于栈结构  
  37.     <span style="color:#FF0000;">CompoundRoot root;  
  38.     transient Map<String, Object> context;  
  39.     Class defaultType;  
  40.     Map<Object, Object> overrides;  
  41.     transient OgnlUtil ognlUtil;  
  42.     transient SecurityMemberAccess securityMemberAccess;</span>  
  43.   
  44.     private boolean devMode;  
  45.     private boolean logMissingProperties;  
  46.   
  47.     protected OgnlValueStack(XWorkConverter xworkConverter, CompoundRootAccessor accessor, TextProvider prov, boolean allowStaticAccess) {  
  48.         setRoot(xworkConverter, accessor, new CompoundRoot(), allowStaticAccess);  
  49.         push(prov);  
  50.     }  
  51.   
  52.     protected OgnlValueStack(ValueStack vs, XWorkConverter xworkConverter, CompoundRootAccessor accessor, boolean allowStaticAccess) {  
  53.         setRoot(xworkConverter, accessor, new CompoundRoot(vs.getRoot()), allowStaticAccess);  
  54.     }  
  55.   
  56.     @Inject  
  57.     public void setOgnlUtil(OgnlUtil ognlUtil) {  
  58.         this.ognlUtil = ognlUtil;  
  59.     }  
  60.   
  61.     protected void setRoot(XWorkConverter xworkConverter, CompoundRootAccessor accessor, CompoundRoot compoundRoot,  
  62.                            boolean allowStaticMethodAccess) {  
  63.         this.root = compoundRoot;  
  64.         this.securityMemberAccess = new SecurityMemberAccess(allowStaticMethodAccess);  
  65.         this.context = Ognl.createDefaultContext(this.root, accessor, new OgnlTypeConverterWrapper(xworkConverter), securityMemberAccess);  
  66.         context.put(VALUE_STACK, this);  
  67.         Ognl.setClassResolver(context, accessor);  
  68.         ((OgnlContext) context).setTraceEvaluations(false);  
  69.         ((OgnlContext) context).setKeepLastEvaluation(false);  
  70.     }  
  71.   
  72.     @Inject(XWorkConstants.DEV_MODE)  
  73.     public void setDevMode(String mode) {  
  74.         devMode = "true".equalsIgnoreCase(mode);  
  75.     }  
  76.   
  77.     @Inject(value = "logMissingProperties", required = false)  
  78.     public void setLogMissingProperties(String logMissingProperties) {  
  79.         this.logMissingProperties = "true".equalsIgnoreCase(logMissingProperties);  
  80.     }  
  81.   
  82.     /** 
  83.      * @see com.opensymphony.xwork2.util.ValueStack#getContext() 
  84.      */  
  85.     public Map<String, Object> getContext() {  
  86.         return context;  
  87.     }  
  88.   
  89.     /** 
  90.      * @see com.opensymphony.xwork2.util.ValueStack#setDefaultType(java.lang.Class) 
  91.      */  
  92.     public void setDefaultType(Class defaultType) {  
  93.         this.defaultType = defaultType;  
  94.     }  
  95.   
  96.     /** 
  97.      * @see com.opensymphony.xwork2.util.ValueStack#setExprOverrides(java.util.Map) 
  98.      */  
  99.     public void setExprOverrides(Map<Object, Object> overrides) {  
  100.         if (this.overrides == null) {  
  101.             this.overrides = overrides;  
  102.         } else {  
  103.             this.overrides.putAll(overrides);  
  104.         }  
  105.     }  
  106.   
  107.     /** 
  108.      * @see com.opensymphony.xwork2.util.ValueStack#getExprOverrides() 
  109.      */  
  110.     public Map<Object, Object> getExprOverrides() {  
  111.         return this.overrides;  
  112.     }  
  113.   
  114.     /** 
  115.      * @see com.opensymphony.xwork2.util.ValueStack#getRoot() 
  116.      */  
  117.     public CompoundRoot getRoot() {  
  118.         return root;  
  119.     }  
  120.   
  121.     /** 
  122.      * @see com.opensymphony.xwork2.util.ValueStack#setParameter(String, Object) 
  123.      */  
  124.     public void setParameter(String expr, Object value) {  
  125.         setValue(expr, value, devMode, false);  
  126.     }  
  127.   
  128.     /** 
  129.  
  130.     /** 
  131.      * @see com.opensymphony.xwork2.util.ValueStack#setValue(java.lang.String, java.lang.Object) 
  132.      */  
  133.     public void setValue(String expr, Object value) {  
  134.         setValue(expr, value, devMode);  
  135.     }  
  136.   
  137.     /** 
  138.      * @see com.opensymphony.xwork2.util.ValueStack#setValue(java.lang.String, java.lang.Object, boolean) 
  139.      */  
  140.     public void setValue(String expr, Object value, boolean throwExceptionOnFailure) {  
  141.         setValue(expr, value, throwExceptionOnFailure, true);  
  142.     }  
  143.   
  144.     private void setValue(String expr, Object value, boolean throwExceptionOnFailure, boolean evalExpression) {  
  145.         Map<String, Object> context = getContext();  
  146.         try {  
  147.             trySetValue(expr, value, throwExceptionOnFailure, context, evalExpression);  
  148.         } catch (OgnlException e) {  
  149.             handleOgnlException(expr, value, throwExceptionOnFailure, e);  
  150.         } catch (RuntimeException re) { //XW-281  
  151.             handleRuntimeException(expr, value, throwExceptionOnFailure, re);  
  152.         } finally {  
  153.             cleanUpContext(context);  
  154.         }  
  155.     }  
  156.   
  157.     private void trySetValue(String expr, Object value, boolean throwExceptionOnFailure, Map<String, Object> context, boolean evalExpression) throws OgnlException {  
  158.         context.put(XWorkConverter.CONVERSION_PROPERTY_FULLNAME, expr);  
  159.         context.put(REPORT_ERRORS_ON_NO_PROP, (throwExceptionOnFailure) ? Boolean.TRUE : Boolean.FALSE);  
  160.         ognlUtil.setValue(expr, context, root, value, evalExpression);  
  161.     }  
  162.   
  163.     private void cleanUpContext(Map<String, Object> context) {  
  164.         ReflectionContextState.clear(context);  
  165.         context.remove(XWorkConverter.CONVERSION_PROPERTY_FULLNAME);  
  166.         context.remove(REPORT_ERRORS_ON_NO_PROP);  
  167.     }  
  168.   
  169.     private void handleRuntimeException(String expr, Object value, boolean throwExceptionOnFailure, RuntimeException re) {  
  170.         if (throwExceptionOnFailure) {  
  171.             String message = ErrorMessageBuilder.create()  
  172.                     .errorSettingExpressionWithValue(expr, value)  
  173.                     .build();  
  174.             throw new XWorkException(message, re);  
  175.         } else {  
  176.             if (LOG.isWarnEnabled()) {  
  177.                 LOG.warn("Error setting value", re);  
  178.             }  
  179.         }  
  180.     }  
  181.   
  182.     private void handleOgnlException(String expr, Object value, boolean throwExceptionOnFailure, OgnlException e) {  
  183.         String msg = "Error setting expression '" + expr + "' with value '" + value + "'";  
  184.         if (LOG.isWarnEnabled()) {  
  185.             LOG.warn(msg, e);  
  186.         }  
  187.         if (throwExceptionOnFailure) {  
  188.             throw new XWorkException(msg, e);  
  189.         }  
  190.     }  
  191.   
  192.     /** 
  193.      * @see com.opensymphony.xwork2.util.ValueStack#findString(java.lang.String) 
  194.      */  
  195.     public String findString(String expr) {  
  196.         return (String) findValue(expr, String.class);  
  197.     }  
  198.   
  199.     public String findString(String expr, boolean throwExceptionOnFailure) {  
  200.         return (String) findValue(expr, String.class, throwExceptionOnFailure);  
  201.     }  
  202.   
  203.     /** 
  204.      * @see com.opensymphony.xwork2.util.ValueStack#findValue(java.lang.String) 
  205.      */  
  206.     public Object findValue(String expr, boolean throwExceptionOnFailure) {  
  207.         try {  
  208.             setupExceptionOnFailure(throwExceptionOnFailure);  
  209.             return tryFindValueWhenExpressionIsNotNull(expr);  
  210.         } catch (OgnlException e) {  
  211.             return handleOgnlException(expr, throwExceptionOnFailure, e);  
  212.         } catch (Exception e) {  
  213.             return handleOtherException(expr, throwExceptionOnFailure, e);  
  214.         } finally {  
  215.             ReflectionContextState.clear(context);  
  216.         }  
  217.     }  
  218.   
  219.     private void setupExceptionOnFailure(boolean throwExceptionOnFailure) {  
  220.         if (throwExceptionOnFailure) {  
  221.             context.put(THROW_EXCEPTION_ON_FAILURE, true);  
  222.         }  
  223.     }  
  224.   
  225.     private Object tryFindValueWhenExpressionIsNotNull(String expr) throws OgnlException {  
  226.         if (expr == null) {  
  227.             return null;  
  228.         }  
  229.         return tryFindValue(expr);  
  230.     }  
  231.   
  232.     private Object handleOtherException(String expr, boolean throwExceptionOnFailure, Exception e) {  
  233.         logLookupFailure(expr, e);  
  234.   
  235.         if (throwExceptionOnFailure)  
  236.             throw new XWorkException(e);  
  237.   
  238.         return findInContext(expr);  
  239.     }  
  240.   
  241.     private Object tryFindValue(String expr) throws OgnlException {  
  242.         Object value;  
  243.         expr = lookupForOverrides(expr);  
  244.         if (defaultType != null) {  
  245.             value = findValue(expr, defaultType);  
  246.         } else {  
  247.             value = getValueUsingOgnl(expr);  
  248.             if (value == null) {  
  249.                 value = findInContext(expr);  
  250.             }  
  251.         }  
  252.         return value;  
  253.     }  
  254.   
  255.     private String lookupForOverrides(String expr) {  
  256.         if ((overrides != null) && overrides.containsKey(expr)) {  
  257.             expr = (String) overrides.get(expr);  
  258.         }  
  259.         return expr;  
  260.     }  
  261.   
  262.     private Object getValueUsingOgnl(String expr) throws OgnlException {  
  263.         try {  
  264.             return ognlUtil.getValue(expr, context, root);  
  265.         } finally {  
  266.             context.remove(THROW_EXCEPTION_ON_FAILURE);  
  267.         }  
  268.     }  
  269.   
  270.     public Object findValue(String expr) {  
  271.         return findValue(expr, false);  
  272.     }  
  273.   
  274.     /** 
  275.      * @see com.opensymphony.xwork2.util.ValueStack#findValue(java.lang.String, java.lang.Class) 
  276.      */  
  277.     public Object findValue(String expr, Class asType, boolean throwExceptionOnFailure) {  
  278.         try {  
  279.             setupExceptionOnFailure(throwExceptionOnFailure);  
  280.             return tryFindValueWhenExpressionIsNotNull(expr, asType);  
  281.         } catch (OgnlException e) {  
  282.             return handleOgnlException(expr, throwExceptionOnFailure, e);  
  283.         } catch (Exception e) {  
  284.             return handleOtherException(expr, throwExceptionOnFailure, e);  
  285.         } finally {  
  286.             ReflectionContextState.clear(context);  
  287.         }  
  288.     }  
  289.   
  290.     private Object tryFindValueWhenExpressionIsNotNull(String expr, Class asType) throws OgnlException {  
  291.         if (expr == null) {  
  292.             return null;  
  293.         }  
  294.         return tryFindValue(expr, asType);  
  295.     }  
  296.   
  297.     private Object handleOgnlException(String expr, boolean throwExceptionOnFailure, OgnlException e) {  
  298.         Object ret = findInContext(expr);  
  299.         if (ret == null) {  
  300.             if (shouldLogNoSuchPropertyWarning(e)) {  
  301.                 LOG.warn("Could not find property [" + ((NoSuchPropertyException) e).getName() + "]");  
  302.             }  
  303.             if (throwExceptionOnFailure) {  
  304.                 throw new XWorkException(e);  
  305.             }  
  306.         }  
  307.         return ret;  
  308.     }  
  309.   
  310.     private boolean shouldLogNoSuchPropertyWarning(OgnlException e) {  
  311.         return e instanceof NoSuchPropertyException && devMode && logMissingProperties;  
  312.     }  
  313.   
  314.     private Object tryFindValue(String expr, Class asType) throws OgnlException {  
  315.         Object value = null;  
  316.         try {  
  317.             expr = lookupForOverrides(expr);  
  318.             value = getValue(expr, asType);  
  319.             if (value == null) {  
  320.                 value = findInContext(expr);  
  321.             }  
  322.         } finally {  
  323.             context.remove(THROW_EXCEPTION_ON_FAILURE);  
  324.         }  
  325.         return value;  
  326.     }  
  327.   
  328.     private Object getValue(String expr, Class asType) throws OgnlException {  
  329.         return ognlUtil.getValue(expr, context, root, asType);  
  330.     }  
  331.   
  332.     private Object findInContext(String name) {  
  333.         return getContext().get(name);  
  334.     }  
  335.   
  336.     public Object findValue(String expr, Class asType) {  
  337.         return findValue(expr, asType, false);  
  338.     }  
  339.   
  340.     /** 
  341.      * Log a failed lookup, being more verbose when devMode=true. 
  342.      * 
  343.      * @param expr The failed expression 
  344.      * @param e    The thrown exception. 
  345.      */  
  346.     private void logLookupFailure(String expr, Exception e) {  
  347.         String msg = LoggerUtils.format("Caught an exception while evaluating expression '#0' against value stack", expr);  
  348.         if (devMode && LOG.isWarnEnabled()) {  
  349.             LOG.warn(msg, e);  
  350.             LOG.warn("NOTE: Previous warning message was issued due to devMode set to true.");  
  351.         } else if (LOG.isDebugEnabled()) {  
  352.             LOG.debug(msg, e);  
  353.         }  
  354.     }  
  355.   
  356.     /** 
  357.      * @see com.opensymphony.xwork2.util.ValueStack#peek() 
  358.      */  
  359.     public Object peek() {  
  360.         return root.peek();  
  361.     }  
  362.   
  363.     /** 
  364.      * @see com.opensymphony.xwork2.util.ValueStack#pop() 
  365.      */  
  366.     public Object pop() {  
  367.         return root.pop();  
  368.     }  
  369.   
  370.     /** 
  371.      * @see com.opensymphony.xwork2.util.ValueStack#push(java.lang.Object) 
  372.      */  
  373.     public void push(Object o) {  
  374.         root.push(o);  
  375.     }  
  376.   
  377.     /** 
  378.      * @see com.opensymphony.xwork2.util.ValueStack#set(java.lang.String, java.lang.Object) 
  379.      */  
  380.     public void set(String key, Object o) {  
  381.         //set basically is backed by a Map pushed on the stack with a key being put on the map and the Object being the value  
  382.         Map setMap = retrieveSetMap();  
  383.         setMap.put(key, o);  
  384.     }  
  385.   
  386.     private Map retrieveSetMap() {  
  387.         Map setMap;  
  388.         Object topObj = peek();  
  389.         if (shouldUseOldMap(topObj)) {  
  390.             setMap = (Map) topObj;  
  391.         } else {  
  392.             setMap = new HashMap();  
  393.             setMap.put(MAP_IDENTIFIER_KEY, "");  
  394.             push(setMap);  
  395.         }  
  396.         return setMap;  
  397.     }  
  398.   
  399.     /** 
  400.      * check if this is a Map put on the stack  for setting if so just use the old map (reduces waste) 
  401.      */  
  402.     private boolean shouldUseOldMap(Object topObj) {  
  403.         return topObj instanceof Map && ((Map) topObj).get(MAP_IDENTIFIER_KEY) != null;  
  404.     }  
  405.   
  406.     /** 
  407.      * @see com.opensymphony.xwork2.util.ValueStack#size() 
  408.      */  
  409.     public int size() {  
  410.         return root.size();  
  411.     }  
  412.   
  413.     private Object readResolve() {  
  414.         // TODO: this should be done better  
  415.         ActionContext ac = ActionContext.getContext();  
  416.         Container cont = ac.getContainer();  
  417.         XWorkConverter xworkConverter = cont.getInstance(XWorkConverter.class);  
  418.         CompoundRootAccessor accessor = (CompoundRootAccessor) cont.getInstance(PropertyAccessor.class, CompoundRoot.class.getName());  
  419.         TextProvider prov = cont.getInstance(TextProvider.class"system");  
  420.         boolean allow = "true".equals(cont.getInstance(String.class"allowStaticMethodAccess"));  
  421.         OgnlValueStack aStack = new OgnlValueStack(xworkConverter, accessor, prov, allow);  
  422.         aStack.setOgnlUtil(cont.getInstance(OgnlUtil.class));  
  423.         aStack.setRoot(xworkConverter, accessor, this.root, allow);  
  424.   
  425.         return aStack;  
  426.     }  
  427.   
  428.   
  429.     public void clearContextValues() {  
  430.         //this is an OGNL ValueStack so the context will be an OgnlContext  
  431.         //it would be better to make context of type OgnlContext  
  432.         ((OgnlContext) context).getValues().clear();  
  433.     }  
  434.   
  435.     public void setAcceptProperties(Set<Pattern> acceptedProperties) {  
  436.         securityMemberAccess.setAcceptProperties(acceptedProperties);  
  437.     }  
  438.   
  439.     public void setPropertiesJudge(PropertiesJudge judge) {  
  440.         securityMemberAccess.setPropertiesJudge(judge);  
  441.     }  
  442.   
  443.     public void setExcludeProperties(Set<Pattern> excludeProperties) {  
  444.         securityMemberAccess.setExcludeProperties(excludeProperties);  
  445.     }  
  446.   
  447. }  
由这个源代码的红色部分,发现值栈中的数据分俩部分村储:root(栈结构,CompoundRoot)和context(map形式,OgnlContext):


ValueStack的生命周期

ValueStack的是保存着request的请求域中的,因此它的生命周期与request的生命周期是相同的。当Struts2接收到一个请求时,会迅速创建ActionContext、Action、ValueStack,然后将Action存放到ValueStack中,所以Action中的所有实例变量都是可以通过OGNL表达式访问的。当请求来的时候,Action,ValueStack的生命周期开始。当请求结束的时候,生命周期结束。

ValueStack的传送带机制

Struts2的Action类通过属性就可以获得所有相关的值,如请求参数,action配置参数,向其他Action传递属性值等。要获得这些值,我们需要做的就是在Action类中声明与参数同名的属性,在Action执行默认的execute方法之前,Struts2就为Action中的属性进行了赋值。要完成这个功能很大程度上依赖于ValueStack对象。当 Struts 2接收到一个.action的请求后,会先建立Action类的对象实例,并且将Action类的对象实例压入ValueStack对象中(实际 上,ValueStack对于相当一个栈),而ValueStack类的setValue和findValue方法可以设置和获得Action对象的属性 值。Struts 2中的某些拦截器正是通过ValueStack类的setValue方法来修改Action类的属性值的。如params拦截器用于将请求参数值映射到相 应成Action类的属性值。在params拦截器中在获得请求参数值后,会使用setValue方法设置相应的Action类的属性。从这一点可以看出,ValueStack对象就象一个传送带,当客户端请求.action时,Struts 2在创建相应用Action对象后就将Action对象放到了ValueStack传送带上,然后ValueStack传送带会带着Action对象经过 若干拦截器,在每一拦截器中都可以通过ValueStack对象设置和获得Action对象中的属性值。实际上,这些拦截器就相当于流水线作业。如果要对 Action对象进行某项加工,再加一个拦截器即可,当不需要进行这项工作时,直接将该拦截器去掉即可。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值