1. The significance of the emergence of OGNL
In mvc, it is an indisputable fact that data flows between various levels. And this kind of flow will also face some difficulties. These difficulties are caused by the different manifestations of data in different worlds:
a. The data on the page is a flat string without a data type. , no matter how complex your data structure is or how rich the data types are, when it comes to display, they will all be displayed equally as strings on the page.
b. Data can be represented by rich data structures and data types in the Java world. You can define your favorite classes and inherit and nest between classes. We usually refer to this model as a complex object tree. At this time, if the data is transferred between the page and the Java world, it will appear to be mismatched. So this leads to several problems that need to be solved;
c. When data is passed from the View layer to the Controller layer, we should ensure that a flat and scattered data collection can be set to the Java world according to certain rules. Go in the object tree. At the same time, it can intelligently convert from string types to various types in Java;
d. When data is passed from the Controller layer to the View layer, we should ensure that the object tree can be processed according to some simple rules in the View layer. access. At the same time, the display format of the data in the object tree is controlled to a certain extent.
If we think about this problem a little deeper, we will find that solving the problem of data flow mismatch due to different presentation forms is actually not unfamiliar to us. The same problem occurs in the Java world and the database world. Faced with this mismatch between the object and relational models, the solution we adopt is to use ORM frameworks, such as Hibernate, iBatis, etc. So now, a mismatch also occurs in the Web layer, so we also need to use some tools to help us solve the problem. In order to solve the mismatch when data is passed from the View layer to the Controller layer, Struts2 adopts a perfect solution from XWork. And on this basis, a perfect mechanism was built to perfectly solve the mismatch in data flow. The OGNL solution came into being
2. OGNL project actual combat
OGNL is the abbreviation of Object-Graph Navigation Language. It is a powerful expression language. Through its simple and consistent expression syntax, objects can be accessed. Any attribute, call the method of the object, traverse the structure diagram of the entire object, and implement functions such as field type conversion.
If the user attribute is a list,
If the user attribute is a map
* */
Projection is to select the same attributes of each element in the set to form a new set, similar to the field operation of a relational database. The projection operation syntax is collection.{XXX}, where XXX is the public property of each element in this collection.
**/
? Selects all elements that satisfy the condition
^ Selects the first element that satisfies the condition
$ Selects the last element that satisfies the condition
/**group.userList.{? #txxx.xxx != null} will get a list of users in a group whose name is not empty
**/
i. OGNL is usually used in conjunction with Struts 2 signs, mainly the use of the three symbols #, % and $. The usage method is as follows:
/**Access OGNL context and Action context, #equivalent to ActionContext.getContext(); the following table has several useful properties in ActionContext:
parameters Map containing the current HTTP request parameters #parameters. id[0] is equivalent to request.getParameterValues("id").get(0);
request contains the Map of the attributes of the current HttpServletRequest #request.userName is equivalent to request.getAttribute("userName")session contains The Map #session.userName of the attributes of the current HttpSession is equivalent to session.getAttribute("userName")application The Map containing the attributes of the ServletContext of the current application #application.userName is equivalent to application.getAttribute("userName")
attr is used to access its attributes in the order of request > session > application #attr.userName is equivalent to reading the userName attribute in the above three scopes in order until it is found
Used for filtering and Projecting collection,
such as books.{?#this.price<100};
Constructing Map, such as #{'foo1':'bar1', 'foo2':'bar2'}.
%" symbol is used to calculate the value of OGNL expression when the attribute of the flag is of string type.
"$" has two main uses, used to reference OGNL expression in international resource files . Configure files in Struts 2 and i18n
/**
j. OGNL can obtain the data sent by the background action get() set() method and flexibly display it to the front desk
The above is the way to learn Webwork (2) For the content of the front-end OGNL trial, please pay attention to the PHP Chinese website (www.php.cn) for more related content!