Java learning notes-EL expression (38), learning notes el

Source: Internet
Author: User

Java learning notes-EL expression (38), learning notes el
EL expression

EL is Expression Language. The main task is to help developers simplify the acquisition of domain attributes.

However, EL and JSTL are usually used in combination.

Syntax:

$ {

// Implicit object | directly write the attributes in the domain

}

Arithmetic Operation <! -- Arithmetic operation --> 20 + 10 =$ {20 + 10} <br/> 20-10 =$ {20-10} <br/> 20*10 =$ {20 * 10} <br/> 20/10 =$ {20/10} <br/> 20% 10 =10 {20% 10} <br/>
Comparison calculation <! -- Comparison calculation --> 20> 10 =$ {20> 10} <br/> 20 <10 =$ {20 <10} <br/> 20> = 10 =$ {20 >=10 }< br/> 10 ==10 =$ {10 ==10} <br/>$ {str = null}
Three-object operator $ {name! = Null? "Welcome": "Registration "}
Logical operation $ {12> 10 & 1 <2} <br/>

1. Obtain domain objects (key points)

1. Common Data

<! -- Get common attributes --> <% request. setAttribute ("name", "jack"); % >$ {name} <br/>

The above code is not specified to any domain space when obtaining the domain attributes, so the underlying layer must be checked in a small to large way.

PageContext. findattrià (""); à implementation principle

2. Get array attributes

<! -- Get array Attributes --> <% int [] ins = {1, 2, 3, 4}; request. setAttribute ("arrs", ins ); % >$ {arrs} <br/>$ {arrs [2]} <br/>$ {arrs ["2"]} <br/> this method is not used for arrays., is a collection.

3 List set

<! -- Get array Attributes --> <% List <String> list = new ArrayList <String> (); list. add (0, "jiao"); list. add (1, "ning"); list. add (2, "bo"); request. setAttribute ("list", list ); % >$ {list} <br/>$ {list [0] }< br/>$ {list ["0"]} <br/>

4. Get Map attributes

<%         LinkedHashMap<String,String> map =  new LinkedHashMap<String,String>();        map.put("001","ooooo");        map.put("002","fffff");        map.put("003","uuuuu");        request.setAttribute("map",map);      %>      ${map }<br/>      ${map[001] }<br/>      ${map["001"] }<br/>      <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>      <c:forEach items="${map}" var="entry">          ${entry.key } = ${entry.value }<br/>      </c:forEach>

6 Bean operations

<! -- Get Bean attributes in Map --> <% LinkedHashMap <String, Cat> cats = new LinkedHashMap <String, Cat> (); cats. put ("001", new Cat ("A Cat", 3, "Persian Cat"); cats. put ("002", new Cat ("B Cat", 3, "raccoon Cat"); cats. put ("003", new Cat ("C Cat", 3, "Garfield"); request. setAttribute ("cats", cats); %> <c: forEach items = "$ {cats}" var = "entry" >$ {entry. key }=$ {entry. value. name}, $ {entry. value. age}, $ {entry. value. type} <br/> </c: forEach>

7. EL implicit object

Pagination

RequestScope

SessionScope

ApplicationScope

If you do not specify a specific domain when obtaining the domain attribute, it is time consuming to search one by one. Therefore, we hope that you must specify a domain name when specifying the domain name.

PageContext this object can get the request object

Example:

<! -- Implicit object -- >$ {pageContext. request. method} <br/>$ {pageContext. request. servletPath} <br/>$ {pageContext. request. contextPath} <br/>$ {pageContext. request. protocol} <br/>

Obtain the request parameter paramparamValues
Example: $ {param. name} <br/>$ {paramValues. likes [0]} <br/>

GET request header information headerheaderValues
Example: $ {header ["accept"]} <br/> initParam
Obtain Cookiecookie
Example: $ {cookie} <br/>$ {cookie. address} <br/>$ {cookie. address. name} <br/>$ {cookie. address. value} <br/>
EL and function library

Problem: When displaying data, you often need to call some methods to perform basic processing on the data to be displayed, such:

Filter data and obtain substrings. Then you need to use the EL expression for quick function calls.

2.1 create a myfun. tld file in the WEB-INF

<?xml version="1.0" encoding="UTF-8"?><taglib xmlns="http://java.sun.com/xml/ns/j2ee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"  version="2.0">  <tlib-version>1.0</tlib-version>  <short-name>jnb</short-name>  <function>    <name>filter</name>    <function-class>cn.itcast.utils.HTMLFilter</function-class>    <function-signature>java.lang.String filter(java.lang.String)</function-signature>  </function></taglib>  

2.2 introduce function libraries

% @ Taglib uri = "/WEB-INF/myfuns. tld" prefix = "jnb" %

1.2 use EL to call Functions

<Td >$ {jnb: filter (entry.value.info)} </td>

Note:

Summary

JSTL has a total of five databases, but since JSP is mainly used for data display, we only learn the core library and function library.

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.