---
title: "关于Struts2.0 标签中采用%{} 的处理原理"
author: "conkeyn"
date: "2011-01-09T20:57:35Z"
tags: []
categories: ["企业架构"]
slug: "struts2-0"
draft: false
lastmod: "2011-02-19T01:09:23Z"
id: 866499
url: "https://conkeyn.iteye.com/blog/866499"
---

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

例如：

 &lt;s:set var="hasDeductAmount" value =" %{bill.supplier.moneyPool&gt;#amount?#amount:bill.supplier.moneyPool}"&gt; &lt;/s:set&gt;

上面的绿色为：value中值的表达式表示的值栈表达式为：去除%{}的表达式  
bill.supplier.moneyPool&gt;#amount?#amount:bill.supplier.moneyPool  
备注： （1）原理看下面的源代码即可理解：  
           （2）#表示OGNL的表达式  
   
&lt;input name="billCon.amount" type="text" class="inputbg" value="&lt;s:property value='#hasDeductAmount'/&gt;" /&gt;

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;
 }
```

 转自：[http://topmanopensource.iteye.com/blog/570022](http://topmanopensource.iteye.com/blog/570022)

 

HTML标签的属性可以被赋值为一个静态的值或一个OGNL表达式。如果你在赋值时使用了一个OGNL表达式并把它用"%{"和"}"括了起来，这个表达式将会被求值。

比如，

      1、下面的label属性将被赋值字符串“useName”：

           label="useName"

      2、而下面这个表达式使用了一个OGNL表达式userName，label属性的值将等于某一个动作类中的userName属性值：

           label="%{useName}"

       3、下面这个表达式将把label属性赋值为会话属性userName的值：

           label="%{#session.userName}"

       4、这个value属性将被赋值为6：

           value="%{1 + 5}"