脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-JSTL标签,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。
来源
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='https://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>
觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的javascript代码实例教程-JSTL标签全部内容,希望文章能够帮你解决javascript代码实例教程-JSTL标签所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。