当前位置:编程学习 > JS >>

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 valu
补充:web前端 , JavaScript ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,