关于Struts2.0 标签中采用%{}%的处理原理

   项目中采用struts2.0时,常常使用计算表单式的处理源码原理解析:

例如:

 <s:set var="hasDeductAmount" value="%{bill.supplier.moneyPool>#amount?#amount:bill.supplier.moneyPool}"> </s:set>

 

 

上面的绿色为:value中值的表达式表示的值栈表达式为:去除%{}的表达式

 

bill.supplier.moneyPool>#amount?#amount:bill.supplier.moneyPool

备注: (1)原理看下面的源代码即可理解:

          (2)#表示OGNL的表达式
  

<input name="billCon.amount" type="text" class="inputbg" value="<s:property value='#hasDeductAmount'/>" />

 

 

 

 

Struts2.0中的每一个标签的都是以组件形式展示:

 

Component源代码如下:


 /**
     * Finds a value from the OGNL stack based on the given expression.
     * Will always evaluate <code>expr</code> against stack except when <code>expr</code>
     * is null. If altsyntax (%{...}) is applied, simply strip it off.
     *
     * @param expr  the expression. Returns <tt>null</tt> if expr is null.
     * @return the value, <tt>null</tt> if not found.
     */
    protected Object findValue(String expr) {
        if (expr == null) {
            return null;
        }

        //获取OGNL的表达式

        expr = stripExpressionIfAltSyntax(expr);

        //从值栈获取表达式的数据对象

        return getStack().findValue(expr, throwExceptionOnELFailure);
    }

 

    /**
     * If altsyntax (%{...}) is applied, simply strip the "%{" and "}" off.
     * @param expr the expression (must be not null)
     * @return the stripped expression if altSyntax is enabled. Otherwise
     * the parameter expression is returned as is.
     */
 protected String stripExpressionIfAltSyntax(String expr) {
  return stripExpressionIfAltSyntax(stack, expr);
 }
 
    /**
     * If altsyntax (%{...}) is applied, simply strip the "%{" and "}" off.
     * @param stack the ValueStack where the context value is searched for.
     * @param expr the expression (must be not null)
     * @return the stripped expression if altSyntax is enabled. Otherwise
     * the parameter expression is returned as is.
     */
 public static String stripExpressionIfAltSyntax(ValueStack stack, String expr) {
  if (altSyntax(stack)) {
            // does the expression start with %{ and end with }? if so, just cut it off!
            if (expr.startsWith("%{") && expr.endsWith("}")) {
                return expr.substring(2, expr.length() - 1);
            }
        }
  return expr;
 }

你可能感兴趣的:(struts2)