JSTL核心標籤示例
阿新 • • 發佈:2019-02-14
<%@ page contentType="text/html;charset=GB2312" %> <jsp:directive.page import="java.util.*"/> <%@ taglib prefix="c" uri="> <html> <head> <title>JSTL練習</title> </head> <body> <h1><font color="red">cf OUT練習</font></h1> <br> <!-- JSTL-OUT 練習事例1 常用輸出--> <c:out value="JSTL 常用輸出"></c:out> <br> <!-- JSTL-OUT 練習事例2 在變數無值的情況下out預設輸出練習--> <c:out value="${username}" default="在變數無值的情況下out預設輸出練習"></c:out> <br> <!-- JSTL-OUT 練習事例3 在變數在session,request值的情況下out預設輸出練習--> <c:out value="${sessionScope.username}" default="在變數sessionScope.username值的情況下out預設輸出練習"></c:out> <br> <!-- JSTL-OUT 練習事例4 在變數在session,request值的情況下out預設輸出練習--> <% session.setAttribute("username1","username1的值"); %> <c:out value="${sessionScope.username1}">在變數sessionScope.username1值的情況下out預設輸出練習 </c:out> <br> <!-- JSTL-OUT 練習事例5 在變數在session,request值的情況下out預設輸出練習含過慮(預設直接輸出escapeXml="true")--> <% session.setAttribute("username1","<h2>username1的值</h2>"); %> <c:out value="${sessionScope.username1}" escapeXml="false">在變數sessionScope.username1值的情況下out預設輸出練習 </c:out> <br> <h1><font color="red">SET練習</font></h1> <!-- JSTL-SET 練習事例1 定義變數setusername 給值為:setusername的值--> <c:set var="setusername" value="setusername的值"/> <!-- 輸出變數setusername --> <c:out value="${setusername}"/> <h1><font color="red">remove練習</font></h1> <!-- remove 是移出從page,request,seesion,application變數的值 --> <!-- 定義了一個變數在session範圍內 --> <c:set var="sessionvar" value="sessionvervalue" scope="session" /> <!--輸出定義的變數的值--> <c:out value="${sessionScope.sessionvar}">remove前</c:out> <!--移出sessionvar變數中的值 --> <c:remove var="sessionvar" scope="session" /> <!--再輸出定義的變數的值--> <c:out value="${sessionScope.sessionvar}">remove後</c:out> <br> <h1><font color="red">catch練習</font></h1> <c:catch var="xt"> <% String numvervalue="str"; //String numvervalue="110"; int i=Integer.parseInt(numvervalue); %> </c:catch> 異常資訊:${xt} <h1><font color="red">IF練習</font></h1> <c:set var="usernameif" value="system" scope="session"/> <c:if test="${sessionScope.usernameif=='system'}" var="isok"/> 判斷結果:<c:out value="${isok}"></c:out> <br> <c:if test="${sessionScope.usernameif=='system'}" var="isok"> 條件成立輸出:歡迎你啊。system(條件不成立不顯示內容) </c:if> <br> <h1><font color="red">choose練習</font></h1> <c:choose> <c:when test="${pram.age>70}">大於70歲的使用者 </c:when> <c:when test="${pram.age>=35 and pram.age <=70}">35-70之間 </c:when> <c:when test="${pram.age<35 and pram.age>0}">0-35</c:when> <c:otherwise> 條件外的 </c:otherwise> </c:choose> <h1><font color="red">forEach練習</font></h1> <h3>陣列: String names[]=new String[4];<br> names[0]="江濤";<br> names[1]="劉德華";<br> names[2]="梁朝偉";<br> names[3]="小剛";<br> pageContext.setAttribute("mlboys",names); <br> pageContext.setAttribute("mlboys1",names); <br> </h3> <% String names[]=new String[4]; names[0]="江濤"; names[1]="劉德華"; names[2]="梁朝偉"; names[3]="小剛"; pageContext.setAttribute("mlboys",names); pageContext.setAttribute("mlboys1",names); %> <h3> 最基礎的陣列輸出:</h3> <c:forEach items="${mlboys}" var="mlboys"> ${mlboys}<br> </c:forEach> <br> <h3> 有引數說明的陣列輸出:</h3> <c:forEach items="${mlboys1}" var="mlboys" begin="1" end="2" step="1" varStatus="i"> 值:${mlboys} 索引號:${i.index} ,總共輸出有:${i.count},是否是第一個值:${i.first },是否是第最後一個值:${i.last}<br> </c:forEach> <br> <h3>List</h3> <% ArrayList<String> mlboylist=new ArrayList<String>(); mlboylist.add("江濤"); mlboylist.add("劉德華"); mlboylist.add("梁朝偉"); mlboylist.add("小剛"); pageContext.setAttribute("mlboylist",mlboylist); %> <h3> 最基礎的集合list輸出:</h3> <c:forEach items="${mlboylist}" var="mlboylist"> ${mlboylist}<br> </c:forEach> <br> <h3>HashMap</h3> <% HashMap hmap=new HashMap(); hmap.put("no1","江濤"); hmap.put("no2","劉德華"); hmap.put("no3","梁朝偉"); hmap.put("no4","小剛"); pageContext.setAttribute("hashmaps",hmap); %> <h3> 最基礎的集合HashMap輸出:</h3> <c:forEach items="${hashmaps}" var="hashmaps"> ${hashmaps.key},${hashmaps.value}<br> </c:forEach> <br> <h3> 最基礎的集合froToke輸出:</h3> <c:set var="namekk" value="a:b:c:e:f"></c:set> <c:forTokens items="${namekk}" var="names2" delims=":"> ${names2}<br> </c:forTokens> <br> <h1><font color="red">import練習</font></h1> <h4>本地</h4> <c:import url="urlimport.jsp"></c:import> <h4>外網url="http://www.163.com"</h4> <c:import url="#"></c:import> <h4>傳參可以是本地網頁,也可以是其它</h4> <c:url value="urlimport.jsp" var="website"> <c:param name="p" value="hello"></c:param> <c:param name="w" value="wello"></c:param> </c:url> ${website} <br> <a href="${website}" >另外顯示</a> <h1><font color="red">redirect練習</font></h1> <h4>轉到urlimport.jsp,將url="urlimport.jsp"</h4> <!-- <c:redirect url="urlimport.jsp"></c:redirect> --> <br> <h4>轉到urlimport.jsp並傳參</h4> <!-- <c:redirect url="urlimport.jsp"> <c:param name="p" value="xxx"></c:param> <c:param name="p2" value="2xxx"></c:param> </c:redirect> --> </body> </html>