Jstl and El expressions JSP-built-in object

Source: Internet
Author: User

Jstl: http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm;


Struts2 Tag: http://struts.apache.org/release/2.1.x/docs/tag-reference.html



Small JavaScript scripts traverse list (not converted to JSON format before)

View code

<SCRIPT type = "text/JavaScript" charset = "UTF-8"> VaR Productinfo = [ <% List <Map <string, Object> List = (list <Map <string, Object>) Request. getattribute ( "Productitem" );  If (List! = Null & List. Size ()> 0 ){  For ( Int I = 0; I <list. Size (); I ++ ){  Try  {Out. Print ( "{Code: \" "+ list. Get (I). Get (" productcode" ) + "\", Name: \ "" + list. Get (I). Get ("productname" ) + "\"}" );  If (I! = List. Size ()-1 ) {Out. Print ( "," );}}  Catch  (Exception e) {e. printstacktrace ();}  Finally  {}}} %> ]; 

Jstl + El traverse list map Array

View code

 List: <C: foreach Var = "student" items = "$ {sessionscope. List}" varstatus = "status"> Index: $ {status. Index} <Br> ID: $ {student. ID} <Br> Name: $ {student. name} <Br> Country: $ {student. Address. Country} <Br> City: $ {student. Address. City} <Br> Street: $ {student. Address. Street} <Br> </C: foreach> Map: <C: foreach Var = "entry" items = "$ {sessionscope. Map}" begin = "0" End = "4" step = "1" varstatus = "status"> Index: $ {status. Index} <Br> ID: $ {entry. Key} <Br> Name: $ {entry. value. name} <Br> Country: $ {entry. value. Address. Country} <Br> City: $ {entry. value. Address. City} <Br> Street: $ {entry. value. Address. Street} <Br> </C: foreach> Array: <C: foreach Var = "color" items = "$ {sessionscope. colors}"> $ {Color} <Br> </C: foreach>

Jstl function library

View code



FN: Contains (string, substring)

Returns true if the string parameter contains the substring parameter.

FN: containsignorecase (string, substring)

Returns true if the string parameter contains the substring (Case Insensitive ).

FN: endswith (string, suffix)

Returns true if string ends with suffix.

FN: escapexml (string)

Convert the XML (HTML) with special meaning to the corresponding XML character entity code, and return

FN: indexof (string, substring)

Returns the position where the substring parameter appears for the first time in the string parameter.

FN: Join (array, separator)

Concatenates a given array with a given delimiter separator to form a new string and return it.

FN: length (item)

Returns the number of elements contained in the parameter item. The parameter item type is array, collection, or string. For the string type, the return value is the number of characters in the string.

FN: Replace (string, before, after)

Returns a String object. Replace all places where the before parameter appears in the parameter string with the parameter after string, and return the replaced result.

FN: Split (string, separator)

Returns an array that uses the separator parameter as the delimiter to split the string parameter. Each part of the split is an element of the array.

FN: startswith (string, prefix)

Returns true if the string parameter starts with the prefix.

FN: substring (string, begin, end)

Returns the string part of the parameter, starting from the begin parameter to the end position of the parameter, including the character of the end position.

FN: substringafter (string, substring)

Returns the part of the substring that follows the string parameter.

FN: substringbefore (string, substring)

Returns the part of the substring before the string parameter.

FN: tolowercase (string)

Convert all characters of the string parameter to lowercase and return

FN: touppercase (string)

Converts all characters of the string parameter to uppercase and returns

FN: trim (string)

Remove the spaces at the beginning and end of the string parameter and return it.

// Assign a value to the traversal, equivalent to <% int example = 100 + 1; %>

<C: Set Var = "example" value = "$ {100 + 1}" Scope = "session"/>

<S: Set Name ="Personname"Value ="Person. Name"/>


// Print the variable, equivalent to <% = example %>

<C: Out value = "$ {example }"Default="A default value" Escapexml = "true"/>

