Ognl, jstl, struts2 label symbol #, $, % usage example

Source: Internet
Author: User

Obtains the value in the session.

<C: Out value = "$ {sessionscope. User. userid}"> </C: Out> <br>

<C: Out value = "$ {user. userloginname}"> </C: Out> <br>

<S: property value = "# session. User. userid"/> <br>

$ {Session. User. userid} <br>

$ {Sessionscope. User. userid} <br>

 

 

Section 3 ognl

Ognl is the abbreviation of object graph Navigation language. For more information, see: http://www.ognl.org. Here we only involve the basic support for ognl in the struts2 framework.

 

Ognl is an object and a query language for attributes. In ognl, there is a context (called context) of map type. In this context, there is a root element (Root). To access the attributes of the root element, you can directly use the attribute name, however, special characters # Must be added for access to other non-root element attributes #.

 

In struts2, the context is actioncontext, and the root element bit value stack (value stack, value stack represents a family of objects rather than an object, and the action class instance also belongs to a value stack ). The content in actioncontext is as follows:

|

| -- Application

|

| -- Session

Context map --- |

| -- Value stack (Root)

|

| -- Request

|

| -- Parameters

|

| -- ATTR (searches page, request, session, then application scopes)

|

Because the action instance is placed in the value stack, and the value stack is one of the root elements, the access to the attribute in the action can not be marked #, the # flag must be used for other accesses.

 

Reference Action attributes

<S: property value = "postalcode"/>

The attributes of other non-root elements in actioncontext can be accessed as follows:

<S: property value = "# session. mysessionpropkey"/> or

<S: property value = "# session [" mysessionpropkey "]"/> or

<S: property value = "# request [" mysessionpropkey "]/>

 

The action class can use static methods in actioncontext to access actioncontext.

Actioncontext. getcontext (). getsession (). Put ("mysessionpropkey", mysessionobject );

 

Ognl and collection (lists, maps, sets)

 

The syntax for generating the list is {E1, E2, E3 }.

<S: Select label = "label" name = "name"

List = "{'name1', 'name2', 'name3'}" value = "% {'name2'}"/>

The preceding Code generates an HTML select object. The optional values are name1, name2, and name3. The default value is name2.

 

Syntax for generating map: # {key1: value1, key2: value2 }.

<S: Select label = "label" name = "name"

List = "# {'foo': 'foovalue', 'bar': 'barvalue'}"/>

The code above generates an HTML select object. The Foo name indicates the content of foovalue, and the bar name indicates the content of barvalue.

 

Determine whether an object exists in the list:

<S: If test = "'foo' in {'foo', 'bar'}">

Muhahaha

</S: If>

<S: else>

Boo

</S: else>

 

<S: If test = "'foo' not in {'foo', 'bar'}">

Muhahaha

</S: If>

<S: else>

Boo

</S: else>

 

Obtain part of a list:

? -All objects that meet the selection Logic

^-The first object that meets the selection Logic

$-The last object that meets the selection Logic

For example:

Person. Relatives .{? # This. Gender = 'male '}

The above code gets this person (person) All male (this. Gender = male) Relatives)

 

 

Lambda expressions

 

Ognl supports Simple Lambda expression syntaxes. You can use these syntaxes to create simple Lambda functions.

 

For example:

Fibonacci:

If n = 0 return 0;

Elseif n = 1 return 1;

Else return fib (n-2) + fib (n-1 );

FIB (0) = 0

FIB (1) = 1

FIB (11) = 89

 

How does ognl Lambda expression work?

Lambda expressions must be placed inside square brackets. # This indicates the parameters of the expressions. For example:

<S: property value = "# fib =: [# This = 0? 0: # This = 1? 1: # fib (# This-2) + # fib (# This-1)], # fib (11) "/>

 

# Fib =: [# This = 0? 0: # This = 1? 1: # fib (# This-2) + # fib (# This-1)] defines a Lambda expression,

# Fib (11) calls this expression.

 

Therefore, the output of the above Code is: 89

 

In jsp2.1, # is used as a special note for JSP El (Expression Language). Therefore, the use of ognl may cause problems,

A simple method is to disable the El feature of jsp2.1, which requires modifying the Web. xml file:

