Article Directory
First, the meaning of
1.1 JSTL meaning
JSP tags used : Acronym JSTL (JavaServer Pages Standard Tag Library) . JSTL custom tag library is a set of Java. It is a collection of JSP tags.
Custom tag library collection : do not write their own direct use.
Action :
(1) implemented in JSP pages code reuse .
Code re-use principle: the principle-based tag libraries, high repetition rate support reuse blocks of code and improve efficiency.
(2) writing JSP pages more readable .
Reason: looked like XML, convenient front-end view and participate in the development.
JSP code effect:
<%
String role = "";
if(request.getParameter("role") != null){
role = request.getParameter("role");
if("user".equals(role)){
%>
欢迎用户!
<%
}
if("admin".equals(role)){
%>
欢迎管理员!
<%
}
}
%>
JSTL tags results:
<c:if test="${param.role == 'user'" var="adminchok">
<c:out value="欢迎用户!"></c:out>
</c:if>
<c:if test="${param.role == 'admin'" var="adminchok">
<c:out value="欢迎管理员!"></c:out>
</c:if>
1.2 JSTL environment to build
Step 1: Determine the version of the server.
The reason: JSTL tags and Servlet and JSP page has a more stringent version of the correspondence. The incorrect version easily thrown.
Tomcat7.0 (JSTL1.1.2)
STEP 2: Download corresponding jakarta-taglibs-standard-1.1.2. Decompression.
The third step: to find \ jakarta-taglibs-standard-1.1.2 \ lib \ jstl.jar and \ jakarta-taglibs-standard-1.1.2 \ lib \ standard.jar two jar package, and copy.
Step Six: Paste the project name \ WebContent \ WEB-INF \ lib in. -BuildPath-a in the right-click ....
1.3 four categories tag
(1) Core tag
(2) formatting tags
(3) SQL labels
(4) XML tags
1.4 EL expressions
1.4.1 Meaning
EL: Expression Language.
EL expressions visiting scope of the order is from small to large:
pageScope sessionScope → → → requestScope applicationScope
1.4.2 Role
Solve the problem property "value = variable," the JSTL tag. Often used in conjunction with JSTL tags, such JSP pages more intuitive, easier wording.
1.4.3 Syntax
} {$ Expression
longhand:
<%=session.getValue("name") %>
(JSTL + EL expression) writing:
<c:out value="${sessionScope.name}" />
1.4.4. "" And "[]" operator
(1) Generally, in an EL expression, either is acceptable.
${user.sex} <=> ${user["sex"]}
(2) a set of positioning elements. use[]
${booklist[0].price}
(3) contain special characters. use[].
${user["frist_nme"]}
(4) by the dynamic variable value. use[].
${user[param]} <=> ${user.name} or ${user.sex}..........
1.4.5 EL variable
JSP built-in objects | EL name |
---|---|
page | pageScope |
request | requestScope |
session | sessionScope |
application | applicationScope |
When defining the search range:
用户名:${sessionScope.username}
It is not set range:
from small to large lookup (page → request → session → application ).
用户名:${username}
用户名:<c:out value="${username}"></c:out>
1.4.6 EL automatic conversion
Question: obtaining a digital text input box.
Common methods:
<%
String str = request.getParameter("id");
int id1 = Integer.parseInt(str);
id1 = id1 + 1;
%>
EL expression method:
<c:out value="${param.id + 1}"/>
1.4.7 EL implicit object
JSP built-in objects | EL name |
---|---|
pageContext | PageConext instance of an object. Corresponding to the processing of the current page. |
pageScope | Page scope (Map category) |
requestScope | Request scope (Map class) |
sessionScope | Session scope (Map category) |
applicationScope | Application Scope (Map category) |
param | The main value of the request parameter (Map class) |
paramValues | All request parameter values stored as a String Array (Map class) |
Header | The main value of the header request (Map class) |
headerValues | All the value of the request header (Map class) |
cookie | cookie value request (Map class) |
Initfrm | Web application initialization parameters (Map category) |
1.4.8 EL operator
category | Operators |
---|---|
Arithmetic Operators | +, -, *, / (or div),% (or mod). |
Relational Operators | ==(eq)、!=(ne)、<(lt)、>(gt)、<=(le)、>=(ge)。 |
Logical Operators | &&(and)、||(or)、!(not)。 |
Operator authentication | empty。 |
<%
String username = "张三";
request.setAttribute("username",username);
%>
<c:out value="${empty.username}"><c:out>
<!-- 结果:false。-->
<!-- 为""或null时,结果才为true。-->
<!-- 使用转义字符时,escapeXml = "false" -->
<c:out value="小于号:<。大于号:>" escapeXml="false"/>
Second, the core tags
label | meaning |
---|---|
<c:out> | For displaying data in a JSP, as <% = ...> |
<c:set> | For storing data. |
<c:remove> | For deleting data |
<c:catch> | For handling error exception conditions, and error message is stored |
<c:if> | Like if we used in the general procedures |
<c:choose> | Itself only as <c: when> and <c: otherwise> parent tag |
<c:when> | <C: choose> subtag for judging condition is satisfied |
<c:otherwise> | <C: choose> sub-tab, connected <c: when> after the label, when <c: when> tag is determined to be false when executed |
<c:import> | Retrieving an absolute or relative URL, and then expose its contents to the page |
<c:forEach> | Through the collection |
<c:forTokens> | String interception |
<c:param> | Or used to redirect page contains parameters passed |
<c:redirect> | Redirected to a new URL. |
<c:url> | Use the optional query parameters to create a URL |
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2.1 set label
2.1.1 the stored-value scope (Scope four kinds of objects) are.
Syntax 1:
<c:set var="varName" scope="scopeName">var_value</c:set>
2.1.2 stored-value to the JavaBean.
<jsp:useBean id="objectName" class="packageName.className"/>
<c:set target="${objectName}" property="objectAttribute" value="objectAattributeValue"/>
2.2 remove labels
<c:remove var="waitDeletVarName"/>
2.3 catch tag
<c:catch var="errorName"/>
<c:set target="aa" property="bb">此对象未创建,会出错!</c:set>
</c:catch>
<!-- 查看出错信息 -->
<c:out value="${errorName}"/>
2.4 if the label
<form action="firstDemo.jsp" method="post">
<input type="text" name="score" value="${param.score}"/>
<input type="submit"/>
</form>
<c:if test="${param.score>=90}" var="result" scope="page" />
<c:out value="优秀"/>
</c:if>
<c:out value="${pageScope.result}"/>
Third, formatting tags
label | meaning |
---|---|
fmt:formatNumber | Accuracy in the specified format or digital format |
fmt:parseNumber | Analytical represents a number, or a percentage of the currency string |
fmt:formatDate | Style or pattern using the specified date and time format |
fmt:parseDate | Parsing a string representing the date or time |
fmt:bundle | Binding Resources |
fmt:setLocale | Region |
fmt:setBundle | Binding Resources |
fmt:timeZone | Specify the time zone |
fmt: setTimeZone | Specify the time zone |
fmt:message | Display resource profile information |
fmt:requestEncoding | Set request character encoding |
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
Four, SQL tag
label | meaning |
---|---|
sql:setDataSource | Specify the data source |
sql:query | Run SQL queries |
sql:update | Run SQL update statement |
sql:param | The SQL statement parameter to the specified value |
sql:dateParam | The date parameter SQL statement to the specified java.util.Date object value |
sql:transaction | Provided nested in a shared database connection database behavioral elements, all the statements in the form of a transaction to run |
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
Five, XML tags
label | meaning |
---|---|
<x:out> | 与<%= … >,类似,不过只用于XPath表达式 |
<x:parse> | 解析 XML 数据 |
<x:set> | 设置XPath表达式 |
<x:if> | 判断XPath表达式,若为真,则执行本体中的内容,否则跳过本体 |
<x:forEach> | 迭代XML文档中的节点 |
<x:choose> | <x:when>和<x:otherwise>的父标签 |
<x:when> | <x:choose>的子标签,用来进行条件判断 |
<x:otherwise> | <x:choose>的子标签,当<x:when>判断为false时被执行 |
<x:transform> | 将XSL转换应用在XML文档中 |
<x:param> | 与<x:transform>共同使用,用于设置XSL样式表 |
5.1 导包
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
5.2 注意
在使用xml标签前,你必须将XML 和 XPath 的相关包拷贝至你的<Tomcat 安装目录>\lib下:
XercesImpl.jar下载地址: http://www.apache.org/dist/xerces/j/
xalan.jar下载地址: http://xml.apache.org/xalan-j/index.html
六、JSTL函数
标签 | 含义 |
---|---|
fn:contains() | 测试输入的字符串是否包含指定的子串 |
fn:containsIgnoreCase() | 测试输入的字符串是否包含指定的子串,大小写不敏感 |
fn:endsWith() | 测试输入的字符串是否以指定的后缀结尾 |
fn:escapeXml() | 跳过可以作为XML标记的字符 |
fn:indexOf() | 返回指定字符串在输入字符串中出现的位置 |
fn:join() | 将数组中的元素合成一个字符串然后输出 |
fn:length() | 返回字符串长度 |
fn:replace() | 将输入字符串中指定的位置替换为指定的字符串然后返回 |
fn:split() | 将字符串用指定的分隔符分隔然后组成一个子字符串数组并返回 |
fn:startsWith() | 测试输入字符串是否以指定的前缀开始 |
fn:substring() | 返回字符串的子集 |
fn:substringAfter() | 返回字符串在指定子串之后的子集 |
fn:substringBefore() | 返回字符串在指定子串之前的子集 |
fn:toLowerCase() | 将字符串中的字符转为小写 |
fn:toUpperCase() | 将字符串中的字符转为大写 |
fn:trim() | 移除首位的空白符 |
6.1 导包
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
6.2 示例
<c:out value="${fn:contains('ASDSFG','S')}"/>