Summary of expression language

Source: Internet
Author: User
the ideal function of JSP:

1. Receiving properties;

2. Judgment;

3. Output;

4. Use Scriptlet as little as possible;

Therefore, the JSP should be as far as possible to reduce the function to this extent.


introduction of expression language

The expression language is designed to allow fewer Java code to be included in the JSP, and the output can be easily completed with an expression language;

Therefore, the purpose of the expression language: more convenient display;

form of general expression language: ${...}


A simple example of an expression language:

Request.setattribute ("name", "Xiazdong");

${name} can be displayed;

The advantages of an expression language:

The traditional way to get properties:

<%@ page contenttype= "text/html" pageencoding= "GBK"%>

Because you also need to determine whether the case is empty, if NULL, it will output null;

However, the expression language does not need to be considered, and if NULL, it is converted to an empty string of "";

<%@ page contenttype= "text/html" pageencoding= "GBK"%>


Three, expression language built-in objects

1.pageScope, Requestscope, Sessionscope, applicationscope built-in objects


Useful when you get properties that are set by built-in objects. This is to get the properties of page, request, session, application settings, such as:

Pagecontext.setattribute ("name", "Xiazdong");

Request.setattribute ("name", "Xiazdong");

${pagescope.name} can get the properties of the page;

${requestscope.name} can obtain the attributes of the request;


2.param and paramvalues built-in objects


Used to receive the incoming parameters;

PARAM.A similar to Request.getparameter ("a");

PARAMVALUES.A similar to Request.getparametervalues ("a");

If you want to get the value of the second A, pass ${paramvalues.a[1]};


3.pageContext built-in Objects


PageContext object can obtain Request,session,application object;

Pagecontext.request access to request built-in objects;

Pagecontext.session gets the session built-in object;

Pagecontext.application obtain Application object;

You can also invoke methods for JSP built-in objects: for example: (All are called by reflection)

(1) ${pagecontext.request.remoteaddr}; call Getremoteaddr ();

(2) ${pagecontext.session.new}; call IsNew () function

(3) ${pagecontext.session.id}; call GetID () function


Iv. expression Language for collections

We can use the expression language to facilitate the operation of the collection, such as:

(1)

list<integer> list = new arraylist<integer> ();

Request.setattribute ("A", list);

${a[0]} means to get the first element in the queue;

(2)

Map m = new HashMap ();

Request.setattribute ("Map", m);

${map["A"]} returns the value of Key=a;

${map.a} Returns the value of Key=a;


Example:

<%@ page contenttype= "text/html" pageencoding= "GBK" import= "java.util.*"%>  


v. Expression language for MVC

The MVC design pattern is to pass the results to the JSP page through the servlet, and if you follow the general idea, you need to import the VO package into the JSP because you need to get the attributes through the Vo object;

Vo is the person class, then the JSP needs to be request.getattribute ("person") with person per = ("person"); Get the person object and then display it, from what you can see, we have to import the person class. However, JSP is best allowed to import only java.util.* packages;
If you are based on an expression language, you can write in the servlet:
Request.setattribute ("Vo", Vo object); Set properties, because RequestDispatcher is a server jump, so you can get results through ${VO. Properties} in the JSP, so you don't have to import VO packages;


The attribute of Vo can be invoked conveniently by means of an expression language;

Class a{

Private String A;

}


1. Passing a Vo object


A a1 = new A ();

Request.setattribute ("obj", A1);

${OBJ.A} obtains a property of the A1 object through reflection;


2. Passing multiple VO objects


List<a> as = new arraylist<a> ();

Request.setattribute ("as", as);

In the JSP page:

List as = (list) Request.getattribute ("as");

Iterator iter = As.iterator ();

while (Iter.hasnext ()) {

pagecontext.setattribute ("obj", Iter.next ());

${OBJ.A}//Call a property of Class A

}

Central:

(1) Receive list

(2) Putting each element into the PageContext attribute and displaying it through an expression language;


VI, operator

Expression-language support

+-*/% arithmetic operator;

= =,. =, >= and other comparison operators;

&&,. , | | such as logical operators;

。 :, empty and other special symbols;

Example:

Request.setattribute ("Num1", 20);

Request.setattribute ("num2", 30);

${NUM1-NUM2} represents a subtraction operation

${NUM1>=NUM2} represents a comparison operation

${Empty Num2} Indicates whether the decision is empty

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.