Set Label
The set tag is used to set values within a range (request, session, or application) or to set property values for an object.
The set tag is used in the following format:
(1) Use the value attribute to specify the value of a variable within a specific range in the following format:
<c:set var="varName" value="varValue" [scope="page|request|session|application"] />
(2) When using the value attribute to specify the value of a variable within a specific range, you can also include a body that acts as the out label body, that is, when the value specified by the value is null, the value specified in the body is used by default in the following format:
<c:set var="varName" value=" Value" [scope="page|request|session|application"] > default value </c:set>
(3) Set the usage format of a particular object property as follows:
<c:set property="propertyName" target="target" value="value" />
(4) When setting an attribute of a particular object, you can also have an entity, which also specifies a default value, in the following format:
<c:set property="propertyName" target="target" value="value" > default value </c:set>
The attributes in the tag are described below:
Value: This property specifies the value of a variable or an attribute in an object and can be an expression.
var: The name of the variable in which the value specified by the value property is stored.
scope: Sets the valid range of variables, if not set, defaults to page.
target: An object for setting properties, which must be a JavaBean or a java.util.Map object.
Property: Sets a property in the object.
set Label Case 1: Simple Use Case
<%@ page contentType="text/html" pageEncoding="GBK"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head><title>set Use of labels</title></head> <body> <c:set var="info" value="Study JSP/Servlet Development Technology" scope="request"/> <h3>Attribute content: ${info}</h3> </body> </html>
Run result:
Property Content: Learn JSP/servlet development techniques
out tag
The most basic tag in the core tag library is <c:out>.It can display a string or the value of an EL expression on a page.Its function is similar to that of JSP's traditional <%=expression%>.The format for using the out tag is as follows:
<c:out value="object" [escapeXml="true|false"] />
This means that when the value attribute specifies an object value of null, the value specified in the body (default value) is displayed, and the body can also be JSP code.
Properties in <c:out>
value: You can specify either a string as output or an EL expression, such as ${3+5}.
escapeXml: Type boolean, determines whether characters <, >, &,',', and so on are converted into string entity code in a structured string, defau lt ing to true.
Defau lt value: Can be a string or an expression (EL expression or JSP expression <%=expression%>).
If the expression or object value specified by the property is null, the value of this default value part is output.
out Tag Case 1: Combining escapeXml
<%@ page contentType="text/html;charset=GB2312"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>c:out Use examples</title> </head> <body> <c:out value="Using the example, the output will be direct<br>" /> <c:out value="<br>Convert to Line Break<br>" escapeXml="false" /> <c:out value="You'll use it now c:out Are you?" /> </body> </html>
Run result:
Using the example, direct output <br> Convert to Line Break Are you using c:out now?
First line, no escapeXml used, <br>direct output
Line 2: escapeXml is used, <br> is output as line break.
out tag case 2: Combining EL expressions
<%@ page contentType="text/html;charset=GBK" %> <%@ page isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <h4><c:out value="In the Core Label Library out A simple example of a label"/></h4> <c:out value="${3+7}"/><br> <c:out value="<p>Has special characters</p>" /><br> <c:out value="<p>Has special characters</p>" escapeXml="false" /> <body> </html>
Run result:
A simple example of the out tag in the core tag library 10 <p>has special characters </p> Has special characters
Program description:
<c:out value="Simple instance of out tag in core tag library"/>Direct output string value specified by value.
The EL expression is used in <c:out value="${3+7}"/> and the calculated result of the output EL expression is 10.
<c:out value="<p>has special characters </p>" />
Because the default escapeXml value is true, that is, converting <p> to character entity code, the HTML output from this code is: <p> with special characters </p>
<c:out value="<p>has special character </p>" escapeXml="false"/>
escapeXml="false" is set, so the output HTML is: <p>has special characters </p>
out tag case 3: Output by pageContext assignment
<%@ page contentType="text/html" pageEncoding="GBK"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head><title>out Label assignment</title></head> <body> <% pageContext.setAttribute("info","Suzhou City, Jiangsu Province") ; pageContext.setAttribute("ref","Kunming City, Yunnan Province") ; pageContext.setAttribute("nullout",null) ; %> <h3>Attributes exist:<c:out value="${info}"/></h3> <h3>Attribute does not exist:<c:out value="${nullout}" default="Default content!"/>~~Output does not exist</h3> <h3>Attributes exist:<c:out value="${ref}" />~~Output Content</h3> </body> </html>
The result is:
Attribute Exists: Suzhou City, Jiangsu Province Attribute does not exist: Default content!~~Output does not exist Attribute Existence: Contents of Output from Kunming City, Yunnan Province
Combination of Set and Out
Simple Use Case 1:
<%@ page contentType="text/html;charset=GB2312"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>c:set/out Use examples</title> </head> <body> Spring Framework is an open source enterprise application development framework that belongs to <c:set value="Lightweight" var="winnerName" /> <c:out value="${winnerName}" /> //Solutions </body> </html>
Run result:
The Spring Framework is an open source enterprise application development framework and a lightweight solution
Combining Scope's case:
<%@ page contentType="text/html;charset=GBK" %> <%@ page isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>JSTL: -- Set Label Instance</title> </head> <body> <h3>set Label Instance</h3> <hr> <!-- adopt set Label assignment --> <c:set var="num1" scope="request" value="${3+5}"/> <c:set var="num2" scope="session" >${3+6}</c:set> <c:set var="num3" scope="session" >10</c:set> <br> <!-- adopt out Label output variable value --> num1 The variable value is:<c:out value="${num1}" /><br> num2 The variable value is:<c:out value="${num2}" /><br> num3 The variable value is:<c:out value="${num3}" /> </body> </html>
The result is:
set label instance -------------------------------------------------------------------------------- num1 variable value is: 8 The num2 variable value is: 9 The num3 variable value is:10