JSP Series 7: JSP expressions (El expressions)

Source: Internet
Author: User

 

1. Expression Language: Expression Language is a concise method for generating dynamic content.

1. jsp2.0's most important two features: JSP syntax defines custom labels and expression languages.

2. to calculate and output the values of Java objects stored in the standard position.

* The syntax is simple and effective.
* Conditional output.
* Precisely access storage objects.
* Simple use of bean attributes.
* Simple access to the collection elements.
* Simple access to request parameters, cookies, and other request data.
* Automatic type conversion.
* The Null Value replaces the error message.

* Operators in the expression language should be used in simple tasks that represent logic (data), so as not to be used in commercial logic (create and process data) and put commercial logic in Java classes.

3. El expression syntax:
$ {Expression}

* It Can Be Used in template text, custom, and standard action labels. But it cannot be used in scripts.

4, Operator
[] (Array method) or. (dot method) Operator: used to access data.
Arithmetic Operators
Relational operators
Logical operators
Empty OPERATOR: checks whether the value is null or empty.
Conditional Operators
Bracket Operator

5. array notation [] and dot notation .:

* Attribute names can be calculated during the request. The values in the brackets can be variables. Allow the use of values that cannot be valid attribute names to be useful in access integration and request headers.

2. Block the use of expression languages
1. Disable the expression language in the entire web application.
If you use a web. xml file of servlet2.3 or earlier versions, the jsp2.0 Expression Language is automatically disabled in the Web application.

2. Disable the Expression Language in multiple JSP pages.
Use the JSP-property-group element in Web. XML to specify the corresponding page.

<JSP-config>
<JSP-property-group>
<URL-pattern> relative path of the JSP page </url-pattern>
<El-ignored> true or false </El-ignored>
</JSP-property-group>
</JSP-config>

3. Disable the expression language on some pages.
Use the iselenabled attribute of the page command. It can only be used in jsp1.2.

4. Disable individual statements in the expression language.
Replace $ with & #36. If the jsp2.0 page uses the expression language and $ {characters, use/$ {to output $ {.

3. Block the use of standard script elements.
 
Use the scripting-invalid sub-element of the JSP-property-group element to force this constraint.

<JSP-config>
<JSP-property-group>
<URL-pattern> the relative path of the JSP page or all JSP pages in the *. jsp table are restricted </url-pattern>
<Scripting-invalid> true or false </Scripting-invalid>
</JSP-property-group>
</JSP-config>

Iv. Scope variables in expressions

* Scope object:
To output the attribute value in the scope, you only need to use the attribute name in El.
El automatically searches for attributes by pagecontext, httpservletrequest, httpsession, and servletcontext from the scope object.
You can also manually use pagination, requestscope, sessionscope, and applicationscope to specify the attribute range.

If the property is found, the tostring method is called and the call result is returned. The Null String instead of null or error message is not returned.

* Request parameters:
Param: equivalent to the getparameter (string name) method.
Paramvalues is equivalent to the getparametervalues (string name) method and returns an array consisting of all values of the specified parameter name.
 
* Request header:
Header and headervalues: Specify and all values for accessing the HTTP request header. Header. Header

* Cookie Access:
Cookie returns a cookie object instead of a value. You must use the Value Attribute of the cookie class to obtain the value. Cookie. usercookie. Value

* Initialization parameters:
Initparam access to context initialization parameters. Initparam. Parameter Name
 
V. Expression of JavaBean and El

* The dot mark method is provided for bean attribute access. The system performs reflection to analyze the internal structure of the object. Returns the attribute value.

* Nested access attributes are supported.

Vi. Access Collection
Allow access to different types of sets in the same way: $ {attributename [entryname]}

* Attributename is an array object and entryname is a subscript.

* Attributename implements the object of the List interface, and entryname is the index.
 
* Attributename implements the object of the map interface, and entryname is the key.

* Entryname in [entryname] can be a variable.

7. Use Functions
1. Define a function: Write a Java class to declare the method as static.
2. correlated function: configure the function using the <function> element in the TLD.
3. Use Function :$ {Prefix: function name (param1, param2 ,......)}

 

Related Article

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.