EL expression in JSP _ dynamic node Java school arrangement, jspel

Source: Internet
Author: User

EL expression in JSP _ dynamic node Java school arrangement, jspel

The JSP page supports EL expressions. The full name of EL is Expression Language. The main functions of EL expressions are:

① Obtain data;

② Perform operations;

③ 11 hidden objects using EL expressions;

④ Call the Java method.

Of course, EL expressions can also be used with JSTL labels to display other functions, such as iterations.

Let's first look at how to get data using EL expressions.

In the form of $ {identifier} On the JSP page, the JSP Engine is notified to call pageContext In the Servlet. findAttribute ("identifier") is used to obtain data. Specifically, an identifier is used as a keyword to search for objects or attributes from each domain. If it is found, it is returned as a string and displayed on the JSP page. If it is not found, it is displayed as a Null String. This is different from the <jsp: getProperty> tag, it also shows that EL expressions are better than this label.

Example 1: obtain common data in a domain

<%     String name = "Ding";     request.setAttribute("personName", name);   %>      ${personName }

Observe in the browser:

Example 2: Get the object or attribute in the domain

<%    Person p = new Person("Ding",25);    request.setAttribute("person", p);   %>      ${person }      <br>   ${person.name }  <br>

Observe in the browser:

Example 3: Get the attributes of an object

In the domain package, two JavaBean:

Public class Person {private String name; private int age; private Address address Address ;... Omitting the get and set methods for each attribute} public class Address {private String city ;... The get and set methods of the city attribute are omitted}

The Code on the JSP page is:

<%      Person p = new Person();      Address a = new Address();      a.setCity("Amoy");      p.setAddress(a);      request.setAttribute("person", p);   %>      ${person.address.city }

Observe in the browser:

Note:In this type of object, attributes are still objects. Note that the attribute names must be consistent in JSP; otherwise, an exception is thrown.

Example 3-2: Use the EL expression in the web project to obtain the current web project path

$ {PageContext. request. contextPath}

The value of this EL expression is:

Note that there is a slash at the beginning of the expression "/". Here, the "pageContext" in the EL expression is an implicit object in EL. For details, refer to the next blog.

Example 4: retrieve the elements in the List set

<%    List<Person> personList = new ArrayList<Person>();    personList.add(new Person("Ding",25));    personList.add(new Person("LRR",24));    request.setAttribute("persons",personList);   %>      ${persons[0].name } love ${persons[1].name}

Observe in the browser:

Example 5: retrieve the elements in the Map set

<%    Map<String,Person> personMap = new HashMap<String,Person>();    personMap.put("p1",new Person("Ding",25));    personMap.put("p2",new Person("LRR",24));        request.setAttribute("pMap", personMap);   %>      ${pMap.p1.name }  <br>   love <br>   ${pMap['p2'].name }

Observe in the browser:

In addition ". "to get the value corresponding to the key in the attribute or Map set, you can also use" ['identifier'] "to get the value (note that there are single quotation marks in brackets ), this is especially used when the identifier is a number ". "will report an error, and" ['identifier'] "will not report an error.

Note: The EL expression can get the object attributes because the get method is provided for the fields in the object, so it becomes an attribute. EL can only get the attributes of the object, but cannot get the fields of the object, this must be clear.

EL expression deficiency: No EL expression can be used to iterate a set from a List set or a Map set. Therefore, if you need to iterate a set, EL expression must be used with the JSTL label.

EL expressions support operations, including arithmetic operations, logical operations, and relational operations:

Syntax: $ {operation expression}

(Arithmetic Operators are omitted here)

Example 6:

<%  request.setAttribute("username", "root");  request.setAttribute("password", "123");  %>  ${username == "root" && password == "123" }

Displayed in the browser: true.

In addition to the preceding three simple operators, EL expressions also support empty and binary expressions (expressions? Value 1: Value 2)

Empty OPERATOR: checks whether the object is null or whether a certain data is a "" null String

Example 7:

<%      request.setAttribute("person", null);      request.setAttribute("address", "");      request.setAttribute("user", new User());   %>   ${empty(person)}   <br>   ${empty(address)}  <br>   ${empty(user)}     <br>

Observe in the browser:

Binary Expression:

Example 8:

<% User user = new User (); user. setUsername ("fjdingsd"); pageContext. setAttribute ("user", user); %> welcome: $ {user! = Null? User. username :''}

Observe in the browser:

In the preceding example, the Binary Expression of EL expression is used. If the user object is not null, the username attribute of this object is output; otherwise, null characters are output.

Another use of binary expressions can also be used for data echo. If a user wants to modify its registration information, then the server (or from the database) when returning data to the display page, the edited information should be displayed again.

Example 9:

<% Request. setAttribute ("gender", "female"); %> <input type = "radio" name = "gender" value = "male" $ {gender = 'male '? 'Checked': ''}> male <input type =" radio "name =" gender "value =" female "$ {gender = 'female '? 'Checked': ''}> female

The browser displays:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.