Use of jstl and El Operators

Source: Internet
Author: User

Use jstl

The jstl tag library consists of the following tags:

  • Core: Core tag, process control, display of scope variables, exception handling, and URL Processing;
  • Formatting: i18n formatting related to dates, times, numbers, etc;
  • XML: parses XML and converts the document format through XSL;
  • SQL: the processing of SQL database operations.

Core and formatting labels are commonly used. XML labels are used for data display in specific scenarios, and SQL labels are used for Agile Development (For separation of labor and attention, J2EE development often uses Dao to encapsulate SQL operations in Java code.)

The jar package that must be introduced is:Jstl. jar, standard. Jar

Introduced in JSP:

  • <% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>
  • <% @ Taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "FMT" %>
  • <% @ Taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "FN" %>
  • <% @ Taglib uri = "http://java.sun.com/jsp/jstl/ SQL" prefix = "SQL" %>
  • <% @ Taglib uri = "/Struts-tags" prefix = "S" %>

 

Core Library

1. <C: Choose>An action is used to control the processing of nested <C: When> and <C: otherwise> actions. It allows only the <C: When> actions with the first test expression to be calculated as true to be processed; if the test expression of all <C: When> actions is calculated as false, A <C: otherwise> action is processed. <C: Choose> A tag has no attributes and can be considered as a parent tag. <C: When> and <C: otherwise> are used as their subtags.

<C: Choose>

<C: whentest = "$ {product. onsale}">

$ {Product. salesprice} on sale!

</C: When>

<C: otherwise>

$ {Product. Price}

</C: otherwise>

</C: Choose>

2. <C: foreach>This action accepts the types listed in the Attribute Table.

<C: foreach items = "$ {ATTS}" Var = "item" varstatus = "S">
<H2> <C: Out value = "$ {item}"/> </H2>
Index: $ {S. Index} </BR>
Count: $ {S. Count} </BR>
First: $ {S. First} </BR>
Last: $ {S. Last} </BR>
</C: foreach>

3.<C: fortoken>A tag can be used to separate a specified string based on a separator.

<C: fortokenitems = "a, B, c, d, e, f, g" delims = "," Var = "item>
$ {Item}

</C: fortoken>

<%
String phonenumber = "123-456-7899 ";
Request. setattribute ("userphone", phonenumber );
%>

<C: fortokensitems = "$ {userphone}" delims = "-" Var = "item">
$ {Item}

</C: fortokens>

4. <C: If>The body of an action is calculated only when the specified expression is calculated as true.

<C: If test = "$ {fn: length (mymark) = 0}">

</C: If>

5.: <C: Out>A tag is the most common label used to display data in JSP. It is used to replace the output object value through the JSP internal object out or the <% = %> label.

Your username is: <C: Out value = "$ {user. Username}" default = "guest"/>

6. <C: param>It is used to pass parameters for pages that contain or redirect

<C: redirecturl = "/myhtml. jsp">

<C: Param name = "username" value = "RW"/>

</C: Redirect>

This example will pass the specified parameter "username = 'rw '" For the redirected "myhtml. jsp '"

7. <C: Remove>Label is used to delete variables that exist in the scope.

<C: Remove Var = "username" Scope = "session"/>

8. <C: Set>Tags are used to assign values to variable attributes in variables or JavaBean.

<C: Set Value = "value" Var = "varname" [scope = "{page | request | session | application}"]/>

9. <C: URL>Mainly used to generate a URL

<A href = "<C: urlvalue =" http://www.javafan.net "> <C: paramname =" Param "value =" value "/> </C: URL> "> JAVA fans </a>

If <C: URL> has the VaR attribute, the URL will be stored in varname without directly outputting the URL.

Functions library (used with El statements)

1. FN: ContainsThe function is used to determine whether the source string contains a substring. The returned result is a Boolean value.

$ {Fn: Contains ("ABC", "A") }$ {fn: containsignorecase ("ABC", "A")} // ignore case sensitivity

2. FN: endswithThe function is used to determine whether the source string matches a series of specific word tails. It is the same as the FN: startswith function.

$ {Fn: endswith ("ABC", "BC ")}

3. FN: escapexmlFunction is used to convert all special characters into character entity codes.

$ {Fn: escapexml (Param: INFO )}

4. FN: indexofFunction is used to obtain the starting position of the substring matching the source string.

$ {Fn: indexof ("ABCD", "ABC ")}

5.Length FunctionsFN: LengthSolved the problem that the set size cannot be obtained through the El expression "$ {collection. Size.

<% Arraylist arraylist1 = new arraylist ();

Arraylist1.add ("AA ");

