El expression, jstl tag library

Source: Internet
Author: User
Tags control label tld

One, EL (expression Language) expression

Syntax structure:${var}

To deactivate an evaluation of an EL expression, you need to set the Iselignored property value to true using the page directive:

<%@="True|false" %>  This way, the El expression is ignored. If set to False, the container evaluates the EL expression. 

Application of El Expression:

    • Get data: Used to replace script expressions in JSPs, retrieve Java objects from a web domain, fetch data.
    • Perform operations: Perform some basic logical operations, relational operations, arithmetic operations.
    • Get Web Development Common objects: Using an implicit object defined by El, you get a reference to the web's common objects to get the data in those objects.
    • Call Java method: El allows custom El functions to invoke methods of Java classes through El Expressions.
1Using a two-tuple expression in an 2<%3Session.setattribute ("User",NewUser ("Aloof and pale Wolf")));4%>5${user==NULL? "Sorry, you didn't log in": User.username}6 7 8<%9List<string> emptylist =NULL;Ten%> One<%--use the empty operator to check if the object is null (empty)--%> A<c:ifTest= "${empty (emptylist)}" > - Sorry, there is no data you want to see -&LT;/C:if>

The EL expression language defines 11 hidden objects, all of which are java.util.Map types, which make it easy to get some common objects in web development and read the data of those objects

1<br/>---------------1, PageContext object: Gets the PageContext object in the JSP page------------------------<br/>2 ${pagecontext}3<br/>---------------2, Pagescope objects: Finding data from the page field (pagescope)------------------------<br/>4<%5Pagecontext.setattribute ("name", "Zhang San");//Map6%>7 ${pagescope.name}8<br/>---------------3, Requestscope objects: Get data from the request domain (Requestscope)------------------------<br/>9<%TenRequest.setattribute ("name", "John Doe");//Map One%> A ${requestscope.name} -<br/>---------------4, Sessionscope object: Get Data from Session field (Sessionscope)------------------------<br/> -<% theSession.setattribute ("User", "XDP");//Map -%> -${sessionscope.user}

The EL expression language can access the EL function. The El function actually corresponds to a Java class method, which must be defined as a public type, and the method that acts as a function must be declared as a public static type. When the Java class is defined, the method of the Java class should be mapped to a function in the tag descriptor (TLD) file.

EL Function:

In general, the development and application of El Custom functions consists of the following three steps:
1. Write a static method of the Java class.
2. Write a tag library descriptor (TLD) file that describes the custom function in the TLD file.
3. Import and use the Custom function in the JSP page.

For details, refer to: (1) http://www.cnblogs.com/xdp-gacl/p/3938361.html (1.5EL function Development Step)

(2) http://blog.csdn.net/goskalrie/article/details/51315397 (custom and use El function)

Second, JSTL (JavaServer Pages standard tag library,jsp tag library)

Jstl is an extension of Apache to El expressions (that is, Jstl relies on EL), and Jstl is the tag language. The JSTL tag has been very handy since it was used, just like the JSP action tag, except that it is not a JSP built-in tag, we need to guide the package ourselves, and specify the tag library.

1. The JSTL tag library contains four classes:

    • Core: Key tag library, learning focus
    • FMT: Format Tag Library, learn only two
    • SQL: Database tag library, obsolete, not learning
    • Xml:xml Tag Library, obsolete, not learning

2. Import tag library with taglib instruction

In addition to the JSP action tag, the use of other third-party tag libraries requires: (1) Guide package, (2) Use the TAGLIB directive in the JSP page using the tag to import the tag library.

Import JSTL Core Tag library:<%@ taglib prefix= "C"uri= "http://java.sun.com/jstl/core"%>

    • Prefix= "C": Specify the tag library prefix, this prefix can be given a random value, but everyone will use the core tag library when the prefix is specified as C;
    • Uri= "Http://java.sun.com/jstl/core": Specify the URI of the tag library, it is not necessarily a real URL, but it can let JSP find the tag library description file;

