The expressions are as follows: (import relevant jar packages, ognl. jar and Javassist. Jar)
Package COM. ognl; import Java. util. arraylist; import Java. util. list; import ognl. ognl; import ognl. ognlcontext; public class ognltest {public static void main (string [] ARGs) throws exception {person = new person (); person. setname ("zhangsan"); dog = new dog (); dog. setname ("wangcai"); dog dog2 = new dog (); dog2.setname ("Maomao"); person. setdog (dog2); ognlcontext context = new ognlcontext (); // implements the map interface C Ontext. put ("person", person); context. put ("dog", dog); context. setroot (person); object = ognl. parseexpression ("name"); system. out. println (object); // name/** ognl. getvalue (object, context, context. getroot () * it searches for objects in the specified context. By default, it searches for objects in the root element * In ognl. If the expression does not use the # sign, ognl searches for the get () * method corresponding to this attribute from the root object. If it is not an attribute in the root object, it must start with # And tell ognl, find the attribute */object object2 = ognl in the specified object. getvalue (object, context, Context. Getroot (); system. out. println (object2); // zhangsanobject o = ognl. getvalue ("name", context, context. getroot (); system. out. println (o); // zhangsanobject object3 = ognl. getvalue ("# Dog. name ", context, context. getroot (); system. out. println (object3); // wangcaiobject object4 = ognl. getvalue ("# person. dog. name ", context, context. getroot (); system. out. println (object4); // Maomao/** method used to call attributes */system. out. println (" ------------------------------- "); // Object OBJ = ognl. getvalue ("getname (). touppercase () ", context, context. getroot (); // The effect is the same as that of object object5 = ognl. getvalue ("name. touppercase () ", context, context. getroot (); system. out. println (object5); // zhangsansystem. out. println ("------------------------------");/** when calling static methods using ognl, you need to write the expression * @ package according to the following syntax. classname @ method (parameters) * Special Class: For ognl, Java. lang. m ATH is its default class, that is, calling Java. lang. when using a static math method, * you do not need to specify the class name */object object6 = ognl. getvalue ("@ Java. lang. integer @ tobinarystring (10) ", context, context. getroot (); system. out. println (object6); // 1010 object object7 = ognl. getvalue ("@ min (7, 10)", context, context. getroot (); system. out. println (object7); // 7system. out. println ("--------------------------");/** the array and Set * are accessed through subscript indexes */object object8 = ognl. getvalue ("{'A ',' Bb ', 'cc'} [2] ", context, context. getroot (); system. out. println (object8); // ccsystem. out. println ("-------------------------");/** map * syntax format: * # {'key1': 'value1 ', 'key2 ': 'value2'} */object object9 = ognl. getvalue ("# {'key1': 'value1', 'key2': 'value2'} ['key2']", context, context. getvalues (); system. out. println (object9); // value2system. out. println ("--------------------------");/** filter * Format: * Collection. {? Expression} * returns a set * # This indicates that each object (Lenovo enhanced for loop) */list <person> List = new arraylist <person> (); person p1 = new person (); person P2 = new person (); person P3 = new person (); p1.setname ("hehe"); p2.setname ("xixi3 "); p3.setname ("haha333"); list. add (P1); list. add (P2); list. add (P3); context. put ("list", list); system. out. println (ognl. getvalue ("# list. {? # This. name. length ()> 4 }. size () ", context, context. getroot (); // 2/** filter and obtain the first element in the Set * expression: * Collection. {^ expression} * returns a set */system. out. println (ognl. getvalue ("# list. {^ # This. name. length ()> 4 }. get (0 ). getname () ", context, context. getroot (); // xixi3/** the filter obtains the last element * expression in the Set: * Collection. {$ expression} */system. out. println (ognl. getvalue ("# list. {$ # This} [0]. name ", context, context. getroot (); // H Aha333/** projection * expression: * Collection. {expression} * returns a set. The returned length is equal to the length of the original set. * The objects in the returned set are the subsets of the original object. ** differences between filtering and projection: * similar to a table in a database, filtering is row fetch, while projection is column fetch */system. out. println (ognl. getvalue ("# list. {name} ", context, context. getvalues (); // [Hehe, xixi3, haha333] system. out. println (ognl. getvalue ("# list. {# This. name. length ()> 4? 'Hello world': # This. name} ", context, context. getroot (); // [Hehe, hello World, hello World]}
In struts2, the root object is valuestack (a stack). In any process of struts2, the top-level object in valuestack must be an action object.
The following objects are called named objects and are not in valuestack:
Class Name |
Access Method |
Parameters) |
# Parameters. uernme |
Request |
# Request. Username |
Application |
# Application. Username |
Improvement on accessing static methods or static member variables @ vs @ method (vs indicates valuestack)