Arraylist1.add ("BB ");

Arraylist1.add ("cc ");

%>

<% Request. getsession (). setattribute ("arraylist1", arraylist1); %>

$ {Fn: length (sessionscope. arraylist1 )}

Assume that an arraylist instance is "arraylist1" and three string objects are added to it. After using the FN: length function, the returned result is "3"

6. FN: replaceFunctions allow replacement of source strings

$ {Fn: Replace ("ABC", "A", "B ")}

7. FN: SplitFunction is used to convert a group of strings separated by delimiters into string arrays.

$ {Fn: Split ("A, B, C ",",")}

8. FN: substringFunction is used to intercept strings.

$ {Fn: substring ("ABC", "1", "2 ")}

9. FN: tolowercaseThe function allows you to convert all characters in the source string to lowercase characters.

$ {Fn: tolowercase ("ABCD ")}

10. FN: trimThe function will delete the "space" at the end of the source string to generate a new string.

$ {Fn: trim ("ABC ")}

 

FMT is rarely used, only a little more than <FMT: formatdate>

<FMT: formatdate>Label used to format a date

<FMT: formatdate pattern = "yyyy-mm-dd hh: mm: SS" value = "$ {post. posttime}" Var = "fmtdate"/>

 

Use of El Operators


Expression Language (Expression Language) is a simplified data access method. With El, you can easily access the hidden objects of JSP and the JavaBean Component in tag format. It is not a common programming language, but a data access language.

Reprinted from:I canCsdn blog Http://blog.csdn.net/zxq1406spys/article/details/1875623

Relational operators (6)

! = Or ne is not equal to $ {5! = 5} or ${5 ne 5} false
<Or lt less than ${3 <5} or ${3 lt 5} true
> Or GT is greater than $ {3> 5} or ${3 GT 5} false
<= Or Le is less than or equal to ${3 <= 5} or ${3 le 5} true
>=Or Ge equals to ${3 >=5} or ${3 Ge 5} false

= Or EQ equals ${5 = 5} or ${5 EQ 5} true

Logical operators (3)

& And intersection $ {A & B} or $ {a and B} true/false
| Or union $ {A | B} or $ {A or B} true/false
! Or not $ {! A} or $ {not a} true/false

Empty Operator$ {Empty Param. name}

Conditional Operators$ {? B: c} if A is true, execute B; if A is false, execute C

Finally, the author writes an eloperator. JSP Example to perform operations on all operators.

<% @ Pagecontenttype = "text/html; charset = gb2312" %>

<% @ Taglibprefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %>

<HTML>

<Head>

<Title> ch6-eloperator. jsp </title>

</Head>

<Body>

<H2> El operator </H2>

<C: Set Value = "Mike" Var = "username" Scope = "request"/>

<Table border = "1" width = "50%" align = "Left">

<Tr>

<Tr>

<TH> formula </Th>

<TH> result </Th>

</Tr>

<Tr> <TD> 14 + 3 </TD> <TD >$ {14 + 3} </TD> </tr>

<Tr> <TD> 14-3 </TD> <TD >$ {14-3} </TD> </tr>

<Tr> <TD> 14*3 </TD> <TD >$ {14*3} </TD> </tr>

<Tr> <TD> 14/3 </TD> <TD >$ {14/3} </TD> </tr>

<Tr> <TD> 14% 3 </TD> <TD >$ {14% 3} </TD> </tr>

<Tr> <TD> 14 = 3 </TD> <TD >$ {14 = 3} </TD> </tr>

<Tr> <TD> 14! = 3 </TD> <TD >$ {14! = 3} </TD> </tr>

<Tr> <TD> 14 <3 </TD> <TD >$ {14 <3} </TD> </tr>

<Tr> <TD> 14> 3 </TD> <TD >$ {14> 3} </TD> </tr>

<Tr> <TD> 14 <= 3 </TD> <TD >$ {14 <= 3} </TD> </tr>

<Tr> <TD> 14 >=3 </TD> <TD >$ {14 >=3} </TD> </tr>

<Tr> <TD> true & false </TD> <TD >$ {true & false} </TD> </tr>

<Tr> <TD> true | false </TD> <TD >$ {true | false} </TD> </tr>

<Tr> <TD>! False </TD> <TD >$ {! False} </TD> </tr>

<Tr> <TD> empty username </TD> <TD >$ {emptyusername} </TD> </tr>

<Tr> <TD> Empty Password </TD> <TD >$ {emptypassword} </TD> </tr>

</Table>

</Body>

</Html>

Reference document: http://vdisk.weibo.com/s/hloWj

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.