<S: property value ="# Personname"Default ="A default value"/> The # symbol of ognl is equivalent to the $ symbol of El.


// Remove the variable from the session <% session. removeattribute ("example") %>

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


// Test is a Boolean operation, and the variables declared in Var record the test result. Scope indicates the existence range of the variable, which has four values. The third point of the blog post (JSP-built-in object)

<C: If test = "$ {conditional operation}" Var = "varname" Scope = "page"/>
// If the condition is trueCode
</C: If>

<S: If test ="% {False }"> <Div>Will not be executed</Div>

</S: If>

<S: elseif test ="% {True }"> <Div>Will be executed</Div>

</S: elseif>

<S: else> <Div>Will not be executed</Div>

</S: else>


// Items is the traversal set variable, and VAR declares that a single object in the set is named currentbook

<C: foreach Var = "currentbook" items = "$ {sessionscope. Titles}">
... // Display book information
</C: foreach>

<S: iterator value = "defaultdimensionitemlist" status = "St" id = "Infn">






1. cyclically traverse the set
1. introduce standard function declaration in JSP
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>

2. To determine the size of a set, the following statement must be introduced:
<% @ Taglib prefix = "FN" uri = "http://java.sun.com/jsp/jstl/functions" %>

3. How to Use jstl to determine whether the set is empty?
$ {User} is set, and user is set name
<C: If test = "$ {empty user}"> no information! </C: If> is empty.
<C: If test = "$ {! Empty user} "> others </C: If> not empty

Whether the JSON set is null:

<C: Choose>
<C: When test = "$ {'[Null]'= Coverageinfo} "> // After debugging for a long time, I wrote $ {null = coverageinfo} or $ {empty coverageinfo}
$ (Function (){
VaR coverageinfo = [];
</C: When>
<C: otherwise>
$ (Function (){
VaR coverageinfo =$ {coverageinfo };
</C: otherwise>
</C: Choose>


4. How to obtain the SET SIZE
$ {Fn: length (sessionscope. Key )}
$ {Fn: length (MAP )}

Get the json set size: $ {covarr}. length;

Get the string length: $ {fn: length ("hello ")}


5. varstatus: displays the state of the cyclic variable.
Example: <C: foreach Var = "currentfood" items = "$ {sessionscope. Foods}" varstatus = "status"
<C: If test = "$ {status % 2 = 1}">
</C: If>
</C: foreach>
Among them, the status variable records the state of the loop variable.


6. How to traverse Map sets
<C: foreach Var = "foodmap" items = "$ {sessionscope. Cart}" varstatus = "status">
<C: Set Var = "subtotal" value = "$ {foodmap. value. Bean. foodprice * foodmap. value. Quantity}"> </C: Set>
</C: foreach>
Traverse the values of the map set:
Foodmap: Save the map in the session
Foodmap. Value: Get the value of map, that is, get an object stored in Map
To obtain data in an object, you must use foodmap. value. quantity to delete the attribute of the object (quantity is the attribute of the object)



7. Traverse numbers
<C: If test = "$ {totalpages >=1}">
<A href = "javascript: void (0)" onclick = "Goback ()"> previous page </a>
<C: foreach begin = "1" End = "$ {totalpages}" Var = "I">
<C: Choose>
<C: When test = "$ {I! = Num} ">
<A href = "$ {path}/pageservlet? Page =$ {I} & branchlevel =$ {branchlevel} & branchtype =$ {branchtype} ">$ {I} </a>
</C: When>
<C: otherwise>
<A style = "background-color: Red" href = "$ {path}/pageservlet? Page =$ {I} & branchlevel =$ {branchlevel} & branchtype =$ {branchtype} ">$ {I} </a>
</C: otherwise>
</C: Choose>
</C: foreach>
<A href = "javascript: void (0)" onclick = "nextpage ()"> next page </a>
</C: If>

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.