EL表达式/JSTL复习总结

来源:互联网 发布:js函数not defined 编辑:程序博客网 时间:2024/06/08 13:02
Html代码  收藏代码
  1. <%  
  2.     String pageValue = "Hello This is page";  
  3.     pageContext.setAttribute("pageV", pageValue);  
  4.     request.setAttribute("reqValue",  
  5.             "<font color='red'>Hello Request</font>");  
  6.     session.setAttribute("sessionValue",  
  7.             "<font color='green'>Hello session</font>");  
  8.     application.setAttribute("appValue",  
  9.             "<font color='blue'>Hello Application</font>");  
  10. %>  
  11.   
  12. <!-- EL/JSTL复习总结 -->    
  13. <h1>1.JSP脚本:</h1>  
  14. <div>  
  15.     JSP表达式:<%=pageContext.getAttribute("pageV")%><br />  
  16.     JSP表达式:<%=request.getAttribute("reqValue")%><br />  
  17.     JSP表达式:<%=session.getAttribute("sessionValue")%><br />  
  18.     JSP表达式:<%=application.getAttribute("appValue")%><br />  
  19. </div>  
  20. <h1>2.EL表达式:</h1>  
  21. <div>  
  22.     EL表达式:${pageV }<br/>  
  23.     EL表达式:${reqValue }<br/>  
  24.     EL表达式:${sessionValue }<br/>  
  25.     EL表达式:${appValue }<br/>  
  26.     【查找顺序:pageScope-requestScope-sessionScope-applicationScope】  
  27. </div>  
  28. <h1>3.EL表达式+scope:</h1>  
  29. <div>  
  30.     EL表达式:【pageScope】:${pageScope.pageV }<br/>  
  31.     EL表达式:【requestScope】:${requestScope.reqValue }<br/>  
  32.     EL表达式:【sessionScope】:${sessionScope.sessionValue }<br/>  
  33.     EL表达式:【applicationScope】:${applicationScope.appValue }<br/>  
  34. </div>  
  35. <h1>4.JSTL表达式:</h1>  
  36. <div>  
  37.     JSTL表达式:this【pageScope】:<c:out value="${pageScope.pageV }" /><br/>  
  38.     JSTL表达式:request:【requestScope】【escapeXml="false"<c:out value="${requestScope.reqValue }" escapeXml="false"/><br/>  
  39.     JSTL表达式:session:【sessionScope】<c:out value="${sessionScope.sessionValue }" /><br/>  
  40.     JSTL表达式:application:【applicationScope】<c:out value="${applicationScope.appValue }" /><br/>  
  41.     JSTL表达式:page:<c:out value="${applicationScope.appValue }" /><br/>  
  42.     JSTL表达式:noThisValue【default="ErrorEmpty"】:<c:out value="${noThisValue }" default="<font size='+3'>ErrorEmpty</font>" escapeXml="false"/><br/>  
  43. </div>  

 输出:

JSP脚本:

JSP表达式:Hello This is page
JSP表达式:Hello Request
JSP表达式:Hello session
JSP表达式:Hello Application

EL表达式:

EL表达式:Hello This is page
EL表达式:Hello Request
EL表达式:Hello session
EL表达式:Hello Application
【查找顺序:pageScope-requestScope-sessionScope-applicationScope】

EL表达式:

EL表达式:【pageScope】:Hello This is page
EL表达式:【requestScope】:Hello Request
EL表达式:【sessionScope】:Hello session
EL表达式:【applicationScope】:Hello Application

JSTL表达式:

JSTL表达式:this【pageScope】:Hello This is page
JSTL表达式:request:【requestScope】【escapeXml="false"】Hello Request
JSTL表达式:session:【sessionScope】<font color='green'>Hello session</font>
JSTL表达式:application:【applicationScope】<font color='blue'>Hello Application</font>
JSTL表达式:page:<font color='blue'>Hello Application</font>
JSTL表达式:noThisValue【default="ErrorEmpty"】:ErrorEmpty
[要点]
1.EL表达式的作用域
2.JSTL<c:out value="">的default、escapeXml属性
***********************
EL表达式:
名字:${user.username }
性别:<c:if test="${user.gender != \"F\"}">男</c:if> 
所在系:${user.dept.dname }
访问map:[map名.keyx]:${user.dept.map.k1}
访问array[index]:${user.dept.arrays[0]}

访问list[index]:${user.dept.list[0]}


Html代码  收藏代码
  1. <%  
  2.     //初始化User  
  3.     List<User> users = new ArrayList<User>();  
  4.     for (int i = 0; i < 20; i++) {  
  5.         User u = new User();  
  6.         u.setUsername("baebae_" + i);  
  7.         if (i % 2 == 0) {  
  8.             u.setGender(EnumSex.M);  
  9.         } else {  
  10.             u.setGender(EnumSex.F);  
  11.         }  
  12.   
  13.         if (i % 3 == 0) {  
  14.             u.setDept(new Dept("行政"));  
  15.         } else {  
  16.             u.setDept(new Dept("IT开发组"));  
  17.         }  
  18.         users.add(u);  
  19.     }  
  20.     request.setAttribute("userList", users);  
  21.         request.setAttribute("splitString", "ab:bc,cd,de;ef,fg#gh,hi");  
  22. %>  
  23.   
  24. <h1>JSTL:</h1>  
  25. <table>  
  26.     <thead>  
  27.         <th width="200">用户名</th>  
  28.         <th width="120">性别</th>  
  29.         <th width="200">所在部门</th>  
  30.     </thead>  
  31.     <c:choose>  
  32.         <c:when test="${empty userList }">  
  33.             <tr>  
  34.                 <td rowspan="3">用户为空,请检查代码或者数据库</td>  
  35.         </c:when>  
  36.         <c:otherwise>  
  37.             <c:forEach  
  38.                 var="user"  
  39.                 items="${userList }"  
  40.                 varStatus="vs"  
  41.             >  
  42.                 <c:choose>  
  43.                     <c:when test="${vs.count % 2 ne 0 }">  
  44.                         <tr bgcolor="#669911">  
  45.                     </c:when>  
  46.                     <c:otherwise>  
  47.                         <tr>  
  48.                     </c:otherwise>  
  49.                 </c:choose>  
  50.                 <td>${user.username }</td>  
  51.                 <td><c:choose>  
  52.                         <c:when test="${user.gender eq \"M\" }">  
  53.                             男  
  54.                             </c:when>  
  55.                         <c:when test="${user.gender eq \"F\" }">  
  56.                             女  
  57.                             </c:when>  
  58.                         <c:otherwise>  
  59.                             不确定  
  60.                             </c:otherwise>  
  61.                     </c:choose></td>  
  62.                 <td>${user.dept.dname }</td>  
  63.                 </tr>  
  64.             </c:forEach>  
  65.         </c:otherwise>  
  66.     </c:choose>  
  67.   
  68. </table>  

 输出结果为:

JSTL

<1>.判断

 

<c:if test="${var}" ></c:if>

<c:choose>

    <c:when test="${var}" var="v">

    </c:when>

    <c:when test="${var}" >

    </c:when>

    <c:otherwise>

    </c:otherwise>

</c:choose>

 

<2>.遍历:

<c:forEach items="${list}" var="item" begin="0" end="${fn:length(userList)" step="1" varStatus="vs"

   

</c:forEach>

<c:forTokens items="${splitString }" delims=",:;#" var="v" >

${v }<br/>

</c:forTokens>

效果:

ab
bc
cd
de
ef
fg
gh
hi


0 0
原创粉丝点击