Java Learning Note-el Expressions (38)

Source: Internet
Author: User
Tags tld

El expression

El is the expression Language, and the main task is to help developers simplify getting domain properties.

In general, however, the use of El and Jstl is required.

Grammar:

${

Implicit objects | Properties in a direct write field

}

? Arithmetic Operations<!--arithmetic Operations-- -+Ten= ${ -+Ten} <br/> --Ten= ${ --Ten} <br/> -*Ten= ${ -*Ten} <br/> -/Ten= ${ -/Ten} <br/> -%Ten= ${ -%Ten} <br/>
 ? Comparison Operation  <!--comparison operation--20  > 10 = ${20  > 10 } <br/> 20  < 10  = ${20  < 10 } <br/> 20  >= 10  = ${20  >= 10 } <br/> 10  = = 10  = ${10  = 10 } <br/> ${str  = = null }
   
?     NULL " Welcome ":" register "}
?     >1<2 }<br/>  

One get domain object (emphasis)

1. General Data

<!--get normal properties                --<% Request.setattribute ("name"," Jack " );       %>      ${name}<br/>

The above code is not assigned to any domain space when acquiring domain properties, then the underlying domain must be detected in a small-to-large way.

Pagecontext.findattribute (""); à realization principle

2 Getting Array properties

<!--get Array properties-      -<%         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, it is a collection to use.

3 List Collection

<!--get Array properties-<%List<String> list =NewArraylist<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 the Map property

<%Linkedhashmap<String,String> map =NewLinkedhashmap<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 operation

<!--get Bean properties in map--<%Linkedhashmap<String,Cat> cats =NewLinkedhashmap<string,cat>(); Cats.put ("001",NewCat ("a cat",3,"Persian cat")); Cats.put ("002",NewCat ("b Cat",3,"Civet Cats")); Cats.put ("003",NewCat ("C Cat",3,"Garfield Cat")); 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 Hidden Objects

Pagescope

Requestscope

Sessionscope

Applicationscope

If you do not specify a specific domain when you get the domain properties, then one-by-one lookup is time-consuming, so I hope you will specify the domain name when the domain name.

PageContext the object can get the request object

Example:

<!--hidden Objects--${pagecontext.request.method}<br/>${pagecontext.request.servletpath}<br/>${pagecontext.request.contextpath}<br/>${pagecontext.request.protocol}<br/>

GET request Parameter Paramparamvalues
Example: ${param.name}<br/>${paramvalues.likes[0]}<br/>

GET request header information Headerheadervalues
Example: ${header["Accept"]}<br/>Initparam
Get Cookiecookie
Example: ${cookie}<br/>${cookie.address}<br/>${cookie.address.name}<br/>${cookie.address.value}<br/>
ELand function libraries

Problem: When the data is displayed, it is often necessary to call some methods to perform basic processing of the data that needs to be displayed, such as:

Data filtering, sub-strings and other operations. Then you need to use the EL expression for a quick function call.

    1. Introducing the Htmlfilter.java class
    2. Description as function

2.1 Creating a Myfun.tld file in 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 Introducing a library of functions

% @taglib uri= "/web-inf/myfuns.tld" prefix= "JNB"%

1.2 Using the EL call function

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

Attention:

    1. You cannot continue to nest El Expressions in an El expression
    2. The function must be static when defining a function.
Summarize

Jstl always has 5 libraries, but since the JSP is mainly for data display, we only learn the core library and function library.

Java Learning Note-el Expressions (38)

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.