3, core tag library (CORE)

Jstl has a total of 13 core tag library tags, divided into 4 categories:

1. Expression control Tags: out, set, remove, catch

2. Process Control Label: If, choose, when, otherwise

3. Loop Label: ForEach, Fortokens

4.URL action tag: import, URL, redirect

(1) <c:out> tags are primarily used to output the contents or results of data Objects (strings, expressions). The use of Jstl and El expression can not be separated from the,<c:out> has a specific result processing function, El alone will reduce the readability of the program, it is recommended to put the El result input into the <c:out> tag.

(2) <c:set> tags are used to place an object within a specified domain scope, or to store an object in a map or JavaBean object.

  <c:set value= "Zhao Wu" target= "${person}" property= "name" ></c:set>
<c:set target= "${person}" property= "Age" >19</c:set>
<li> get the name value of the object person from the bean: <c:out value= "${person.name}" ></c:out></li>
<li> gets the age value of the object person from the bean: <c:out value= "${person.age}" ></c:out></li>

(3) <c:remove> tags are used primarily to remove specified variables from the specified JSP scope.

(4) <c:catch> tags are used to capture the exception that is thrown in the content nested within the tag body.

(5) The <c:if> tag is the same as the IF statement in the program to achieve conditional control.

<%--uses the If label to determine and assign the result of the test to Adminchock, which is stored in the default page range. --%>  <c:if test= "${param.uname== ' admin '}" var= "Adminchock" >

(6) <c:choose>, <c:when> and <c:otherwise> These 3 tags are typically used together with the,<c:choose> tags as <c:when> and < The parent tag of the c:otherwise> tag to use. Using <c:choose>,<c:when> and <c:otherwise> three tags, you can construct complex conditional judging structures similar to "If-else If-else".

(7) <c:forEach> tag the elements in the collection (Collection) are traversed according to the loop condition.

(8) <c:import> the label can contain other static or dynamic files to this JSP page, and the difference between <jsp:include> is:<jsp:include> can only contain files in the same web app. <c:import> can include files from other web apps, even resources on the network.

(9) <c:url> tags are used to construct a URL address in a JSP page whose main purpose is to implement URL rewriting.

<c:redirect> the label is used to implement the redirect of the request. You can also add the specified parameters to the URL with the <c:param> tag.

4. FMT Tag Library

The FMT tag library is used to format the output, which usually requires formatting with time and numbers:

(1) Format date

<%@ taglib prefix= "FMT" uri= "http://java.sun.com/jsp/jstl/fmt"%>  ...   <%      new  Date ();      Pagecontext.setattribute ("D", date);   %>  <fmt:formatdate value= "${d}" pattern= "Yyyy-mm-dd HH:mm:ss"/>

(2) Formatting numbers

<%      Double d1 = 3.5;       double d2 = 4.4;       Pagecontext.setattribute ("D1", D1);      Pagecontext.setattribute ("D2", D2);   %>  <fmt:formatnumber value= "${d1}" pattern= "0.00"/><br/>  <fmt:formatnumber value= "${ D2} "pattern=" #.## "/>

5. Custom Jstl Tags

See: http://blog.csdn.net/qq_25827845/article/details/53311722 (Custom label)

All of these are simple learning processes.

Resources:

EL:

(1) http://www.runoob.com/jsp/jsp-expression-language.html

(2) http://blog.csdn.net/goskalrie/article/details/51315397

(3) http://www.cnblogs.com/xdp-gacl/p/3938361.html

(4) http://blog.csdn.net/qwerasdf123/article/details/4189889

Jstl:

(5) http://www.runoob.com/jsp/jsp-jstl.html

(6) http://blog.csdn.net/qq_25827845/article/details/53311722

(7)http://www.cnblogs.com/lihuiyy/archive/2012/02/24/2366806.html

(8) http://www.cnblogs.com/xdp-gacl/p/3943390.html

El expression, jstl tag 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.