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}
注意事项
使用EL表达式是不能直接调用对象方法的!${pageScope.module_lie.id.equals(parent_ids) }这是直接调用了id的equals方法!这样是不行的!
用EL请注意,在EL中值是用“.”来导航的${pageScope.module_lie.id eq parent_ids}
在正则表达式中${}调用不能调用方法,即不可以这样${oauser.getUserId()} ${u.getPostcode()}而是要这样${oauser.userId}要省略括号和get,也就是直接用模型里面的属性就可以了,才能正确运行
<c:if test="${notleader eq (isleader) }"></c:if>

浙公网安备 33010602011771号