This article tries to use the most simple language and examples, introduce OGNL grammar rules, the article main content reference since the official document commons.apache.org/proper/comm…

  • This essay is mainly an introduction to grammar
  • Because this article will be a precursor to post-Quick-fix 2.0 support for OGNL, it’s out in advance

1. Introduction

Ognl, Object Graphic Navigation Language, assembles a readable and executable expression statement according to some simple rules of the convention

The following is a typical expression

"name".toCharArray()[0].numericValue.toString()
Copy the code

Even if you don’t understand OGNL at all, you can simply understand it with the basic knowledge of Java, which is the charm of OGNL (learn a little bit and you can start immediately).

2. Locate objects

Note that the contents of this section are classified for my own convenience and are not official

We know that in Java, everything is an object, so our OGNL expression must start with an object. In OGNL, we can usually divide the target objects to be executed into three categories

  • Simple objects :(e.g., primitive data types, strings)
  • Non-simple objects :(non-simple objects, instance access)
  • Static objects :(static classes)

In GONL syntax, the above three cases are marked according to the different beginning

Static objects

It simply means I want to access a method of a static class (or a member of a static class)

@java.lang.Math
Copy the code

The syntax begins at @, followed by the full class name

An example case is the following, equivalent to calling math.max (10, 20) directly in Java code

@java.lang.Math@max(10, 20)
Copy the code

Non-simple object

Access a member or method of a normal object

#demo
Copy the code

The syntax is based on #, followed by the object name (note that this object needs to be in the context of Ognl and can be uniquely located by the object name).

The simple object

The basic type of object access, without any prefix, can be used directly, as follows

// The length of the string"name".length() // Count 1+2 // Booleantrue
Copy the code

3. Method calls

Executes a method of the target object as follows

// Method access for non-basic objects,# between object and method. The connection
# obj. Method (parameters)// Static object method access, @, @ connection between the object and method @xxx@method(parameter) // basic object method access, and non-basic object methods the same way"name".length()
Copy the code

4. Member access

Access the members of the target object as follows

// Member access for non-basic objects,# between objects and members. The connection
#obj.field// Static object member access, beginning with @, object and member connection @xxx@field // basic object member access, and non-basic object member access"name".hash
Copy the code

Collection of 5.

Ognl has special support for commonly used collections

List

Create a list with {} and access the object’s subscript elements with []

Create a list with three elements: 1,2,3; Gets the element with subscript 2 in the list

{1, 2, 3} [2]Copy the code

Arrays

Array, which can be used in combination with new

New int [] {1, 2, 3}Copy the code

Map

#{k:v, k:v

The following statement creates a map and gets the element whose key is name

#{"name" : "Blog", "age" : 18}["name"]
Copy the code

6. Expression statements

There’s some simple, basic member access, method calls, and then there’s the awesome use of expressions

Members of the assignment

#demo. Name = "blog"
Copy the code

Expression calculation

500 plus 20 minus 30 times 3Copy the code

Ternary operator

"name".length() % 2 == 0 ? "Even length" : "Odd length"
Copy the code

Gather support

Make some simplification for collection, easy to call

// inStatement to determine whether the list contains"name" in {"name"."hello"{1,2,3,4,5,6}.{?#this % 2 == 0}{1,2,3,4,5,6}.{^#this % 2 == 0}{1,2,3,4,5,6}.{$#this % 2 == 0}
Copy the code

Object creation

You can create an object directly with New, which can be useful when you need to execute a target method whose parameters are non-primitive types

// new + full class name new java.lang.string ("hello world")
Copy the code

Chain statement

What is a chained statement?

It’s kind of like the Builder in design mode, where I’m going to do a bunch of operations and get the target

The definition rules are as follows, enclosing parentheses and separated by commas. The last one is the target to be returned

(step1, step2,... , result)Copy the code

Combined with the object creation above, this can be very powerful

package git.hui;
class User {
  public String name;
  public Integer age;
}
Copy the code

Directly create a usable User object, after the following execution, directly obtain a User object with initialized properties

(#user=new git.hui.user (), #user.name=" Blog", #user.age=18, #user)
Copy the code

Lambda expressions

This is a bit more advanced, first defining a lambda expression, then using the previous chaining method to call it, and then a factorial case

#fact = :[#this<=1? 1 : #this*#fact(#this-1)], #fact(3)
Copy the code

II. The other

1. A gray Blog:liuyueyi.github.io/hexblog

A gray personal blog, recording all the study and work in the blog, welcome everyone to go to stroll

2. Statement

As far as the letter is not as good as, has been the content, purely one’s own words, because of the limited personal ability, it is hard to avoid omissions and mistakes, such as finding bugs or better suggestions, welcome criticism and correction, not grudging gratitude

  • Micro Blog address: Small Gray Blog
  • QQ: a gray /3302797840

3. Scan attention

A gray blog

Knowledge of the planet