% {}

Source: Internet
Author: User

Struts2的标签都支持动态数据的访问,标签的属性都可以使用OGNL表达式。Struts2标签的属性具有类型,这些类型可以简单地分为字符串类型和非字符串类型,对于字符串类型的属性,如果要访问动态数据,需要使用%{…}这样的语法。例如:
<s:include value=”%{url}”/>

include标签的value属性民是字符串类型,Struts2将对这个属性进行解析,如果属性值使用%{…}包含,那么%{…}中的内容将被视为OGNL表达式。如果属性中没有使用%{…},那么属性值将被直接看成是字符串数据。例如:
<s:include value=”urlTag.action”/>

对于非字符串类型的属性值,将直接作为OGNL表达式进行求值。例如:
<s:property value=”username”/>

property标签的value属性是Object类型,它的值username将作为OGNL表达式进行求值,结果是值栈中位于栈顶的对象的username属性的值。如果要为非字符串类型的属性直接指定字符串数据,那么需要使用OGNL中的字符串常量,即用单引号或双引用将字符串括起来。例如:
<s:property value=”’zhangSan’”/>

在property标签的value属性中,也可以使用%{…}。不过使用%{…}与不使用没有什么区别。Struts2会忽略%{…}的存在,把内容以OGNL表达式来处理。
<s:property value=”%{’zhangSan’}”/>

对property标签的value属性来说,%{‘zhangSan’}与’zhangSan’是相同的。因为value属性的类型是Object,所以value属性的内容直接被作为OGNL表达式处理。但Struts2不会因为加了%{…}而报错,而是忽略掉%{…}的存在,直接把内容取出做为OGNL表达式处理。
总结一下,Struts2标签的属性按照下列三个规则进行计算:
所有的字符串属性类型都会解析%{…}这样的语法。将%{…}内容做为OGNL表达式处理。
所有的非字符串属性类型都不会被解析,而是直接被看作一个OGNL表达式进行求值。
对于第二个规则的例外情况是,如果非字符串属性中使用了%{…}语法,那么%{…}将被忽略,花括号中的内容将作为OGNL表达式被计算。

当我们使用标签时,忘记了某个属性是字符串类型,还是非字符串类型,那么有一个冬的方法,那就是不考虑它是什么类型,统一使用%{…}语法。

% {}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.