JSTL表达式和EL表达式区别

来源:互联网 发布:电脑装哪些软件 编辑:程序博客网 时间:2024/05/19 15:42

JSTL与EL表达式

jstl : http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm;

       介绍:http://docs.oracle.com/javaee/6/tutorial/doc/(http://docs.oracle.com/javaee/6/tutorial/doc/bnacj.html#bnaco)

       api:http://tomcat.apache.org/taglibs/standard/apidocs/

struts2 tag: http://struts.apache.org/release/2.1.x/docs/tag-reference.html 

javascript中小脚本遍历list(之前没有转换成json格式)

 View Code

 jstl+EL遍历list map array

View Code

 jstl函数库

View Code

           //给遍历赋值,相当于 <% int example = 100+1; %>

<c:set var= "example" value="${100+1}" scope="session"  />

<s:set name="personName" value="person.name"/> 

          //打印变量,相当于 <%=example%>

<c:out value="${example}" default="a default value"  escapeXml="true"/>

<s:property value="#personName" default="a default value" />  OGNL的#符号相当与EL的$符号 

         //从session中移除变量 <% session.removeAttribute("example")%>

<c:remove var= "example" scope="session"/> 

         //test里面是一个boolean的运算, var里面声明的变量则记录test的结果,scope指明这个变量的存在范围,有4个值,博文的第三点(JSP- 内置对象)

<c:if test= "${ 条件运算 }"  var= "varName" scope= "page"  />
        //条件为true时,执行的代码
</c:if>

<s:if test="%{false}"><div>Will Not Be Executed</div>

</s:if>

<s:elseif test="%{true}"><div>Will Be Executed</div>

</s:elseif>

<s:else> <div>Will Not Be Executed</div>

</s:else> 

        //items中是遍历的集合变量,var里面声明集合中的单个对象名称为currentBook

<c:forEach var="currentBook" items="${sessionScope.titles}">
                …//显示书籍信息 
</c:forEach>

<s:iterator value="defaultDimensionItemList" status="st" id="infn">

</s> 

一、循环遍历集合
1、在jsp中引入标准函数声明
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 


2、若要判断集合的大小,则需要引入如下声明
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>


3、如何使用jstl判断集合是否为空
${user}为集合,user为集合名
<c:if test="${empty user}">无信息!</c:if>为空
<c:if test="${!empty user}">其它</c:if>非空

json集合判断是否为空:

 <c:choose>
    <c:when test="${'[null]' == coverageinfo}">//调试了好久,之前就写的${ null == coverageinfo} or ${empty coverageinfo}
       $(function(){
           var coverageinfo= [];
       });
    </c:when>
    <c:otherwise>
        $(function(){
          var coverageinfo= ${coverageinfo};
        });
    </c:otherwise>
    </c:choose>

4、如何取得集合的大小
${fn:length(sessionScope.键)}
${fn:length(map)}

取得json集合的大小:${covArr}.length;

取得字符串的长度 :${fn:length("hello")}

5、varStatus显示循环变量的状态
例:<c:forEach var="currentFood" items="${sessionScope.foods}" varStatus="status"
<c:if test="${status%2==1}">
.....................
</c:if>
</c:forEach>
其中status这个变量就记录了循环变量的状态

6、如何遍历Map集合
<c:forEach var="foodmap" items="${sessionScope.cart}" varStatus="status">
<c:set var="subtotal" value="${foodmap.value.bean.foodPrice*foodmap.value.quantity}"></c:set>
</c:forEach>
遍历Map集合的值:
foodmap:保存session中的map
foodmap.value:取得map的值,即获取保存在map中的一个对象
要获取对象中的数据,必须用foodmap.value.quantity去点对象的属性(quantity就是对象的属性) 

7.遍历数字
  <c:if test="${totalPages>=1}">
      <a href="javascript:void(0)"  onclick="goback()">上一页</a>
       <c:forEach begin="1" end="${totalPages}" var="i">
         <c:choose>
              <c:when test="${i != num}">
                 <a href="${path}/PageServlet?page=${i}&branchlevel=${branchlevel}&branchtype=${branchtype}">${i}</a>
              </c:when>
              <c:otherwise>
                 <a style="background-color: red" href="${path}/PageServlet?page=${i}&branchlevel=${branchlevel}&branchtype=${branchtype}">${i}</a>
              </c:otherwise>
          </c:choose>
       </c:forEach>
      <a href="javascript:void(0)" onclick="nextpage()">下一页</a>

   </c:if>


原创粉丝点击