Underlying usage of Ognl and underlying usage of Ognl
I learned how to use ognl today. I would like to share with you here. Hope to help you.
Package com. hcj. test; import java. util. arrayList; import java. util. list; import ognl. ognl; import ognl. ognlContext; import ognl. ognlException; public class Test {public static void main (String [] args) throws OgnlException {Person p = new Person (); p. setName ("Zhang San"); Cat cat = new Cat (); cat. setName ("huaxiao"); // create ognl context OgnlContext ognlContext = new OgnlContext (); ognlContext. put ("person", p); ognlContext. put ("cat ", Cat); // set the person root object ognlContext. setRoot (p); // get the root Object root = ognlContext. getRoot (); // get the root Object attribute Object obj1 = Ognl. parseExpression ("name"); System. out. println (obj1); Object value1 = Ognl. getValue (obj1, ognlContext, root); System. out. println (value1); System. out. println ("==========="); // access the attribute Object obj2 = Ognl of a non-root Object. parseExpression ("# cat. name "); System. out. println (obj2); Object value2 = Ognl. getValue (obj2, og NlContext, root); System. out. println (value2); System. out. println ("==========="); // static method of the response Class Object obj3 = Ognl. parseExpression ("@ java. lang. math @ PI "); System. out. println (obj3); Object value3 = Ognl. getValue (obj3, ognlContext, root); System. out. println (value3); System. out. println ("======"); // access the math method Object obj4 = Ognl. parseExpression ("@ max (1, 3)"); System. out. println (obj4); Object value4 = Ognl. getValue (obj4, OgnlContext, root); System. out. println (value4); System. out. println ("======"); // create an Object obj5 = Ognl. parseExpression ("new com. hcj. test. cat () "); System. out. println (obj5); Object value5 = Ognl. getValue (obj5, ognlContext, root); System. out. println (value5); System. out. println ("======"); // create the list Object obj6 = Ognl. parseExpression ("{'A', 'B', 'C'}"); System. out. println (obj6); Object value6 = Ognl. getValue (obj6, OgnlContext, root); System. out. println (value6); System. out. println ("======"); // access the list Object attribute Object obj7 = Ognl. parseExpression ("{'A', 'B', 'C'} [1]"); System. out. println (obj7); Object value7 = Ognl. getValue (obj7, ognlContext, root); System. out. println (value7); System. out. println ("======"); // create the map Object obj8 = Ognl. parseExpression ("# {'1': 'A', '2': 'bb', '3': 'cc'}"); System. out. println (obj8); Object value8 = O Gnl. getValue (obj8, ognlContext, root); System. out. println (value8); System. out. println ("======"); // access the map Object attribute Object obj9 = Ognl. parseExpression ("# {'1': 'A', '2': 'bb', '3': 'cc'} ['3']"); System. out. println (obj9); Object value9 = Ognl. getValue (obj9, ognlContext, root); System. out. println (value9); System. out. println ("======"); // select, format: collection. {? Expression} List <Person> persons = new ArrayList <Person> (); Person p1 = new Person (); p1.setName ("zz12345"); Person p2 = new Person (); p2.setName ("aa121312"); Person p3 = new Person (); p3.setName ("bb"); persons. add (p1); persons. add (p2); persons. add (p3); // put persons into ognl context. put ("persons", persons); Object obj10 = Ognl. parseExpression ("# persons. {? # This. name. length ()> 2} "); System. out. println (obj10); Object value10 = Ognl. getValue (obj10, ognlContext, root); System. out. println (value10); System. out. println ("======"); // projection, format: collection. {? Expression} Object obj11 = Ognl. parseExpression ("# persons. {# this. name} "); System. out. println (obj11); Object value11 = Ognl. getValue (obj11, ognlContext, root); System. out. println (value11); System. out. println ("====== ");}}
Output result:
Name zhang San ==========## cat. name flower cat =============@ java.lang.Math@PI3.141592653589793 =======@ java. lang. math @ max (1, 3) 3 ==== new com. hcj. test. cat () com. hcj. test. cat @ 1dff3a2 ====={ 'A', 'B', 'C'} [a, B, c] ====={ 'A ', 'B', 'C'} [1] B ====#{ '1': "aa", '2': "bb", '3 ': "cc"} {1 = aa, 2 = bb, 3 = cc }=====#{ '1': "aa", '2 ': "bb", '3': "cc"} ['3'] cc =====# persons. {? (# This. name. length ()> 2)} [com. hcj. test. person @ b988a6, com. hcj. test. person @ ba6c83] ====## persons. {# this. name} [zz12345, aa121312, bb] ====
Role of % in ognl expression
In the struts2 string, % {} indicates that it is an ognl expression and needs to be parsed.
For example, <s: textfield lable = "user" name = "user" value = "% {# session. user}"/>
However, some labels are identified by default. The entire string is an expression, so % {} is not required {}
For example, <s: property value = "# session. user"/>
In non-struts2 labels, % {} cannot be used. You can use the following method:
<Input name = "user" value = "$ {session. user}"/>. This is an el expression.
<Input name = "user" value = "<s: property value = '# session. user'/>"/>, which is a nested use of the struts2 expression.
However, note that the struts2 tag cannot be nested in strings in the struts2 tag again.
For example, <s: textfield lable = "user" name = "user" value = "<s: property value = '# session. user'/>"/>
You cannot use the el expression in the struts2 tag,
For example, <s: textfield lable = "user" name = "user" value = "$ {session. user}"/>
What is OGNL used?
???? What are you asking ??