JSTL(JSP Standard Tag Library ,JSP 标准标签库)

JSTL核心标签库

     JSP 页面需导入<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

c:out 内容输出标签;

<body>
<%
    pageContext.setAttribute("people","张三");
%>
<h2><c:out value="${people}"></c:out></h2>
<h2><c:out value="${people2}" default="某人"></c:out></h2>
</body>
View Code

c:set 用来设置 4中属性范围值的标签;

<body>
<c:set var="people" value="张三" scope="request"></c:set>
<h2><c:out value="${people}"></c:out></h2>
<jsp:useBean id="people2" class="com.java1234.model.People" scope="page"></jsp:useBean>
<c:set property="id" target="${people2 }" value="007"></c:set>
<c:set property="name" target="${people2 }" value="王二小"></c:set>
<c:set property="age" target="${people2 }" value="16"></c:set>
<h2>编号:${people2.id }</h2>
<h2>姓名:${people2.name }</h2>
<h2>年龄:${people2.age }</h2>
</body>
View Code

c:remove 用来删除指定范围中的属性;

<body>
<c:set var="people" value="张三" scope="request"></c:set>
<h2><c:out value="${people}" default="没人啊"></c:out></h2>
<c:remove var="people" scope="request"/>
<h2><c:out value="${people}" default="没人啊"></c:out></h2>
</body>
View Code

c:catch 用来处理程序中产生的异常;

<body>
<c:catch var="errMsg">
    <%
        int a=1/0;
    %>
</c:catch>
<h2>异常信息:${errMsg }</h2>
</body>
View Code

c:if 用来条件判断;(如果加了scope="page" ,就要加 var="r" ,否则报错,底层要用到)

<body>
<jsp:useBean id="people" class="com.java1234.model.People" scope="page"></jsp:useBean>
<c:set property="id" target="${people }" value="007"></c:set>
<c:set property="name" target="${people }" value="王二小"></c:set>
<c:set property="age" target="${people }" value="16"></c:set>
<c:if test="${people.name=='王二小' }" var="r" scope="page">
    <h2>是王二小</h2>
</c:if>
<c:if test="${people.age<18 }">
    <h2>是未成年</h2>
</c:if>
</body>
View Code

c:choose、c:when、c:otherwise 用来多条件判断;

<body>
<jsp:useBean id="people" class="com.java1234.model.People" scope="page"></jsp:useBean>
<c:set property="id" target="${people }" value="007"></c:set>
<c:set property="name" target="${people }" value="王二小"></c:set>
<c:set property="age" target="${people }" value="19"></c:set>


<c:choose>
    <c:when test="${people.age<18 }">
        <h2>小于18</h2>
    </c:when>
    <c:when test="${people.age==18 }">
        <h2>等于18</h2>
    </c:when>
    <c:otherwise>
        <h2>大于18</h2>
    </c:otherwise>
</c:choose>
</body>
View Code

c:forEach 用来遍历数组或者集合;

<body>
<%
    String dogs[]={"小黑","小黄","小白","小小"};
    pageContext.setAttribute("dogs",dogs);
%>
<c:forEach var="dog" items="${dogs }">
    ${dog }
</c:forEach>
<hr/>
<c:forEach var="dog" items="${dogs }" step="2" >  <!--跳2步 输出-->
    ${dog }
</c:forEach>
<hr/>
<c:forEach var="dog" items="${dogs }" begin="1" end="2" >   <!-- 输出1,2位置,起始位是0 -->
    ${dog }
</c:forEach>
<hr/>
<%
    List<People> pList=new ArrayList<People>();
    pList.add(new People(1,"张三",10));
    pList.add(new People(2,"李四",20));
    pList.add(new People(3,"王五",30));
    pageContext.setAttribute("pList",pList);
%>
<table>
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
    </tr>
    <c:forEach var="p" items="${pList }">
        <tr>
            <td>${p.id }</td>
            <td>${p.name }</td>
            <td>${p.age }</td>
        </tr>
    </c:forEach>
</table>
</body>
View Code
varStatus是<c:forEach>jstl循环标签的一个属性,varStatus属性。就拿varStatus=“status”来说,事实上定义了一个status名的对象作为varStatus的绑定值。该绑定值也就是status封装了当前遍历的状态,比如,可以从该对象上查看是遍历到了第几个元素:${status.count}

我们常会用c标签来遍历需要的数据,为了方便使用,varStatus属性可以方便我们实现一些与行数相关的功能,如:奇数行、偶数行差异;最后一行特殊处理等等。先就varStatus属性常用参数总结下: 

${status.index}      输出行号,从0开始。${status.count}      输出行号,从1开始。${status.current}   当前这次迭代的(集合中的)项${status.first}  判断当前项是否为集合中的第一项,返回值为true或false${status.last}   判断当前项是否为集合中的最后一项,返回值为true或falsebegin、end、step分别表示:起始序号,结束序号,跳跃步伐。
如:<c:forEach begin='1' end='5' step='2' items='${list}' var='item'>表示:操作list集合汇中1~5条数据,不是逐条循环,而是按每2个取值。即操作集合中的第1、3、5条数据。

<c:forEach var="varity" items="${sessionScope.myList}" varStatus="status">
                                 <!--实现斑马线效果--> 
                                 <c:if test="${status.count%2==0}" >
                                       <tr bgcolor="lightyellow">
                                  </c:if>
                                  <c:if test="${status.count%2!=0}" >
                                       <tr>
                                  </c:if>
                                       <td>Elements</td>
                                 </tr> 
                        </c:forEach>


<c:forEach var="varity" items="${sessionScope.myList}" varStatus="status">
         ${status.first}/${status.last}
               当前:${status.current}
 </c:forEach>
c:forEach varStatus

c:fortokens 分隔输出;

<body>
<%
    String str1="www.java1234.com";
    String str2="张三,李四,王五";
    pageContext.setAttribute("str1",str1);
    pageContext.setAttribute("str2",str2);
%>
<c:forTokens items="${str1 }" delims="." var="s1">
    ${s1 }
</c:forTokens>
<hr/>
<c:forTokens items="${str2 }" delims="," var="s2">
    ${s2 }
</c:forTokens>
</body>
View Code

c:import 导入页面;

<body>
<c:import url="c_forEach.jsp"></c:import>
<c:import url="c_if.jsp"></c:import>
</body>
View Code

c:url 生成一个 url 地址;

<body>
<c:url value="http://www.java1234.com" var="url">
    <c:param name="name" value="xiaofeng"></c:param>
    <c:param name="age" value="26"></c:param>
</c:url>
<a href="${url }">Java知识分享网</a>
</body>
View Code

c:redirect 客户端跳转

<body>
<c:redirect url="target.jsp">
    <c:param name="name" value="xiaofeng"></c:param>
    <c:param name="age" value="26"></c:param>
</c:redirect>
</body>


-----------------------target.jsp-------------------------------------
</head>
<body>
<h2>姓名:${param.name }</h2>
<h2>年龄:${param.age }</h2>
</body>
View Code

 

posted @ 2017-03-03 23:22  SKYisLimit  阅读(126)  评论(0)    收藏  举报