<JSP-config>

<JSP-property-group>

<URL-pattern> *. jsp </url-pattern>

<El-ignored> true </El-ignored>

</JSP-property-group>

</JSP-config>

 

A brief summary of El Expression Language
 

Basic syntax

I. Introduction to El
1. syntax structure
$ {Expression}
2. [] and. Operators
El provides two operators:. and [] to access data.
When the attribute name to be accessed contains some special characters, such as. Or? If it is not a letter or number, you must use []. For example:
$ {User. My-name} should be changed to $ {user ["My-name"]}
If you want dynamic values, you can use [] instead of dynamic values. For example:
Data in $ {sessionscope. User [data]} is a variable.
3. Variables
El is easy to access variable data, for example, $ {username }. It means to retrieve the variable named username in a range.
Because we do not specify the username of the range, it will search for the range from page, request, session, and application in sequence.
If the username is found on the way, it will be directly returned and will not be found any more, but if all the ranges are not found, null will be returned.
Name of attribute range in El
Page pagination
Request requestscope
Session sessionscope
Application applicationscope

Ii. El implicit object
1. implied objects related to the scope
The El implied objects related to the scope include pagination, requestscope, sessionscope, and applicationscope;
They are basically the same as pagecontext, request, session, and application of JSP;
In El, the four implicit objects can only be used to obtain the range attribute value, that is, getattribute (string name), but cannot obtain other related information.

For example, to obtain the value of the username attribute stored in the session, you can use the following methods:
Session. getattribute ("username") gets the value of username,
Use the following method in El:
$ {Sessionscope. Username}

2. hidden objects related to input
There are two implicit objects related to input: Param and paramvalues, which are special hidden objects in El.

For example, to obtain user request parameters, you can use the following methods:
Request. getparameter (string name)
Request. getparametervalues (string name)
In El, you can use PARAM and paramvalues to obtain data.
$ {Param. name}
$ {Paramvalues. name}

3. Other hidden objects

Cookie
Jstl does not provide the cookie setting action,
For example, you can use $ {cookie. usercountry} to obtain a value named usercountry in the cookie.

Header and headervalues
The header stores the data used by the browser and server for communication.
For example, to obtain the version of your browser, use $ {header ["User-Agent"]}.
In addition, it is rare that the same header name may have different values. In this case, you must use headervalues to obtain these values.

Initparam
Initparam gets the context parameter for setting the web site)
For example, the general method is string userid = (string) application. getinitparameter ("userid ");
You can use $ {initparam. userid} to obtain the userid name.

Pagecontext
Pagecontext obtains other details about user requirements or pages.
$ {Pagecontext. Request. querystring} gets the request parameter string
$ {Pagecontext. Request. requesturl} retrieves the request URL, excluding the request parameter string
$ {Pagecontext. Request. contextpath} indicates the name of the Web application of the service.
$ {Pagecontext. Request. Method} Get the HTTP method (get, post)
$ {Pagecontext. Request. Protocol} gets the used protocol (HTTP/1.1, HTTP/1.0)
$ {Pagecontext. Request. remoteuser} obtains the user name
$ {Pagecontext. Request. remoteaddr} obtains the user's IP address
$ {Pagecontext. session. New} checks whether the session is new.
$ {Pagecontext. session. ID} obtains the session ID.
$ {Pagecontext. servletcontext. serverinfo} obtains the service information of the host.

Iii. El Operators
1. There are five Arithmetic Operators: +,-, * or $,/or Div, %, or mod.
2. There are six Relational operators: = or EQ ,! = Or NE, <or lt,> or GT, <= or Le,> = or Ge
3. There are three logical operators: & and, | or ,! Or not
4. Other operators include empty, conditional, and ().
Example: $ {empty Param. name}, $ {? B: c}, $ {A * (B + C )}

4. El Functions ).
Syntax: NS: function (arg1, arg2, arg3 .... Argn)
NS is the prefix, which must be the prefix of the taglib command.

---------------------------------------------

Supplement:

<% @ Taglib prefix = "C" uri = "http://java.sun.com/jstl/core_rt" %>

Foreach:

<C: foreach items = "$ {messages }"
Var = "item"
Begin = "0"
End = "9"
Step = "1"
Varstatus = "Var">
......
</C: foreach>

 

