OGNL表达式struts2标签“%,#,$”

  1. 一、什么是OGNL,有什么特点? <wbr><wbr></wbr></wbr>
  2. <wbr><wbr></wbr></wbr>
  3. OGNL(Object-Graph<wbr>Navigation<wbr>Language),大概可以理解为:对象图形化导航语言。是一种可以方便地操作对象属性的开源表达式语言。OGNL有如下特点:<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  4. <wbr><wbr></wbr></wbr>
  5. 1、支持对象方法调用,形式如:objName.methodName(); <wbr><wbr></wbr></wbr>
  6. <wbr><wbr></wbr></wbr>
  7. 2、支持类静态的方法调用和值访问,表达式的格式为@[类全名(包括包路)]@[方法名<wbr>|<wbr><wbr>值名],例如: <wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  8. <wbr><wbr></wbr></wbr>
  9. @java.lang.String@format('foo<wbr>%s'</wbr>,<wbr><span style="color:#0000ff">'bar'</span>)或<span style="color:#646464">@tutorial</span>.MyConstant<span style="color:#646464">@APP_NAME</span>; <wbr><wbr></wbr></wbr></wbr>
  10. <wbr><wbr></wbr></wbr>
  11. 3、支持赋值操作和表达式串联,例如: <wbr><wbr></wbr></wbr>
  12. <wbr><wbr></wbr></wbr>
  13. price=100,<wbr>discount=<span style="color:#c00000">0.8</span>,<wbr>calculatePrice(),这个表达式会返回<span style="color:#c00000">80</span>; <wbr><wbr></wbr></wbr></wbr></wbr>
  14. <wbr><wbr></wbr></wbr>
  15. 4、访问OGNL上下文(OGNL<wbr>context)和ActionContext; <wbr><wbr></wbr></wbr></wbr>
  16. <wbr><wbr></wbr></wbr>
  17. 5、操作集合对象。 <wbr><wbr></wbr></wbr>
  18. <wbr><wbr></wbr></wbr>
  19. <wbr><wbr><wbr></wbr></wbr></wbr>
  20. <wbr><wbr></wbr></wbr>
  21. 二、使用OGNL表达式 <wbr><wbr></wbr></wbr>
  22. <wbr><wbr></wbr></wbr>
  23. OGNL要结合struts标签来使用。由于比较灵活,也容易把人给弄晕,尤其是“%”、“#”、“$”这三个符号的使用。由于$广泛应用于EL中,这里重点写%和#符号的用法。 <wbr><wbr></wbr></wbr>
  24. <wbr><wbr></wbr></wbr>
  25. 1、“#”符号有三种用途: <wbr><wbr></wbr></wbr>
  26. <wbr><wbr></wbr></wbr>
  27. (1)、访问非根对象(struts中值栈为根对象)如OGNL上下文和Action上下文,#相当于ActionContext.getContext();下表有几个ActionContext中有用的属性: <wbr><wbr></wbr></wbr>
  28. <wbr><wbr></wbr></wbr>
  29. <wbr><wbr><wbr></wbr></wbr></wbr>
  30. <wbr><wbr></wbr></wbr>
  31. 名称 <wbr><wbr></wbr></wbr>
  32. <wbr>作用 <wbr><wbr></wbr></wbr></wbr>
  33. <wbr>例子 <wbr><wbr></wbr></wbr></wbr>
  34. <wbr><wbr><wbr></wbr></wbr></wbr>
  35. parameters <wbr><wbr></wbr></wbr>
  36. <wbr>包含当前HTTP请求参数的Map <wbr><wbr></wbr></wbr></wbr>
  37. <wbr>#parameters.id[<span style="color:#c00000">0</span>]作用相当于request.getParameter(<span style="color:#0000ff">"id"</span>)<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  38. <wbr><wbr><wbr></wbr></wbr></wbr>
  39. request <wbr><wbr></wbr></wbr>
  40. <wbr>包含当前HttpServletRequest的属性(attribute)的Map <wbr><wbr></wbr></wbr></wbr>
  41. <wbr>#request.userName相当于request.getAttribute(<span style="color:#0000ff">"userName"</span>)<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  42. <wbr><wbr><wbr></wbr></wbr></wbr>
  43. session <wbr><wbr></wbr></wbr>
  44. <wbr>包含当前HttpSession的属性(attribute)的Map <wbr><wbr></wbr></wbr></wbr>
  45. <wbr>#session.userName相当于session.getAttribute(<span style="color:#0000ff">"userName"</span>)<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  46. <wbr><wbr><wbr></wbr></wbr></wbr>
  47. application <wbr><wbr></wbr></wbr>
  48. <wbr>包含当前应用的ServletContext的属性(attribute)的Map <wbr><wbr></wbr></wbr></wbr>
  49. <wbr>#application.userName相当于application.getAttribute(<span style="color:#0000ff">"userName"</span>)<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  50. <wbr><wbr><wbr></wbr></wbr></wbr>
  51. <wbr><wbr></wbr></wbr>
  52. 注:attr<wbr>用于按request<wbr>&gt;<wbr>session<wbr>&gt;<wbr>application顺序访问其属性(attribute),#attr.userName相当于按顺序在以上三个范围(scope)内读取userName属性,直到找到为止。用于过滤和投影(projecting)集合,如books.{?#<strong><span style="color:#7f0055">this</span></strong>.price&lt;<span style="color:#c00000">100</span>};构造Map,如#{<span style="color:#0000ff">'foo1'</span>:<span style="color:#0000ff">'bar1'</span>,<wbr><span style="color:#0000ff">'foo2'</span>:<span style="color:#0000ff">'bar2'</span>}。 <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  53. <wbr><wbr></wbr></wbr>
  54. <wbr><wbr><wbr></wbr></wbr></wbr>
  55. <wbr><wbr></wbr></wbr>
  56. (2)、用于过滤和投影(projecting)集合,如:<wbr>books.{?#<strong><span style="color:#7f0055">this</span></strong>.price&gt;<span style="color:#c00000">35</span>}<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  57. <wbr><wbr></wbr></wbr>
  58. books.{?#this.price>35} <wbr><wbr></wbr></wbr>
  59. <wbr><wbr></wbr></wbr>
  60. (3)、构造Map,如:<wbr>#{<span style="color:#0000ff">'foo1'</span>:<span style="color:#0000ff">'bar1'</span>,<wbr><span style="color:#0000ff">'foo2'</span>:<span style="color:#0000ff">'bar2'</span>}<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  61. <wbr><wbr></wbr></wbr>
  62. #{'foo1':'bar1',<wbr><span style="color:#0000ff">'foo2'</span>:<span style="color:#0000ff">'bar2'</span>}这种方式常用在给radio或select、checkbox等标签赋值上。如果要在页面中取一个map的值可以这样写:<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  63. <wbr><wbr></wbr></wbr>
  64. <s:property<wbr>value=<span style="color:#0000ff">"#myMap['foo1']"</span>/&gt;<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  65. <wbr><wbr></wbr></wbr>
  66. <s:property<wbr>value=<span style="color:#0000ff">"#myMap['foo1']"</span>/&gt; <wbr><wbr></wbr></wbr></wbr>
  67. <wbr><wbr></wbr></wbr>
  68. 2、“%”符号的用途是在标签的属性值被理解为字符串类型时,告诉执行环境%{}里的是OGNL表达式。 <wbr><wbr></wbr></wbr>
  69. <wbr><wbr></wbr></wbr>
  70. 这是一开始最让我不能理解的符号,原因是一些相关资源在表述时不太准备,经过一翻痛苦的探索,终于明白了它的用途。实际上就是让被理解为字符串的表达式,被真正当成ognl来执行。很有点类似javascript里面的eval_r()功能,例如<wbr>: <wbr><wbr></wbr></wbr></wbr>
  71. <wbr><wbr></wbr></wbr>
  72. var<wbr>oDiv<wbr>=<wbr>eval_r(<span style="color:#0000ff">"document.all.div"</span>+index)<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  73. <wbr><wbr></wbr></wbr>
  74. var<wbr>oDiv<wbr>=<wbr>eval_r(<span style="color:#0000ff">"document.all.div"</span>+index) <wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  75. <wbr><wbr></wbr></wbr>
  76. 当index变量为1时,语句就会被当作var<wbr>oDiv<wbr>=<wbr>document.all.div1<wbr><wbr>var<wbr>oDiv<wbr>=<wbr>document.all.div1来执行。%{}就是起这个作用。举例:<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  77. <wbr><wbr></wbr></wbr>
  78. <s:set<wbr>name=<span style="color:#0000ff">"myMap"</span><wbr>value=<span style="color:#0000ff">"#{'key1':'value1','key2':'value2'}"</span>/&gt;<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  79. <wbr><wbr></wbr></wbr>
  80. <s:property<wbr>value=<span style="color:#0000ff">"#myMap['key1']"</span>/&gt;<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  81. <wbr><wbr></wbr></wbr>
  82. <s:url<wbr>value=<span style="color:#0000ff">"#myMap['key1']"</span><wbr>/&gt;<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  83. <wbr><wbr></wbr></wbr>
  84. <s:set<wbr>name=<span style="color:#0000ff">"myMap"</span><wbr>value=<span style="color:#0000ff">"#{'key1':'value1','key2':'value2'}"</span>/&gt; <wbr><wbr></wbr></wbr></wbr></wbr>
  85. <wbr><wbr></wbr></wbr>
  86. <s:property<wbr>value=<span style="color:#0000ff">"#myMap['key1']"</span>/&gt; <wbr><wbr></wbr></wbr></wbr>
  87. <wbr><wbr></wbr></wbr>
  88. <s:url<wbr>value=<span style="color:#0000ff">"#myMap['key1']"</span>/&gt; <wbr><wbr></wbr></wbr></wbr>
  89. <wbr><wbr></wbr></wbr>
  90. 上面的代码第2行会在页面上输出“value1”,而第3行则会输出"#myMap['key1']"这么一个字符串。<wbr>如果将第<span style="color:#c00000">3</span>行改写成这样:<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  91. <wbr><wbr></wbr></wbr>
  92. <s:url<wbr>value=<span style="color:#0000ff">"%{#myMap['key1']}"</span>/&gt;<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  93. <wbr><wbr></wbr></wbr>
  94. <s:url<wbr>value=<span style="color:#0000ff">"%{#myMap['key1']}"</span>/&gt; <wbr><wbr></wbr></wbr></wbr>
  95. <wbr><wbr></wbr></wbr>
  96. 则输出为“value1”。 <wbr><wbr></wbr></wbr>
  97. <wbr><wbr></wbr></wbr>
  98. <wbr><wbr><wbr></wbr></wbr></wbr>
  99. <wbr><wbr></wbr></wbr>
  100. <wbr><wbr><wbr></wbr></wbr></wbr>
  101. <wbr><wbr></wbr></wbr>
  102. 这说明struts2里不同的标签对ognl的表达式的理解是不一样的。如果当有的标签“看不懂”类似“#myMap['key1']”的语句时,就要用%{}来把这括进去,“翻译”一下了。 <wbr><wbr></wbr></wbr>
  103. <wbr><wbr></wbr></wbr>
  104. 3、“$”有两种用途 <wbr><wbr></wbr></wbr>
  105. <wbr><wbr></wbr></wbr>
  106. (1)、在国际化资源文件中,引用OGNL表达式。 <wbr><wbr></wbr></wbr>
  107. <wbr><wbr></wbr></wbr>
  108. (2)、在Struts<wbr><span style="color:#c00000">2</span>配置文件中,引用OGNL表达式:<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  109. <wbr><wbr></wbr></wbr>
  110. <action<wbr>name=<span style="color:#0000ff">"saveUser"</span><wbr><strong><span style="color:#7f0055">class</span></strong>=<span style="color:#0000ff">"userAction"</span><wbr>method=<span style="color:#0000ff">"save"</span>&gt; <wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  111. <wbr><wbr></wbr></wbr>
  112. <result<wbr>type=<span style="color:#0000ff">"redirect"</span>&gt;listUser.action?msg=${msg}&lt;/result&gt; <wbr><wbr></wbr></wbr></wbr>
  113. <wbr><wbr></wbr></wbr>
  114. </action> <wbr><wbr></wbr></wbr>
  115. <wbr><wbr></wbr></wbr>
  116. <action<wbr>name=<span style="color:#0000ff">"saveUser"</span><wbr><strong><span style="color:#7f0055">class</span></strong>=<span style="color:#0000ff">"userAction"</span><wbr>method=<span style="color:#0000ff">"save"</span>&gt;<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  117. <wbr><wbr></wbr></wbr>
  118. <result<wbr>type=<span style="color:#0000ff">"redirect"</span>&gt;listUser.action?msg=${msg}&lt;/result&gt; <wbr><wbr></wbr></wbr></wbr>
  119. <wbr><wbr></wbr></wbr>
  120. </action><wbr></wbr>

你可能感兴趣的:(struts2标签)