JSP provides the custom tag function, extends JSP's tag processing capabilities, and facilitates developers to develop. However, different developers may define different tags for the same function, which makes custom tags arbitrary. Many functions are shared by developers in Web development. Therefore, defining a set of standard tag libraries for developers can undoubtedly improve development efficiency. This is the origin of jstl (JavaServer Pages standard tag library, jstl. This article mainly introduces jstl.
The introduction to jstl will not be repeated here. If you are interested, you can use Baidu or Google.
To use jstl, you must. jar and standard. put the JAR file in classpath. If you still need to use the XML processing and database access (SQL) Labels, you also need to put the relevant jar files in classpath, these jar files are all stored in the downloaded ZIP file. This zip file can be downloaded from http://atat.apache.org/taglibs/standard.
The jstl tag library is divided into five categories: jstl core tag library, database tag library, i18n formatting tag library, XML tag library, and jstl function tag library.
Jstl core tag Library
The core tag library mainly includes general-purpose tags, condition tags, iteration tags, and URL-related tags. To use the core tag library on the JSP page, you must use the <% @ taglib %> command to specify the core tag. The Code is as follows:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<C: Set> label
Jstl provides labels for defining variables <C: Set>, which can store variables in the JSP range or the JavaBean attribute. The syntax format is divided into the following five types:
1) Specify the variable name and variable value, and do not specify Scope)
<c:set var="username" value="value"></c:set>
2) store the value in the scope variable.
<c:set var="username" value="value" scope="page|request|session|application"></c:set>
3) store the data in the body content to the scope variable.
<C: Set Var = "username" Scope = "Page | request | session | application"> body content </C: Set>
4) store the value in the properties of the target object.
<c:set value="value" target="target" property="propertyName"></c:set>
5) store the data in the body content to the properties of the target object.
<C: set target = "target" property = "propertyname"> body content </C: Set>
<C: Out> label
<C: Out> is used to display data, similar to <% = %>, but more powerful. Using it to output content is simpler than using scriptlets code, facilitating page maintenance. Its Syntax format is divided into the following four types.
1) specify the value to be displayed by specifying the Value Attribute in <C: Out>.
<c:out value="value"/>
2) use the escapexml attribute to set whether to output the value in the value as is. <C: Out> converts <,>, ', ", & to & lt;, & gt;, & #039;, & #034; and & amp ;. If you do not want to convert, set the escapexml attribute to "false ".
<c:out value="value" escapeXml="true|false" />
3) use the default attribute to specify the default value. If the value is null, the default value is displayed.
<c:out value="value" default="no data" />
4) Specify the default value through the body content. If the value is null, the body content is displayed.
<C: Out value = "value"> body content </C: Out>
<C: Remove> label
<C: Remove> A tag is used for a variable within a specified range. The syntax format is as follows:
<c:remove var="number" scope="session" />
The VaR attribute specifies the name of the variable to be deleted. The SCOP attribute is optional and is used to specify the variable range. The default value is page.
<C: If> label
<C: If> labels are used to execute process control. They have the same functions as if in Java. It has two syntax formats: No ontology content and no ontology content.
1) The <C: If> label format without the body content is as follows:
<c:if test="testCondition" var="varName" [scope="{page|request|session|application}"] />
The test attribute must be filled in to indicate the expression used for judgment. The property var defines a variable that stores the result after judgment. Scope indicates the storage range of variables defined by var.
2) The <C: If> label format with ontology content is as follows:
<C: If test = "testcondition" Var = "varname" [scope = "{page | request | session | application}"]> body content </C: If>
When the expression result in test is true, the body content is executed. Otherwise, the body content is not executed.
<C: Choose>, <C: When>, and <C: otherwise> labels
<C: If> A tag can only identify one expression and cannot specify statements that are not executed properly. Jstl also provides the statement <C: Choose> label similar to if... else.
The <C: Choose> label can only be the parent label of the <C: When> and <C: otherwise> labels. One or more <C: When> labels can contain 0 or more <C: otherwise> labels. The syntax format is as follows:
<c:choose> .... <c:when test="testCondition"></c:when> .... <c:otherwise></c:otherwise></c:choose>
<C: foreach> label
<C: foreach> A tag has two syntax formats: one is used to traverse the members of a collection object, and the other is used to make the statement run cyclically for a specified number of times.
1) <C: foreach> traversal combines object members. The syntax format is as follows:
<C: foreach Var = "varname" items = "Collection" varstatus = "varstatusname" begin = "begin" End = "end" step = "Step"> body content </C: foreach>
VaR is used to store the specified members. Items refers to the set object to be iterated. Varstatus is used to store information about the specified members. Begin indicates the start position (0 by default), end indicates the end position, and step indicates the step size (1 by default ).
2) <C: foreach> specifies the number of statement executions. The syntax format is as follows.
<C: foreach Var = "varname" varstatus = "varstatusname" begin = "begin" End = "end" step = "Step"> body content </C: foreach>
<C: fortokens> label
<C: fortokens> tags are used to split strings based on specified delimiters. The syntax format is as follows.
<C: fortokens Var = "varname" items = "stringoftokens" delims = "delimiters" varstatus = "varstatusname" begin = "begin" End = "end" step = "Step"> body content </C: fortokens>
The VaR, varstatus, begin, end, and step attributes have the same usage as those in the <C: foreach> label. The items attribute indicates the split string, and the delims attribute specifies the delimiter.
<C: Import> label
<C: Import> A tag can include other static or dynamic files in its own JSP webpage. The biggest difference between it and JSP action commands <JSP: Include> is that <JSP: Include> can only contain files in the same web application as itself, and <C: import> it can also contain files of different Web applications or other websites. The syntax format is as follows:
<C: import url = "url" context = "context" Var = "varname" Scope = "Page | request | session | application" charencoding = "charencoding"> body content </C: import>
Alternatively, the resource content is imported as a reader object. The format is as follows:
<C: Import url = "url" context = "context" varreader = "varreadername" charencoding = "charencoding"> body content </C: Import>
<C: param> label
<C: param> A tag is used to pass a parameter. It contains two attributes: Name and value. The syntax format is as follows:
<C: Param name = "parameter name" value = "parameter value"/>
<C: URL> tag
<C: URL> the tag uses the correct URL rewriting rule to construct a URL. The syntax format is as follows:
<c:url value="expression" context="expression" var="name" scope="scope"> <c:param name="expression" value="expression"/> ...</c:url>
The value attribute is used to specify the basic URL and mark the conversion as necessary. If this basic URL starts with a slash, the servlet context name will be added before it. You can use the context attribute to provide an explicit context name. If this attribute is omitted, the name of the current servlet context is used. This is especially useful because the servlet context name is determined during deployment rather than during development. (If this basic URL does not start with a slash, it is considered as a relative URL, and the context name does not need to be added .)
If any request parameter is specified through the nested <C: param> flag, their names and values are added to the end of the generated URL using the standard notation of the http get request. URL encoding: To generate a valid URL, any characters in the names or values of these parameters are converted as appropriate.
<C: Redirect> label
<C: Redirect> the tag redirects client requests to another resource. The syntax format is as follows:
<c:redirect url="expression" context="expression"> <c:param name="expression" value="expression"/> ...</c:redirect>
<C: Redirect> it is used to send an HTTP redirect response to a user's browser. It is equivalent to the sendredirect () method function of javax. servlet. http. httpservletresponse in jstl.
Reprinted please indicate the source: http://blog.csdn.net/iAm333