zoukankan      html  css  js  c++  java
  • java web----jstl和EL表达式

    jstl使用

    jstl手册:https://www.runoob.com/jsp/jsp-jstl.html

    1、在pom中引入依赖(导入jar包)

    在maven 仓库中可以查看最新版

    <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
    </dependency>
    

    2、在jsp中插入

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    

      

    使用

    <c:if>标签

    如果message为空,就不显示;$(message!=null)

    利用这种思路

    如果简单可以使用这种方式来替代jstl

    <div class="callout callout-danger" ${message==null?"style='display: none'":""}>
                <h4>${message}</h4>
    </div>

    或者

    <div class="callout callout-danger" style="display: ${message==null?"none":""};" }>
                <h4>${message}</h4>
    </div>
    

      

    或者在jsp中使用java代码块,来判断(使用jstl代替java代码块)

     

    <c:forEach>标签

     如果for循环的是map集合

    <c:forEach items="${map}" var="map">
        ${map.key}---${map.value}       //不可以直接点键名
    </c:forEach>
    

      

    <c:forEach items="${tbUsers}" var="tbUser" varStatus="status">
       <tr>
            <td><input id="${tbUser.id}" type="checkbox" class="flat-red"> ${status.index+1}</td>
            <td>${tbUser.username}</td>
            <td>${tbUser.phone}</td>
            <td>${tbUser.email}</td>
            <td><fmt:formatDate value="${tbUser.updated}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate></td>
            <td>
            </td>
        </tr>
    </c:forEach>
    

    varStatus="status" 提供计数功能; ${status.index}从0开始,${status.count}从1开始

       

    EL表达式

    只获取的是pageContext、request、session、application四个对象中的数据,如果${xx},中的xx找不到,就什么不做

    <body>
    ${param.uname}             //<%=request.getParameter("uname")%>
    ${str}                     //<%=request.getAttribute("str")%>
    ${user.addr.town}          //<%=((User)request.getAttribute("user")).getAddr().getTown()%>
    ${list[2]}                 //<%=((Arraylist)request.getAttribute("list")).get(2)%>
    ${list[0].addr.pre}        //<%=((User)(((Arraylist)request.getAttribute("list2")).get(0))).getAddr().getpre()%>
    ${Map.c}                   //<%=((HashMap)request.getAttribute("map")).get("c")%>
    ${Map2.a1.addr.city}       //<%=((User)(((HashMap)request.getAttribute("map2")).get("al"))).getAddr().getcity()%>
    </body>

    查找顺序:

      pageContext--->request--->session--->application  找到就不在找了

    指定作用域查找

    ${pageScope.键名}         //取出pageContext中的数据
    ${requestScope.键名}      //取出request中的数据
    ${sessionScope.键名}      //取出session中的数据
    ${applicationScope.键名}  //取出application中的数据(存放所有的用户都可以访问的数据)
    

    EL逻辑运算

    ${1+2}==${1+"2"}          //等于3,${1+"a"}会报错,a不能转成数字,所以EL表达式不支持字符串的连接
    ${1==1?"true":"false"} //三元运算

    表达式空值判断

    ${empty 键名}  //判断键名对应的对象是否为空,("",空列表,空hashpmap都为true,注意如果是new 对象(这个对象默认属性是有值的,那就不是空了,false))
    

    获取请求头和cookie

    ${header["键名"]}
    ${headerValues["键名"][0]}
    ${cookie.键名.name}
    ${cookie.键名.value}
    

      

  • 相关阅读:
    EveryOne Piano
    3D扫描商店
    Substance PBR Guide
    C2GOASM
    Unity调试外部DLL
    BinaryReader优化
    IL
    如何画头发
    Bitrix24
    Mac查找包含指定字符串的文件
  • 原文地址:https://www.cnblogs.com/yanxiaoge/p/10825108.html
Copyright © 2011-2022 走看看