JAVA Web base el expression and Jstl tag library

Source: Internet
Author: User
Tags arithmetic operators case statement control label logical operators variable scope java web

Tag: is the location tag index and the first content core number

I. El expression language
1.EL expression full name language, used to get the contents of its built-in object on a JSP page
syntax: ${...}
in the JSP, the page directive has a property of iselignored, which is used to mark whether the pages ignore El expressions, the default is false, that is, do not ignore
2. Usage Notes
Examples of general wording: <%= request.getattribute ("Test") >
El expression notation: ${test}//when the scope property name is unique, you can write this
${requestscope.test}//represents a value that gets the property name test in the request scope
${requsetscope["Test"]}
The wording of the. and [] can be common in most cases, but the following can only be used in the wording of []:
when there is a special character in the expression such as test_name can only use [] the wording;
when a dynamic property needs to be obtained, it can only be written by [], that is, the variable name is in the [];
[] can also be used to represent an array or index value in the collection, such as Arr's value {A,b,c}, can be obtained by means of ${arr[0]};
3.EL Variables
${test}
when the current polygon does not specify a value below which scope, it will be found by default in the following order, and if none are found, output an empty string
pagescope --requestscope--Sessionscope-Applicationscope
Auto-type conversions can be performed on the obtained values in 4.EL
If you get the values in Test1 and test2 as String values, you can do the math directly: ${test1 + test2}
5. Get Request Parameters
GET request parameters can be obtained by form or hyperlink
General wording: <%= request.getparameter ("name") >
El expression notation: ${param.name}
6.EL operator
arithmetic operators: +-*/or div% or mod example: ${5 div 3} or ${5/3}
relational operators: = = or eq (equals) = or NE (not equals) < or LT (less than) > or GT (Great then) <= or Le (less equals) >= or GE (Great equals)
logical operators: && OR and | | OR or! or not
validation Operator: empty (empty for null and "" is the judge of return true)
Example: Using the validation operator to implement when the value of the variable test is NULL, the output "test is empty", otherwise the output test value
${empty test? " Test is empty ": Test}
two. Jstl Tag Library application
The JSTL full name JSP standard tag Library, which consists of five different tag libraries, is the core tag library, the format tag library, the SQL tag library, the XML tag library, the function tag library, respectively.
use these tag libraries to define the referenced tag libraries and access prefixes at the top of the JSP page using the <%@ taglib%> directive, which is the main focus of the core tag library, because the core tag libraries are generally applied in the actual project development.
the instruction format for the core tag library is: <%@ taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "C"%> here the prefix is defined as C, which is commonly known as the Convention.
The Core tag library includes expression tags, process control labels, and loop tags.
1. Expression Tags
<c:out/> or <c:out></c:out>; output labels
format:
<c:out value= "" default= "" escapexml= "Ture/false"/>
equivalent to <%= expression%>, divided into labeled Body and no label body, the same function;
Value : A constant can be assigned directly, or an EL expression can be assigned: value= "${test}", which will output the value in quotation marks;
Default: Defaults output value, output this value when value is null, and output an empty string if no default value is defined;
EscapeXML: Controls the escape character, which is true by default, or, by default, if a tag exists in the value as a string;


<c:set/> or <c:set></c:set>; Variable Settings tab
Format
<c:set var= "" value= "" scope= "" >
var: specifies the variable name;
Value: Specify variable values, can be El, can also be written in the label body such as: <c:set var= "T" value= "Xiao Ming"/> or <c:set var= "T1" > Xiaoming </c:set>
Scope: Specify variable scope, default is page, optional value includes: page,request,response,application;


<:C remove/>; Variables Remove tags
Format:
<c:remove var= "" scope= ""/>
var: Specifies the name of the variable to remove
Scope: Specifies the valid range of variables to remove, when variable names of different ranges are duplicated, this property can be used to determine which of the removed, if not specified, is removed, the default value is page;


<c:catch></c:catch>; Catching exception tags
format:
<c:catch var= "" >
code with possible exception: ${5/0}
</c:catch>
var: Specifies the variables that store the exception information, such as not saving, can be omitted, similar to Try...catch; The above code will output infinity (infinity), which is an exception in the El expression;
2. Process Control Label
<c:if></c:if> condition judgment label
format:
<c:if var = "test=" "Scope= " ">
Label Body
</c:if>
var: Specifies the name of the variable that determines the result;
Test: Required attribute, specify conditional expression, can use El, return value is true or false, when true, tag body, tag body can be used for any JSP elements;
scope: Specifies the valid range of variables;


<c:choose></c:choose>; Condition Selection tab
commonly used in combination with <c:when></c:when> condition test labels and <c:otherwise></c:otherwise> other condition tags, similar to switch ... Case Statement
format:
<c:choose>
<c:when test= "" >
<c:out value= "" ></c:out>//or other label body
</c:when>
<c:otherwise>//Don't write .
<c:out value= "" ></c:out>
</c:otherwise>
</c:choose>
3. Cycle Labels
<c:forEach></c:forEach> Cycle Labels
used to traverse all or part of the data in a collection or array
format:
<c:foreach items = "var =" list "begin=" "end=" "step=" "varstatus=" vs ">
${list} ${vs.index} ${vs.count} ${vs.first} ${vs.last}
</c:forEach>
items: Specify the array or collection to traverse
var: sets a variable name to store values that are traversed from an array or collection
Begin , end: Specifies the start, ending position, and all of the default traversal that needs to be traversed
Step: The number of skips per traversal, the default value is 1
Varstatus: Specifying a loop state variable
Index: The indexed value in the original collection, starting from 0
Count: Current loop count, starting from 1,
First: Whether it is the current loop
Last: Whether it is the final of the current loop
note First and last, is the judgment whether in the output results of the initial or final, not the original set
Example: Output 1 to 10 all integers
<c:foreach var = "I" begin= "1" end= "ten" >
${i},
</c:forEach>


<c:forTokens></c:forTokens> Iteration Labels
Used to split the string with the specified delimiter and traverse the segmented array of strings;
Format:
<c:fortokens items= "" delims= "" var= "" ></c:forTokens>
Items: Specifying the string object to iterate
Delims: Specify delimiter, can have multiple at the same time as: delims= ",; : "
var: Specify the variable name and save the split string
Other attribute usages with the foreach label

JAVA Web base el expression and 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.