jstl标签的用法(一)

    xiaoxiao2025-10-28  4

    首先下载jstl的标签库jakarta-taglibs-standard-current.zip,解压后将里面lib文件夹下的jstl.jar 和 standard.jar 两个jar包拷到自己工程WEB-INF\lib下,将标签库中的tld文件夹,拷到自己工程WEB-INF下,

    在页面中引入对应的jstl的taglib,例如如果使用core标签,则引入:

    <%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>

    然后即可使用jstl标签了。

    下面总结一下core标签的用法:

    页面引入<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>

    1.<c:out>

    作用:输出字符,用来代替<%out.println("...")%>,或者<%= %>来输出对象的值。

    例子:

    [html]  view plain  copy <c:out value="${user.id}" default="0" escapeXml="false"/>      <c:out value="${user.id}" escapeXml="false">0</c:out>      <c:out value="${user.id}" default="0" />      <c:out value="${user.id}" />  

    2.<c:set>

    作用:为变量或JavaBean中的变量属性赋值,用来代替<% request.setAttribute("", "") %>,其中的request也可以是page、session、application等。

    例子:

    [html]  view plain  copy <c:set var="name" value="this is set example" scope="request"/>      <c:set var="user2" value="${user}" scope="request"/>      <c:set target="${user}" property="name" value="hay, my name is xiaoming"/>  

    3.<c:remove>

    作用:删除存在于scope中的变量。用来代替<%request.removeAttribute(",,,")%> 等。其中的request也可以是page、session、application等内置对象。

    例子:

    [html]  view plain  copy <c:remove var="user" scope="session"/>      <c:remove var="user" scope="request"/>  

    4.<c:catch>

    作用:用于捕获JSP元素抛出的异常,当错误发生<c:catch>和</c:catch>之间时,出错代码会被忽略,但整个网页不会中止。它包含一个var属性,是一个描述异常的变量,该变量可选。若没有var属性的定义,那么仅仅捕捉异常而不做任何事情,若定义了var属性,则可以利用var所定义的异常变量进行判断转发到其他页面或提示报错信息。

     例子:

    [html]  view plain  copy <c:catch var="pring">    <%     String a = (String)request.getAttribute("zeze");     out.println(a.equals(""));    %>   </c:catch>      <if:testif:test=${pring!=null } >   occur error!   </if:test>  

    5.<c:if>

    作用:当测试表达式为true时,执行测试体代码。

    例子:

    [html]  view plain  copy <if:testif:test=${username=='user' } >   welcome, user   </if:test>   

    6.<c:choose>

    作用:用于嵌套<c:when>和<c:otherwise>动作。<c:choose>标签可以嵌套1个或多个<c:when>标签,0个或1个<c:otherwise>标签。

    例子:

    [html]  view plain  copy <c:choose>   <c:when test="${username=='xiaoming'}">   welcome, xiaoming   </c:when>   <c:when test="${username=='xiaohong'}">   welcome, xiaohong   </c:when>   <c:otherwise>   sorry, you are not xiaoming or xiaohong   </c:otherwise>   </c:choose>  

    7.<c:when>

    作用:等价于“if”语句,它包含一个test属性,表示需要判断的条件。

    例子:见 <c:choose>

     

    8.<c:otherwise>

    作用:等价于“else”语句,不包括任何属性。

    例子:见 <c:choose>

     

    9. <c:forEach>

    作用:相当与for循环,执行固定次数的body体中的代码。

    例子:

    [html]  view plain  copy <c:forEach var="item" begin="1" end="10" step="2">   ${item}    </c:forEach>      <c:forEach items="${userList}" var="user" >   ${user.username}   </c:forEach>  

    10. <c:forTokens>

    作用:可以根据某个分隔符分隔指定字符串,相当于 java.util.StringTokenizer类。

    例子:

    [html]  view plain  copy <c:forTokens items="a,b,d,agc,bcd,haha" delims="," var="item">   ${item}   </c:forTokens>  

    11. <c:import>

    作用:包含另一个JSP页面到本页面来,类似于jsp中的<jsp:include>

    例子:

    [html]  view plain  copy <c:import url="http://www.163.com" >      <c:import url="http://www.163.com" >     <c:param name="test" value="1234" />   </c:import>   

    12. <c:url>

    作用:生成一个url

    例子:

    [html]  view plain  copy <c:url value="http://www.163.com" >   <c:param name="name" value="xiaoming"/>     </c:url>      <a href="<c:url value="http://www.163.com" ><c:param name="name" value="xiaoming"/></c:url>">网易</a>  

    13. <c:redirect>

    作用:重定向

    例子:

    [html]  view plain  copy <c:redirect url=http://www.163.com>       <c:param name="param" value="value"/>    </c:redirect>   

    14. <c:param>

    作用:用于传递参数

    例子:见<c:import>,<c:url>,<c:param>

    转载请注明原文地址: https://ju.6miu.com/read-1303599.html
    最新回复(0)