JSTL核心标签详解

jstl核心标签的使用

a,使用步骤:

       step1 导入标签相关的jar包。即standard.jar、 jstl.jar。如果使用的是javaee5.0及以上版本,不用导入了。

       step2 使用taglib指令导入标签。

       step3 使用标签         

       b,常用的标签

1 if

       <c:if test="" var="" scope="">test属性(必选):当值为true时,执行标签体的内容。

       var属性(可选): 指定绑订名

       scope属性(可选):指定绑订的范围

示例:

<body style="font-size:30px;">

       <%

           request.setAttribute("str","abc");

        %>

       <c:if test="${'abc' eq str}">

           abc 等于 abc

       </c:if>

       <br/>

       <c:if test="${param.age >=18}"

       var="rs" scope="request">

           <span style="color:green;">你是一个成年人</span>

       </c:if>

       <c:if test="${!rs}">

           <span style="color:red;">你还没有成年</span>

       </c:if>

    </body>

2  choose                      

       <c:choose>

       <c:when test=""> test属性(必选):当值为true时,执行标签体的内容。

       <c:otherwise>

       when必须放在choose标签里使用,when可以出现多次,otherwise只能出现一次,表示例外的情况。

示例:

<body style="font-size:30px;">

       <%

           User user = new User();

           user.setName("zs");

           user.setGendar("m");

           request.setAttribute("user",user);

        %>

        name:${user.name}

        gendar:<c:if test="${user.gendar == 'm'}">

        男

        </c:if>

        <c:if test="${user.gendar == 'f'}">

        女

        </c:if>

        <br/>

        gendar:<c:choose>

           <c:when test="${user.gendar == 'm'}"></c:when>

           <c:when test="${user.gendar == 'f'}"></c:when>

           <c:otherwise>其它</c:otherwise>

        </c:choose>

    </body>

3 forEach

       <c:forEach var="" items=""varStatus="">遍历集合

       items属性: 指定遍历的集合。var属性:指定绑订名。varStatus属性:指定绑订名

示例:

<%@page import="java.util.*,bean.*" pageEncoding="utf-8"

contentType="text/html;charset=utf-8"%>

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

<html>

   <head>

      <style>

        .row1{

           background-color:yellow;

        }

        .row2{

           background-color:red;

        }

      </style>

   </head>

   <body style="font-size:30px;">

      <%

        User user = new User();

        user.setName("user1");

        user.setGendar("m");

        List<User> users = new ArrayList<User>();

        users.add(user);

        User user2 = new User();

        user2.setName("user2");

        user2.setGendar("f");

        users.add(user2);

        User user3 = new User();

        user3.setName("user3");

        user3.setGendar("f");

        users.add(user3);

        request.setAttribute("users",users);

       %>

       <table border="1" width="60%" cellpadding="0" cellspacing="0">

       <tr><td>index</td><td>count</td><td>name</td><td>gendar</td></tr>

       <c:forEach var="user" items="${users}"

       varStatus="status">

         <tr class="row${status.index % 2 + 1}">

         <td>${status.index}</td>

         <td>${status.count}</td>

         <td>${user.name}</td>

         <td>${user.gendar}</td>

         </tr>

       </c:forEach>

       </table>

   </body>

</html>

4 out 

<c:outvalue="" default="" escapeXml="">

       value属性: 要输出的值。defalut属性:缺省值(找不到对应的值)。escapeXml属性: 当值为true时,会将">","<","&","'"替换成对应的实体。

示例:

<body style="font-size:30px;">

       &quot;out标记<br/>

           1+1:<c:out value="${1+1}"/><br/>

           name:<c:out value="${name}" default="zs"/>

           &lt;table&gt;<br/>

           <c:out value="<table>" escapeXml="false"/><br/>

</body>

5 remove

       <c:remove var="rs" scope="request">

       相当于request.removeAttribute("rs");

remove标记<br/>

<c:remove var="rs" scope="request"/>

rs:${rs}<br/>

6 set 

<c:setvar="rs" scope="request" value="${1+1}">

       相当于request.setAttribute("rs",2);

set标记<br/>

<c:set var="rs" scope="request" value="${1+1}"/>

rs:${rs}<br/>

7  catch

<c:catch>

<%@page pageEncoding="utf-8"

contentType="text/html;charset=utf-8"%>

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

<html>

    <head></head>

    <body style="font-size:30px;">

       <c:catch var="errInfo">

       <%

           int a = Integer.parseInt("123a");

           out.println(a);

        %>

        </c:catch>

        ${errInfo}

    </body>

</html>

8  url

<c:url>在url地址后面添加sessionId如果使用的是绝对路径,会自动在地址前添加应用名。

<body style="font-size:30px;">

       <c:set var="rs" scope="session" value="${1+1}"/>

       <a href="<c:url value='jsp06.jsp'/>">jsp06</a>

       <br/>

       <a href="<c:url value='/jsp06.jsp'/>">jsp06</a>

       <form action="<c:url value='/abc.do'/>"></form>

    </body>

9  import

<c:import>

<%@page pageEncoding="utf-8"

contentType="text/html;charset=utf-8"%>

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

<html>

    <head></head>

    <body style="font-size:30px;">

       jsp04...<br/>

       <c:import url="jsp05.jsp"/>

    </body>

</html>

10 redirect

<c:redirect>

<%@page pageEncoding="utf-8"

contentType="text/html;charset=utf-8"%>

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

<html>

    <head></head>

    <body style="font-size:30px;">

       <c:set var="rs" value="${1+1}" scope="session"/>

       <c:redirect url="jsp03.jsp"/>

    </body>

</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值