Ognl expression struts2 label "%, #, $"

Source: Internet
Author: User
  1. 1. What is ognl and what are its characteristics? 
  2.   
  3. Ognl (Object-graphNavigationLanguage. Is an open-source expression language that allows you to conveniently operate object attributes. Ognl has the following features: 
  4.   
  5. 1. Supports object method calls in the form of objname. methodname (); 
  6.   
  7. 2. Supports static class method calls and value access. The expression format is @ [full class name (including package path)] @ [method name| Value name], for example: 
  8.   
  9. @ Java. Lang. String @ format ('foo% S','Bar') or @ tutorial. myconstant @ app_name;
     
  10.   
  11. 3. The assignment operation and expression concatenation are supported, for example: 
  12.   
  13. Price = 100,Discount =0.8,Calculateprice (), which returns80;
     
  14.   
  15. 4. Access ognl context (ognlContext) and actioncontext; 
  16.   
  17. 5. Operation set object. 
  18.   
  19.   
  20.   
  21. Ii. Use ognl expressions 
  22.   
  23. Ognl should be used with Struts labels. Because of its flexibility, it is easy to confuse people, especially the use of the three symbols "%", "#", and "$. Because $ is widely used in El, it focuses on the usage of the % and # symbols. 
  24.   
  25. 1. The "#" symbol has three purposes: 
  26.   
  27. (1) access non-root objects (the struts value stack is the root object) such as ognl context and action context. # It is equivalent to actioncontext. getcontext (). The following table contains several useful attributes in actioncontext: 
  28.   
  29.   
  30.   
  31. Name 
  32.  Function 
  33.  Example 
  34.   
  35. Parameters 
  36.  Map containing the current HTTP Request Parameters 
  37.  # Parameters. ID [0] is equivalent to request. getparameter ("ID ") 
  38.   
  39. Request 
  40.  Map that contains the attribute of the current httpservletrequest 
  41.  # Request. username is equivalent to request. getattribute ("Username ") 
  42.   
  43. Session 
  44.  Map that contains the attribute of the current httpsession 
  45.  # Session. username is equivalent to session. getattribute ("Username ") 
  46.   
  47. Application 
  48.  Map that contains the attribute of the current application's servletcontext 
  49.  # Application. username is equivalent to application. getattribute ("Username ") 
  50.   
  51.   
  52. Note: ATTRUsed to press request>Session>Application accesses its attributes in sequence, # ATTR. username is equivalent to reading the username attribute in the preceding three scopes in order until it is found. Used to filter and project sets, such as books .{? #This. Price <100}; construct a map, for example, # {'foo1': 'bar1 ','Foo2': 'bar2 '}.
     
  53.   
  54.   
  55.   
  56. (2) used for filtering and projecting sets, such:Books .{? #This. Price> 35} 
     
  57.   
  58. Books .{? #This. Price> 35} 
  59.   
  60. (3) construct a map, such:#{'Foo1': 'bar1 ','Foo2': 'bar2 '} 
     
  61.   
  62. # {'Foo1': 'bar1 ','Foo2': 'bar2'} is usually used to assign values to tags such as radio, select, and checkbox. If you want to obtain a MAP value in the page, you can write it as follows:
     
  63.   
  64. <S: PropertyValue ="# Mymap ['foo1']"/>  
  65.   
  66. <S: PropertyValue ="# Mymap ['foo1']"/> 
  67.   
  68. 2. The "%" symbol is used to tell the execution environment % {} That the ognl expression is used when the tag property value is understood as a string type. 
  69.   
  70. This is the most incomprehensible symbol at the beginning, because some resources are not prepared for presentation. After a painful exploration, I finally understood its purpose. Actually, it is to let the expression that is understood as a string and be actually executed as ognl. Similar to the eval_r () function in Javascript, for example: 
  71.   
  72. VaROdiv=Eval_r ("Document. All. Div" + index)  
  73.   
  74. VaROdiv=Eval_r ("Document. All. Div" + index) 
  75.   
  76. When the index variable is 1, the statement is treated as VAROdiv=Document. All. div1 VaROdiv=Document. All. div1. % {} Serves this purpose. Example: 
  77.   
  78. <S: SetName ="Mymap"Value ="# {'Key1': 'value1', 'key2': 'value2'}"/>   
  79.   
  80. <S: PropertyValue ="# Mymap ['key1']"/>   
  81.   
  82. <S: URLValue ="# Mymap ['key1']"/> 
  83.   
  84. <S: SetName ="Mymap"Value ="# {'Key1': 'value1', 'key2': 'value2'}"/> 
  85.   
  86. <S: PropertyValue ="# Mymap ['key1']"/> 
  87.   
  88. <S: URLValue ="# Mymap ['key1']"/> 
  89.   
  90. The code above 2nd will output "value1" on the page, while line 3rd will output a string such as "# mymap ['key1.IfThe three lines are rewritten as follows:
     
  91.   
  92. <S: URLValue ="% {# Mymap ['key1']}"/>  
  93.   
  94. <S: URLValue ="% {# Mymap ['key1']}"/> 
  95.   
  96. The output is "value1 ". 
  97.   
  98.   
  99.   
  100.   
  101.   
  102. This shows that different tags in struts2 have different understandings of ognl expressions. If some labels "do not understand" statements similar to "# mymap ['key1']", use % {} to include the statements and translate them. 
  103.   
  104. 3. "$" has two purposes: 
  105.   
  106. (1) Reference ognl expressions in international resource files. 
  107.   
  108. (2) In struts2. Reference The ognl expression in the configuration file: 
  109.   
  110. <ActionName ="Saveuser"Class= "Useraction"Method ="Save">
     
  111.   
  112. <ResultType ="Redirect"> listuser. Action? MSG =$ {MSG} </result> 
  113.   
  114. </Action> 
  115.   
  116. <ActionName ="Saveuser"Class= "Useraction"Method ="Save">   
     
  117.   
  118. <ResultType ="Redirect"> listuser. Action? MSG =$ {MSG} </result> 
  119.   
  120. </Action>

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.