JSTL標籤

來源
jsp中有大量<% %>java片段
jsp中html標籤+jsp標籤+java片段
提出
把<% %>java片段用標籤替換



C標籤
<c:out>標籤
最常用的標籤,用於在 JSP 中顯示數據
<c:out value="hello world" ></c:out>
<h1> 如何輸出request/session/application/pageContext域對象的內容 </h1>
<!--獲取“abc”中的值,如果獲取不到,默認值就是“hello”,escapeXml=false使得“abc”取到的文字中的html標籤可用
false【html形式】,true【文本形式】-->
<c:out value=" ${abc}" default ="hello" escapeXml="false" ></c:out>
<%
             //如果域對象中有相同的屬性名,c:out的優先級是
             //pageContext > request > session > application
            request.setAttribute( "abc","你好1<a href='http://www.baidu.com'>baidu</a>");
            session.setAttribute( "abc","你好2" );
            application.setAttribute( "abc","你好3" );
            pageContext.setAttribute( "abc","你好4" );
%>
獲取一個對象的元素
<!-- 如下兩種方式等價 -->
<c:out value=" ${xm.name}"></ c:out>
${xm.age}
${xm.age}  等價於 ((User)request.getAttribute("xm")).getAge()
<%            
            User u = new User();
            u.setName( "小明");
            u.setAge(22);
            request.setAttribute( "xm",u);
%>
 
<c:set>標籤
<!-- 等價於request.setAttribute("abc","中國,北京") -->
<c:set var="abc" value="中國,北京" scope ="request"></ c:set>
 
<c:remove>標籤
<!-- 等價於request.setAttribute(" abc","中國,北京") -->
<c:set var="a" value="中國,北京" scope ="request"></ c:set>
<c:out value=" ${a}"></ c:out>
<c:remove var="a" scope="request"></ c:remove>
<c:out value=" ${a}" default ="沒有了"></ c:out>
 
<c:catch>標籤
<c:catch var="myexception" >
<%int a = 8/0; %>
</c:catch>
<!-- 打印出捕獲的異常 -->
<c:out value=" ${myexception.message}"></ c:out>
 
<c:if>標籤
             <!-- 等價於request.setAttribute(a,"hello") -->
             <c:set var="a" value="hello" scope="request" ></c:set>
             <!-- 等價於request.setAttribute(age,23) -->
             <c:set var="age" value="23" scope="request" ></c:set>   

             <!-- 如果a == 'hello'就輸出ACK! 否則輸出NCK! -->
            <c:if test=" ${a == 'hello'}"> ACK!</c:if >
             <c:if test=" ${a != 'hello'}"> NCK!</c:if >
            
             <!--數值判斷  -->
             <c:if test=" ${age == 23}"> 23歲</c:if >
             <c:if test=" ${age >=18}"> 已經成年</c:if >
            
             <!--獲取一個對象的屬性  -->
            <c:if test=" ${u.name == 'micky'}">
                   <c:out value=" ${u.age}"></ c:out>
             </c:if>
             <%
                  User user = new User();
                  user.setName( "micky");
                  user.setAge(1);
                  request.setAttribute( "u",user);
             %>    
 
<c:choose><c:when><c:otherwise>標籤
            <c:choose>
                  <c:when test=" ${rat.age < 2}">
                         <font color="red" >娃娃</ font>
                   </c:when>
                   <c:when test=" ${rat.age >=2 and rat.age < 5} ">
                         <font color="blue" >壯年</ font>
                   </c:when>
                   <c:otherwise>
                         <font color="green" >老年</ font>
                   </c:otherwise>
             </c:choose>
             <%
                  User u = new User();
                  u.setName( "rat");
                  u.setAge(10);
                  request.setAttribute( "rat",u);
             %>
 
<c:foreach>標籤
            <!--取出array,並把沒次取出來的元素放在變量a  -->
             <c:forEach items=" ${array}" var ="a">
                   <c:out value=" ${a.name}"></ c:out>
                   <c:out value=" ${a.age}"></ c:out>
             </c:forEach>
            
             <!-- 從1-10依次打印 -->
             <c:forEach begin="1" end="10" var="i" >
                   <c:out value=" [${i}] "></c:out >
             </c:forEach>

             <!-- 從1-10隔3打印 -->
             <c:forEach begin="1" end="10" step="3" var="i">
                   <c:out value=" [${i}] "></c:out >
             </c:forEach>
             <%
                  ArrayList<User> al = new ArrayList<User>();
                  User u1 = new User();
                  u1.setName( "老大");
                  u1.setAge(30);
                  
                  User u2 = new User();
                  u2.setName( "老二");
                  u2.setAge(25);
                  
                  User u3 = new User();
                  u3.setName( "老三");
                  u3.setAge(20);
                  
                  al.add(u1);
                  al.add(u2);
                  al.add(u3);
                  
                  request.setAttribute( "array",al);
             %>
 
<c:forToken>標籤
<!-- 從items中從0-7,隔2取出數據放入tmp,分隔符是‘,’ -->
<c:forTokens items="a,b,c,d,e,f,g" begin="0" end="7" step="2" delims="," var="tmp" >
    ${ tmp}
</c:forTokens>
發佈了78 篇原創文章 · 獲贊 100 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章