OGNL stands for Object Graph Navigation Language.
It’s an expression language for getting/setting properties of Java objects and other extra functions. It works as a binding between GUI elements or UI components to model objects.
This post is a summary after studying Apache Struts 2 Documentation - ONGL.
Import tag lib
Before using ONGL, we need to import it in JSP page:
Control Tags
- if/elseif/else
- iterator
- append
- merge
- generator
- subset
- sort
if/elseif/else
if
and elseif
must have test
attribute in which should be a boolean expression.
iterator
Tag to iterate a collection (List, set, array or map). It has three attributes:
Name | Required | Default | Description |
---|---|---|---|
begin | X | 0 | Index to start iteration |
end | X | size of collection | Index to end iteration |
status | X | Instance of IteratorStatus which will be pushed into stack upon each iteration | |
step | X | 1 | Iteration index will be increased by this value on each iteration |
value | X | Collection to iterate | |
var | X | Name used to reference the value pushed into the Value Stack |
Instance of IteratorStatus also has following functions:
if status="test"
,
#test.count
returns number of element already iterated. (equals toint getCount()
)#test.index
returns index of current element. (equals toint getIndex()
)#test.even
returns wether current element is even. (equals toboolean isEven()
)#test.odd
returns wether current element is odd. (equals toboolean isOdd()
)#test.first
returns wether current element is the first one. (equals toboolean isFirst()
)#test.last
returns wether current element is the last one. (equals toboolean isLast()
)
If now we have a list called employeeList which is List<Employee>
. Employee has two attributes name
and age
with getter/setter:
If it’s List<String>, use s:property
without value
to present a string element in the list:
If it’s Map:
append
It appends several collections to form a new collection.
The new collection has the following order (different from tag `merge):
- First Entry of the First Iterator
- Second Entry of the First Iterator
- Third Entry of the First Iterator
- First Entry of the Second Iterator
- Second Entry of the Second Iterator
- Third Entry of the Second Iterator
The following example shows three lists of type List<String>
to be appended together. These three lists are passed by some Action.
merge
The same as append
but in different order:
- First Entry of the First Iterator
- First Entry of the Second Iterator
- Second Entry of the First Iterator
- Second Entry of the Second Iterator
- Third Entry of the First Iterator
- Third Entry of the Second Iterator
generator
Generate an iterator based on give string. If var
not set, it will only exist in Value Stack which means you can not use it outside the tag. (Not in page context)
Name | Required | Default | Description |
---|---|---|---|
separator | O | The separator to be used in separating the val into entries of the iterator | |
val | O | The source to be parsed into an iterator | |
var | X | The name to store the resultant iterator into page context | |
count | X | Max number of entries to be in the iterator | |
converter | X | Convert the String entry parsed from val into an object |
A string separated by “,”:
Generate an iterator with comparator attribute:
This will generate an iterator with each entries decided by the converter supplied. With this converter, it simply add “converter-“ to each entries.
subset
Outputs a subset of an iterator.
Name | Required | Default | Description |
---|---|---|---|
count | X | Indicate the number of entries to be in the resulting subset iterator | |
decider | X | Extension to plug-in a decider to determine if that particular entry is to be included in the resulting subset iterator | |
var | X | The name to store the resultant iterator into page context | |
source | X | Source to do subset | |
start | X | 0 | Indicate the startig index |
Simply subset:
With decider:
Sort
It sorts a List using a Comparator
Name | Required | Default | Description |
---|---|---|---|
comparator | O | The comparator to use (java.util.Comparator) | |
var | X | The name to store the resultant iterator into page context | |
source | X | Source to sort |
Example
Sort by string length:
Data Tags
action
It could call actions directly from a JSP page by specifying the action name and an optional namespace.
Any result processor defined for this action in struts.xml will be ignored, unless the executeResult parameter is specified.
Name | Required | Default | Description |
---|---|---|---|
executeResult | x | false | Whether the result of this action (probably a view) should be executed/rendered |
var | X | Name used to reference the value pushed into the Value Stack | |
name | O | Name of the action to be executed (without the extension suffix eg. .action) | |
namespace | X | Namespace from where tag is used | Namespace of the action |
ignoreContextParams | X | false | Whether the request parameters are to be passed to the action |
rethrowException | X | false | Whether an exception should be rethrown, if the target action throws an exception |
Example
When we acess the test page with a request parameter - localhost:8000/index?author=Dong
: