myeclipse中导入使用JSTL

    技术2022-05-11  81

    一.导入JSTL

    方法一:

    右键你的项目,选择myeclipse中的add jstl library选项 

    方法二:

    新建一个工程,在JSTL Support那一栏中的Add JSTL libraries to WEB_INF/lib folder打勾,点完成即可。

     二.使用JSTL

    一、JSTL 简介JSP标准标记库(JSP Standard Tag Library,JSTL)是一个实现Web应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断、数据管理格式化、XML操作以及数据库访问。JSTL 1.0 有四个定制标记库:core、format、xml 和 sql.1.core 标记库提供了定制操作,通过限制了作用域的变量管理数据,以及执行页面内容的迭代和条件操作。2.format 标记库定义了用来格式化数据(尤其是数字和日期)的操作。它还支持使用本地化资源束进行 JSP 页面的国际化。3.xml 库包含一些标记,这些标记用来操作通过 XML 表示的数据。4.sql 库定义了用来查询关系数据库的操作。

    二、表达式语言(Expression Language)1.隐含对象(常用) 1)requestScope:与请求作用域属性的名称和值相关联的 Map 类2)sessionScope:与会话作用域属性的名称和值相关联的 Map 类   3)applicationScope:与应用程序作用域属性名称和值相关联的 Map 类4)pageScope:与页面作用域属性的名称和值相关联的 Map 类5)param:按名称存储请求参数的主要值的 Map 类6)paramValues:请求参数的所有值作为 String 数组存储的 Map 类2.EL 表达式的格式:用美元符号($)定界,内容包括在花括号({})中,eg:${value}3.EL的存取器:使用点运算符(.)和方括号运算符([]).1)点运算符通常用于访问对象的特性。例如,在表达式 ${user.firstName} 中,使用点运算符来访问 user 标识符所引用对象的名为 firstName 的特性。2)方括号运算符用来检索数组和集合的元素。在数组和有序集合的情况下,把要检索的元素的下标放在方括号中。对于实现 java.util.Map 接口的集合,方括号运算符使用关联的键查找存储在映射中的值。在方括号中指定键,并将相应的值作为表达式的值返回。3)点运算符和方括号运算符可能实现某种程度的互换4.EL运算符1)算术运算符: +、-、*、/(或 div)和 %(或 mod) 2)关系运算符: ==(或 eq)、!=(或 ne)、<(或 lt)、>(或 gt)、<=(或 le)和 >=(或 ge) 3)逻辑运算符: &&(或 and)、||(或 or)和 !(或 not) 4)验证运算符: empty 三、常用的JSTL core tag1.set定义变量标记:定义限制了作用域的变量Syntax: 1):<c:set value=”value” var=”varName” [scope=”{page|request|session|application}”]/>2):<c:set var=”varName” [scope=”{page|request|session|application}”]> body content  </c:set>2.out打印标记:显示表达式的值,取代jsp scriptlet中的<%=variableName%>Syntax: 1):<c:out value=”${variableNam}” [escapeXml=”{true|false}”]> default value </c:out>2):<c:out value=”value” escapeXml=”{true|false}”] [default=”defaultValue”] />3.forEach循环标记(for/while):提供指定的循环次数(适用syntax 2)或在某个数据结构(Collection/Array)(适用syntax 1)上进行循环的能力Syntax:1):<c:forEach [var=”varName”]  items=”collection”  [varStatus=”varStatusName”]   [begin=”begin”]  [end=”end”]  [step=”step”] > body content </c:forEach>2):<c:forEach  [var=”varName”]  [varStatus=”varStatusName”] begin=”begin”  end=”end”  [step=”step”]> body content</c:forEach>4.forTokens循环标记:允许对一个String值进行循环,使用所选择的字符作为记号(token)之间的定界符Syntax:<c:forTokens items="stringOfTokens" delims="delimiters" [var="varName"] [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]> body content </c:forTokens >5.if条件求值标记(if):只在指定的测试条件为真时求具体的值,没有”else”条件—要么执行要么忽略执行体Syntax:1):<c:if test=”testCondition” var=”varName” [scope=”{page|request|session|application}”]/>2):<c:if test=”testCondition” [var=”varName”] [scope=”{page|request|session|application}”]>body content</c:if>6.choose条件求值标记(switch-case/if-else):使用嵌套when标记(类似case)和otherwise标记(可选,类似default)Syntax:1):<c:choose> body content (<when> and <otherwise> subtags)</c:choose>2):<c:when test=”testCondition”>body content</c:when>3):<c:otherwise> conditional block</c:otherwise>7.remove 删除变量标记:用来删除限制了作用域的变量的Syntax: <c:remove var=”varName” [scope=”{page|request|session|application}”]/>四、开始使用JSTL1.必需的文件:1)jar档:jstl.jar,standard.jar.一般是放置在lib目录下2)tld文件:c.tld,一般会是在WEB-INF目录下,依tld文件的位置修改web.xml文档3)一个web.xml的例子:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app>  <taglib>    <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>    <taglib-location>/WEB-INF/c.tld</taglib-location>  </taglib>    </web-app>2.在JSP页面中声明标记库:<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>五、参考资料JSTL规范:http://java.sun.com/products/jsp/jstl/index.jsp

    ibm:    http://www.ibm.com/developerworks/cn/java/j-jstl0211/JSTL 1.0相关下载:http://www.apache.org/dist/jakarta/taglibs/standard-1.0/JSTL1.0规范书:http://www.jcp.org/aboutJava/communityprocess/first/jsr052/index.html表达式语言:http://www-900.ibm.com/developerWorks/cn/java/j-jstl0211/六、综合使用Core Tag的例子:<%@ page contentType="text/html; charset=GBK" %><%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %><html><head><title>UseJSTLCoreTag</title></head><body bgcolor="#ffffff"><h1>JSTL Core Tag</h1>taglib declare<br /><%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %><table border="1">  <tr>    <td>Tag    </td>    <td>      Example    </td>    <td>      Result    </td>  </tr>  <tr>    <td>      set    </td>    <td>      <c:set value="sessionVariableValue" scope="session" var="sessionVariable"/>    </td>    <td>      <c:set value="sessionVariableValue" scope="session" var="sessionVariable"/>     </td>  </tr>  <tr>    <td>out    </td>    <td>      <c:out value="${sessionScope.sessionVariable}" default="none" escapeXml="false"/>   <br/>      <br/>      <c:out value="${sessionScope['sessionVariable']}" default="none" escapeXml="false"/>    </td>    <td>      <c:out value="${sessionScope.sessionVariable}" default="none" escapeXml="false"/>  <br />      <br />      <c:out value="${sessionScope['sessionVariable']}" default="none" escapeXml="false"/>    </td>  </tr>  <tr>    <td>      remove    </td>    <td>      <c:remove var="sessionVariableValue" scope="session"/>    </td>    <td>      <c:remove var="sessionVariable" scope="session"/>    </td>  </tr>  <tr>    <td colspan="2">      after remove,<c:out value="${sessionScope.sessionVariable}" default="none" escapeXml="false"/>    </td>     <td>      <c:out value="${sessionScope.sessionVariable}" default="none" escapeXml="false"/>    </td>  </tr>  <tr>    <td>      forEach    </td>    <td>      <table>        <tr>      <c:forEach begin="1" end="10" step="1" var="loop">        <td>        <c:out value="${loop}"/>        </td>      </c:forEach>        </tr>      </table>    </td>    <td>      <table>        <tr>      <c:forEach begin="1" end="10" step="1" var="loop">        <td>        <c:out value="${loop}"/>        </td>      </c:forEach>        </tr>      </table>    </td>  </tr>  <tr>    <td>      forTokens    </td>    <td>      <c:forTokens var="token" delims="#,;4" items="0#1,2,34,5,6,7;8,9,A,B,C#,;4D" begin="3" end="10" step="2">        <c:out value="${token}">        </c:out>      </c:forTokens>    </td>    <td>      <c:forTokens var="token" delims="#,;4" items="0#1,2,34,5,6#,7;8,9,A,B,C#,;4D" begin="3" end="10" step="2">        <c:out value="${token}">        </c:out>      </c:forTokens>    </td>  </tr>  <tr>    <td>      if    </td>    <td>      <c:if test="${sessionScope.sessionVariable=='none'}">        There is not sessionVariable      </c:if>      <br />      <c:if test="${sessionScope.sessionVariable!='none'}">        The value of sessionVariable is not none      </c:if>    </td>    <td>      <c:if test="${sessionScope.sessionVariable=='none'}">        There is not sessionVariable      </c:if>      <c:if test="${sessionScope.sessionVariable!='none'}">        The value of sessionVariable is not none      </c:if>    </td>  </tr>  <tr>    <td>      choose    </td>    <td>      <c:choose><br/>        <c:when test="${empty sessionScope.sessionVariable}"><br/>          There is not sessionVariable        </c:when><br/>        <c:otherwise><br/>          The value of sessionVariable is not none        </c:otherwise><br/>      </c:choose><br/>    </td>    <td>      <c:choose>        <c:when test="${empty sessionScope.sessionVariable}">          There is not sessionVariable        </c:when>        <c:otherwise>          The value of sessionVariable is not none        </c:otherwise>      </c:choose>    </td>  </tr></table><form  method="post">  <table>    <c:forEach var="loop" begin="1" end="10" step="1">      <tr>        <td>          <input type="checkbox" name="checkBox" value="<c:out value='${loop}' />"/><c:out value='${loop}' />        </td>      </tr>    </c:forEach>  </table>  <input type="hidden" name="hiddenVar" value="hiddenValue"/>  <input type="submit" value="submit"/></form><c:out value="param.hiddenVar" default="none"/><c:out value="${param.hiddenVar}" default="none"/><c:if test="${not empty param.hiddenVar}"><p>  after click submit button,display following:</p>you select:<c:forEach var="checkboxValues" items="${paramValues.checkBox}" varStatus="status">  <c:out value="${checkboxValues}"/>  <c:if test="${!status.last}">,  </c:if></c:forEach></c:if><P>请通过参看该页源文件的下列部分以体验out标记的escapeXml属性的作用。<br/><c:set var="tempVar" scope="page">  <pre><&></pre> </c:set>set escapeXml=false,output:<c:out value="${tempVar}" escapeXml="false"/>set escapeXml=true,output:<c:out value="${tempVar}" escapeXml="true"/></p></body></html>


    最新回复(0)