Out:

<C: Out value = "$ {logininfo. Username}"/>
C: Out> output the content in the value to the current position. Here, the content of the logininfo object
The username attribute value is output to the current position of the page.
$ {......} Is the Expression Language (EL) syntax in jsp2.0. It defines an expression,
The expression can be a constant (as shown above) or a specific expression Statement (such as in the foreach loop body ).
). Typical cases are as follows:
Ø $ {logininfo. Username}
This indicates that the username attribute of the logininfo object is referenced. We can use the "." operator to reference
You can also use "[]" to reference object attributes, for example, $ {logininfo [username]}.
The same effect is achieved with $ {logininfo. Username.
"[]" Indicates that if the attribute name contains special characters, such as "." or "-",
In this case, you must use "[]" to obtain the attribute value to avoid syntax conflicts (avoid as much as possible during system development ).
).
The equivalent JSP script is roughly as follows:
Logininfo =
(Logininfo) Session. getattribute ("logininfo ");
String username = logininfo. GetUserName ();
As you can see, El greatly saves the encoding capacity.
Another problem that is raised here is where El will find the logininfo object.
For expressions such as $ {logininfo. Username },
The variable logininfo is negative. If no value is found, the request, session,
Search within the application range until it is found. If the matched
Return null.
If you need to specify the search range of the variable, you can specify the search range in the El expression:
$ {Pagination. logininfo. Username}
$ {Requestscope. logininfo. Username}
$ {Sessionscope. logininfo. Username}
$ {Applicationscope. logininfo. Username}
In spring, the result data returned by all logical processing units will be placed as the attribute
Return to the httpservletrequest object (for specific implementation, see spring source code .)
Org. springframework. Web. servlet. View. internalresourceview.
Implementation Code of the exposemodelasrequestattributes method), that is, spring
In MVC, The result data objects are requestscope by default. Therefore, in spring MVC,
The following addressing methods should be used with caution:
$ {Sessionscope. logininfo. Username}
$ {Applicationscope. logininfo. Username}
Ø ${1 + 2}
The result is the expression calculation result, that is, the integer 3.
Ø $ {I> 1}
If the variable value is I> 1, return the bool type true. Compared with the previous example, we can find that El will
Returns different data types based on the calculated results of the expression.
Expressions are written in roughly the same way as expressions in Java code.

 

If/choose:

<C: If test = "$ {var. Index % 2 = 0}">
*
</C: If>
The condition is generally an El expression.
<C: If> the else clause is not provided, which may be inconvenient to use. In this case, you can use <C: Choose>
Tag to achieve a similar purpose:
<C: Choose>
<C: When test = "$ {var. Index % 2 = 0}">
*
</C: When>
<C: otherwise>
!
</C: otherwise>
</C: Choose>
Similar to the switch statement in Java, <C: Choose> provides a simplified processing method under complicated conditions. Its
The <C: When> clause in is similar to the case clause and can appear multiple times. The above code outputs the "*" number in an odd number of rows,
The output is "!" in an even row.
---------------------------------------------

Supplement:

1 El expressions are represented by $ {}. All HTML and JSP tags can be used to replace complex Java code on JSP pages.

2 El expressions can operate on constant variables and implicit objects. the most common implicit objects are $ {Param} and $ {paramvalues }. $ {Param} indicates the value of a single string in the request parameters. $ {paramvalues} indicates that a group of values of the request parameters are returned. pagination indicates a variable in the page range. requestscope indicates the variable of the request object. sessionscope indicates the variables in the session range. applicationscope indicates the application range variable.

3 <% @ page iselignored = "true" %> indicates whether the El language is disabled. True indicates that the El language is disabled. False indicates that the El language is enabled by default in jsp2.0.

4 The El language can display logical expressions. For example, $ {true and false}, the result is a false relational expression. For example, $ {5> 6}, the result is a false arithmetic expression. For example, ${5 + 5}, the result is 10

5. The search range of variables in El is: the page request session application vertex operator (.) and "[]" indicate the value of the variable. The difference is that [] can display non-word class variables.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/axzywan/archive/2008/07/12/2643921.aspx

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.