JSTL基础

下载: 从Apache标准标记库和解压的压缩文件。

 

JSTL全名为JavaServer Pages Standard Tag Library。JSTL是由JCP(Java Community Process)所制定的标准规范,它主要提供给Java Web开发人员一个标准通用的标签函数库。

 

标签引用:

1、以classPath中,加入jar包:  standard-1.1.2.jar ,  jstl-1.1.2.jar

2、在相目\WEB-INF\tld\文件夹中放入常用的tld文件:c.tld,fmt.tld

3、在jsp文件的顶部加入以下内容:

Java代码   收藏代码
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  

 

核心JSTL标签:

引用: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

功能上分为4类:
1.表达式控制标签:out、set、remove、catch
2.流程控制标签:if、choose、when、otherwise
3.循环标签:forEach、forTokens
4.URL操作标签:import、url、redirect

demo:

<c:out value="&lt要显示的数据对象(未使用转义字符)&gt" escapeXml="true" default="默认值"></c:out>  
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<jsp:useBean id="person" class="lihui.Person"></jsp:useBean>
  <c:set value="张三" var="name1" scope="session"></c:set>
<c:if test="条件1" var="name" [scope="page|request|session|application"]></c:if>

 

语法1:迭代一集合对象之所有成员

Html代码   收藏代码
  1. <c:forEach [var="varName"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]>  
  2. 本体内容  
  3. </c:forEach>  

语法2:迭代指定的次数

Html代码   收藏代码
  1. <c:forEach [var="varName"] [varStatus="varStatusName"] begin="begin" end="end" [step="step"]>  
  2. 本体内容  
  3. </c:forEach>  

 <c:forEach> 遍历 List列表:

 对于一个基本类型的数组,当前元素将作为相应包装类(Integer、Float等等)的一个实例提供。

<c:forEach items="${domainList }" var="item">  
<tr>  
  <td align="center" valign="middle">${item["domain"]==null?"&nbsp;":item["domain"]}</td>  
  <td align="center" valign="middle"><fmt:formatDate value="${item['bind_date']}" pattern="yyyy-MM-dd HH:mm:ss"/></td>  
  <td align="center" valign="middle">  
    <c:if test="${item['domain']!=null}">  
    <a href="javascript:;" id="${item['domain']}" class="del">&nbsp;</a>  
    </c:if>  
    </td>  
</tr>    
</c:forEach> 

 <c:forEach> 遍历Map:

 对于一个java.util.Map,当前元素则作为一个java.util.Map.Entry提供。

<c:if test="${!empty permissionMap}">    
<c:forEach items="${permissionMap}" var="item">  
<tr>  
    <td>${item.value.id}</td>  
    <td>${item.value.urlOnClass}</td>  
    <td>${item.value.urlOnMethod}</td>  
</tr>  
</c:forEach>  
</c:if>    

 

<c:forTokens items="stringOfTokens" delims="delimiters" [var="varName"]  
[varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]>  
本体内容  
</c:forTokens>  

 

 

<c:import> 把其他静态或动态文件包含到 JSP 页面。与<jsp:include>的区别是后者只能包含同一个web应用中的文件,前者可以包含其他web应用中的文件,甚至是网络上的资源

<c:import url="url" [context="context"] [value="value"] [scope="..."] [charEncoding="encoding"]></c:import>

<c:redirect url="url" [context="context"]>
            <c:param name="name1" value="value1">
       </c:redirect>

 

 

格式化标签:

使用JSTL格式标签来格式化和显示文本,日期,时间和数字的,国际化的网站。以下是在您的JSP的语法,包括格式化库:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

以下是格式化JSTL标签列表:

TagDescription
<fmt:formatNumber>To render numerical value with specific precision or format.
<fmt:parseNumber>Parses the string representation of a number, currency, or percentage.
<fmt:formatDate>Formats a date and/or time using the supplied styles and pattern
<fmt:parseDate>Parses the string representation of a date and/or time
<fmt:bundle>Loads a resource bundle to be used by its tag body.
<fmt:setLocale>Stores the given locale in the locale configuration variable.
<fmt:setBundle>Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.
<fmt:timeZone>Specifies the time zone for any time formatting or parsing actions nested in its body.
<fmt:setTimeZone>Stores the given time zone in the time zone configuration variable
<fmt:message>To display an internationalized message.
<fmt:requestEncoding>Sets the request character encoding

SQL标签:

JSTL的SQL标签库标签可以交互关系型数据库(如Oracle,MySQL或Microsoft SQL Server的关系数据库管理系统)。

以下是在您的JSP语法包括JSTL SQL库:

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

以下是SQL JSTL标签的列表:

TagDescription
<sql:setDataSource>Creates a simple DataSource suitable only for prototyping
<sql:query>Executes the SQL query defined in its body or through the sql attribute.
<sql:update>Executes the SQL update defined in its body or through the sql attribute.
<sql:param>Sets a parameter in an SQL statement to the specified value.
<sql:dateParam>Sets a parameter in an SQL statement to the specified java.util.Date value.
<sql:transaction >Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.

XML 标签:

JSTL XML标记提供了一种创建和操作XML文档的JSP为中心的方式。以下是在您的JSP的语法包括JSTL XML库。

JSTL XML标记库具有自定义标签与XML数据交互。这包括XML解析,转换XML数据,流控制,基于XPath表达式。

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

在继续之前的例子中,将需要复制下面的两个XML和XPath相关的库到<Tomcat安装目录> 的\lib目录:

  1. XercesImpl.jar: 下载地址 http://www.apache.org/dist/xerces/j/

  2. xalan.jar: 下载地址 http://xml.apache.org/xalan-j/index.html

以下是XML JSTL标签列表:

TagDescription
<x:out>Like <%= ... >, but for XPath expressions.
<x:parse>Use to parse XML data specified either via an attribute or in the tag body.
<x:set >Sets a variable to the value of an XPath expression.
<x:if >Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored.
<x:forEach>To loop over nodes in an XML document.
<x:choose>Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
<x:when >Subtag of <choose> that includes its body if its expression evalutes to 'true'
<x:otherwise >Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'
<x:transform >Applies an XSL transformation on a XML document
<x:param >Use along with the transform tag to set a parameter in the XSLT stylesheet

JSTL 函数:

JSTL包括一些标准功能,其中大部分是常见的字符串操作函数。以下是在JSP的语法包函JSTL函数库:

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

以下是JSTL函数列表:

 

FunctionDescription
fn:contains()Tests if an input string contains the specified substring.
fn:containsIgnoreCase()Tests if an input string contains the specified substring in a case insensitive way.
fn:endsWith()Tests if an input string ends with the specified suffix.
fn:escapeXml()Escapes characters that could be interpreted as XML markup.
fn:indexOf()Returns the index withing a string of the first occurrence of a specified substring.
fn:join()Joins all elements of an array into a string.
fn:length()Returns the number of items in a collection, or the number of characters in a string.
fn:replace()Returns a string resulting from replacing in an input string all occurrences with a given string.
fn:split()Splits a string into an array of substrings.
fn:startsWith()Tests if an input string starts with the specified prefix.
fn:substring()Returns a subset of a string.
fn:substringAfter()Returns a subset of a string following a specific substring.
fn:substringBefore()Returns a subset of a string before a specific substring.
fn:toLowerCase()Converts all of the characters of a string to lower case.
fn:toUpperCase()Converts all of the characters of a string to upper case.
fn:trim()Removes white spaces from both ends of a string.

 

转自:http://elf8848.iteye.com/blog/245559
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值