Struts learning path (2) Front-end OGNL trial, strutsognl

Source: Internet
Author: User

Struts learning path (2) Front-end OGNL trial, strutsognl
1. Significance of the appearance of OGNL

In mvc, data is transferred between different layers. This kind of circulation will face some dilemmas. These dilemmas are caused by the different forms of data in different worlds:

A. the data on the page is a flat string with no data type. No matter how complicated your data structure is, how many data types are, when it is displayed, strings are displayed on the page.

B. Data in the Java World can be represented by rich data structures and data types. You can customize your favorite classes and inherit and nest them between classes. We usually call this model a complex object tree. At this point, if data is transferred between the page and Java World, it will not match. Therefore, several problems need to be solved;

C. when data is transferred from the View layer to the Controller layer, we should ensure that a flat and scattered dataset can be set to an object tree in the Java World with certain rules. At the same time, it can intelligently convert from string type to various types in Java;

D. when data is transferred from the Controller layer to the View layer, we should ensure that the View layer can access the object tree with some simple rules. At the same time, the data display format in the object tree is controlled to a certain extent.

If we think deeply about this problem, we will find that it is no stranger to us to solve the problem of stream mismatch due to different forms of data. The same problem occurs in the Java and database worlds. In the face of this mismatch between objects and relational models, we use the ORM framework, such as Hibernate and iBatis. So now, the Web layer does not match, so we also need some tools to help us solve the problem. To solve the inconsistency between data transmitted from the View layer to the Controller layer, Struts2 adopts a perfect XWork solution. On this basis, a perfect mechanism is built to perfectly solve the mismatch in data flow. The OGNL solution came into being.

2. OGNL Project Practice

OGNL is the abbreviation of Object-Graph Navigation Language. It is a powerful expression Language. Through its simple and consistent expression syntax, it can access any attribute of an Object and call the Object method,
Traverse the structure of the entire object to convert the field type.

A. OGNL allows us to access the Object layer with a very simple expression [OGNL supports searching Map key values]

<Ww: property value = 'user. name'/> You can directly click the attributes of an object. For example, if the user attribute is list, <ww: property value = 'user [0]. name'/> you can obtain the name of the first user object. For example, the user attribute is a map <ww: property value = 'user [name] '/> get the value corresponding to the user "name" key

B. operators (such as +-*/) are supported, which have higher degrees of freedom and stronger functions than common signs.

<ww:property value="100 - wcjd"/>

C. Support Object method calls, such as xxx. doSomeSpecial ()

D. supports static class method calls and value access. The expression 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

<ww:property value="#DateUtil.getNowdate(\"yyyy-MM-dd\")"/>

E. The value assignment operation and expression concatenation are supported, such as price = 100, discount = 0.8, and calculatePrice (price * discount). This expression returns 80

F. Access 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 in databases ).
Projection is to select the same attributes of each element in the set to form a new set, similar to field operations in relational databases. The projection operation syntax is collection. {XXX}, where XXX is the public attribute of each element in the set.

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

Select is to filter the set elements that meet the selection conditions, similar to record operations of relational databases. The Select Operation syntax is collection. {x yyy}, where X is a selection operator, followed by the selected logical expression. There are three selection operators:
? Select all elements that meet the conditions
^ Select the first element that meets the conditions
$ Select the last element that meets the conditions

/** Group. userList .{? # Txxx. xxx! = Null} returns the list of users whose names are not empty in A group **/

I. OGNL is usually used together with the Struts 2 logo, mainly using the three symbols #, %, and $. The usage is as follows:

/** Access OGNL context and Action context, # equivalent to ActionContext. getContext (); The following table contains several useful attributes of ActionContext: parameters contains the Map # parameters of the current HTTP request parameter. id [0] is equivalent to request. getParameterValues ("id "). get (0); request contains the Map # request of the attribute of the current HttpServletRequest. userName is equivalent to request. getAttribute ("userName") session contains the Map # session of the attribute of the current HttpSession. userName is equivalent to session. getAttribute ("userName") application contains the attributes of the current application's ServletContext ( Attribute) Map # application. userName is equivalent to application. getAttribute ("userName") attr is used to access attributes in the order of request> session> application # attr. userName is equivalent to reading the userName attribute in the scope until it is found for filtering and projecting sets, such as books. {? # This. price <100}; construct a Map, for example, # {'foo1': 'bar1', 'foo2': 'bar2 '}. % "Is used to calculate the value of the OGNL expression when the flag property is of the string type. "$" Has two main purposes: to reference OGNL expressions in international resource files. Configure the file in Struts 2 and i18n /**

J. OGNL can obtain the data transmitted by the background action get () set () method and display it to the foreground flexibly.

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.