EL表达式判断Map是否为空和map的取值

来源:互联网 发布:淘宝全民切红包 编辑:程序博客网 时间:2024/05/21 10:28
EL表达式判断Map是否为空和map的取值


1.  Map<String, Object> fieldMap
<c:if test="${fieldMap['realname'].showstatus == 1}">


action中的代码

private Map<String, String> msgs = new HashMap<String, String>
msgs.put("loginError", "验证码错误");


jsp页面中的代码:
<script type="text/javascript">
    var msgTip = "${msgs['loginError']}";
    alert(msgTip);
</script>
--------------------------------------------------------------------------------------------------------------------------------------
注意事项:map名后不要加点,直接是[]
          key要用单引号
          在js中写,要在整个取值外面加双引号

1.在JSP页面中引入sun的核心标签库
     <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
2.在JSP页面中引入sun的函数标签库
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions"  prefix="fn"%>
3.判断是否为空
    <c:if test="${!empty map}">
    </c:if>
4.判断长度大于0
    <c:if test="${fn:length(map)>0}">
    </c:if>
注:fn:length(obj),对于空对象也会返回0
5.例子:将request传入的hashmap显示在表格中    
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding
="utf-8" import="java.util.HashMap,java.util.Iterator"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %><!-- 引入sun的核心标签库 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions"  prefix="fn"%><!-- 引入sun的函数标签库 -->
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>显示jsp传递的参数-使用标签库</title>
</head>
<body>
<center><h1>传递的参数</h1></center>
<hr/>
<c:choose>
    <c:when test="${fn:length(requestScope.paramMap)>0}">
          <table border="1">
              <tr><th>参数名</th><th>参数值</th></tr>
            <c:forEach items="${requestScope.paramMap}" var="map">
                  <tr><td>${map.key}</td><td>${map.value}</td></tr>
             </c:forEach>
         </table>
    </c:when>
    <c:otherwise>
        <c:out value="没有在request中传递paramMap属性"/><br/>
        <p>paramMap大小为:${fn:length(requestScope.paramMap)}</p>
    </c:otherwise>
</c:choose>
</body>
</html>
0 0
原创粉丝点击