Home > Java > javaTutorial > The Road to Webwork Learning (2) Front-end OGNL Trial

The Road to Webwork Learning (2) Front-end OGNL Trial

黄舟
Release: 2016-12-23 16:29:33
Original
1418 people have browsed it

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.

a. OGNL allows us to access the object layer with very simple expressions [OGNL supports key value search for Map]

You can directly click on the properties of the object ,


If the user attribute is a list, you can get the name of the first user object

If the user attribute is a map Get the value corresponding to the user "name" key

b. Supports operators (such as +-*/), which has a higher degree of freedom and stronger functions than ordinary flags


c. Support object method calls, such as xxx.doSomeSpecial()

d. Support class static method calls and value access, expressions The format is @[full class name (including package path)]@[method name | value name], for example: @java.lang.String@format('foo %s', 'bar') or @tutorial.MyConstant@APP_NAME


e. Supports assignment operations and expression concatenation, such as price=100, discount=0.8, calculatePrice (price*discount), this expression will return 80

f. Access the OGNL context and ActionContext

g. You can also create an object through the constructor of any class object

/**new Java.net.URL("xxxxxx/")

* */

h.OGNL supports projection and selection similar to those in the database.

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.

/**group.userList.{username} will get a list of the names of all users in a group

**/

Selection is to filter the set elements that meet the selection conditions, similar to the record operation of a relational database. The syntax of the selection operation is: collection.{X YYY}, where X is a selection operator, followed by the logical expression for selection. There are three selection operators:

? 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!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template