Struts 2 tag Library (1) -- tag library Basics

Source: Internet
Author: User

1. Why label?

When a JSP page is embedded with a large number of Java scripts, the readability of the entire page decreases, and the readability also decreases.

Advantages of using custom tags: simple, conducive to team collaboration, high readability.

Import the struts 2 tag Library: <% @ taglib prefix = "s" uri = "/struts-tags" %>

2. Struts 2 defines all labels in the space where URI is "/struts-tags". However, we can still simply divide Struts 2 labels into the following three categories:

1) UI tags, that is, user interface tags, are mainly used to generate tags for HTML elements.

2) Non-UI tags, mainly used for data access, logical control, and other tags

3) Ajax labels

3. Supported OGNL expressions for Struts 2

OGNL is the abbreviation of Object-Graph Navigation Language. It is a powerful Expression Language (EL). It uses simple and consistent Expression syntax, you can access any property of an object, call the object method, traverse the structure of the entire object, and convert the field type. It uses the same expression to access the attributes of an object. OGNL is usually used together with the Struts 2 flag. The usage of the three symbols #, %, and $.

L "#" has three main purposes:

① Access OGNL context and Action context, # equivalent to ActionContext. getContext ();

Parameters contains the Map # parameters. id [0] of the current HTTP request parameter, which is equivalent to request. getParameter ("id ")

The request contains the Map of the attribute of the current HttpServletRequest # request. userName is equivalent to request. getAttribute ("userName ")

Map # session. userName of the attribute that contains the current HttpSession is equivalent to session. getAttribute ("userName ")

Map # application. userName of the application that contains the attribute of the current application's ServletContext 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 scope of the preceding three ranges in order until it is found.

② Used to filter and project sets, such as books .{? # This. price <100 };

③ Construct Map, syntax structure # {key1: value1, key2: value2 ,......}. For example, # {'foo1': 'bar1', 'foo2': 'bar2 '}.

L "%" is used to calculate the value of the OGNL expression when the flag attribute is of the string type.

L "$" has two main purposes: to reference OGNL expressions in international resource files.

 

1) OGNL provides two element operators: in and out in, which determine whether an element is in a specified set.

For example:

<S: if test = "'foo' in {'foo', 'bar'}"> include </s: if> <s: else> not included </s: else> <s: if test = "'foo' not in {'foo', 'bar'}"> does not contain </s: if> <s: else> include </s: else>

2) In addition, OGNL allows a rule to obtain a subset of a set. There are three operators:

①? : Retrieve all elements that conform to the selection Logic

② ^: Retrieve the first element of all the selection Logic

③ $: Retrieve the last element that conforms to the selection Logic

person.relatives.{? #this.gender == 'male'}

Retrieve all relatives whose gender is male.
3) Access static members with OGNL

By default, Struts 2 disables static access methods and only allows access to static fields through OGNL expressions. To enable OGNL to access static members, configure the following settings in struts. xml:

<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>

Once set, the OGNL expression can access static members through the following syntax:

@ ClassName @ staticField

@ ClassName @ staticMethod (val ...)

<Body> access system environment variable: <s: property value = "@ java. lang. system @ getenv ('java _ home') "/> <br/> value of the circumference rate: <s: property value =" @ JAVA. lang. math @ PI "/> </body>

4) Lambda (Lambda) Expressions

Simple functions can be used in OGNL, such as the Fibonacci sequence.

If n = 0 retrun 0;

Else if n = 1 return 1;

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

Ask for fib (11)

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

The above expression can output the fib (11) value.

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.