- <%
- String pageValue = "Hello This is page";
- pageContext.setAttribute("pageV", pageValue);
- request.setAttribute("reqValue",
- "<font color='red'>Hello Request</font>");
- session.setAttribute("sessionValue",
- "<font color='green'>Hello session</font>");
- application.setAttribute("appValue",
- "<font color='blue'>Hello Application</font>");
- %>
- <!-- EL/JSTL复习总结 -->
- <h1>1.JSP脚本:</h1>
- <div>
- JSP表达式:<%=pageContext.getAttribute("pageV")%><br />
- JSP表达式:<%=request.getAttribute("reqValue")%><br />
- JSP表达式:<%=session.getAttribute("sessionValue")%><br />
- JSP表达式:<%=application.getAttribute("appValue")%><br />
- </div>
- <h1>2.EL表达式:</h1>
- <div>
- EL表达式:${pageV }<br/>
- EL表达式:${reqValue }<br/>
- EL表达式:${sessionValue }<br/>
- EL表达式:${appValue }<br/>
- 【查找顺序:pageScope-requestScope-sessionScope-applicationScope】
- </div>
- <h1>3.EL表达式+scope:</h1>
- <div>
- EL表达式:【pageScope】:${pageScope.pageV }<br/>
- EL表达式:【requestScope】:${requestScope.reqValue }<br/>
- EL表达式:【sessionScope】:${sessionScope.sessionValue }<br/>
- EL表达式:【applicationScope】:${applicationScope.appValue }<br/>
- </div>
- <h1>4.JSTL表达式:</h1>
- <div>
- JSTL表达式:this【pageScope】:<c:out value="${pageScope.pageV }" /><br/>
- JSTL表达式:request:【requestScope】【escapeXml="false"】<c:out value="${requestScope.reqValue }" escapeXml="false"/><br/>
- JSTL表达式:session:【sessionScope】<c:out value="${sessionScope.sessionValue }" /><br/>
- JSTL表达式:application:【applicationScope】<c:out value="${applicationScope.appValue }" /><br/>
- JSTL表达式:page:<c:out value="${applicationScope.appValue }" /><br/>
- JSTL表达式:noThisValue【default="ErrorEmpty"】:<c:out value="${noThisValue }" default="<font size='+3'>ErrorEmpty</font>" escapeXml="false"/><br/>
- </div>
输出:
JSP脚本:
JSP表达式:Hello This is page
JSP表达式: Hello Request
JSP表达式: Hello session
JSP表达式: Hello Application
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表达式: 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
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
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]}
- <%
- //初始化User
- List<User> users = new ArrayList<User>();
- for (int i = 0; i < 20; i++) {
- User u = new User();
- u.setUsername("baebae_" + i);
- if (i % 2 == 0) {
- u.setGender(EnumSex.M);
- } else {
- u.setGender(EnumSex.F);
- }
- if (i % 3 == 0) {
- u.setDept(new Dept("行政"));
- } else {
- u.setDept(new Dept("IT开发组"));
- }
- users.add(u);
- }
- request.setAttribute("userList", users);
- request.setAttribute("splitString", "ab:bc,cd,de;ef,fg#gh,hi");
- %>
- <h1>JSTL:</h1>
- <table>
- <thead>
- <th width="200">用户名</th>
- <th width="120">性别</th>
- <th width="200">所在部门</th>
- </thead>
- <c:choose>
- <c:when test="${empty userList }">
- <tr>
- <td rowspan="3">用户为空,请检查代码或者数据库</td>
- </c:when>
- <c:otherwise>
- <c:forEach
- var="user"
- items="${userList }"
- varStatus="vs"
- >
- <c:choose>
- <c:when test="${vs.count % 2 ne 0 }">
- <tr bgcolor="#669911">
- </c:when>
- <c:otherwise>
- <tr>
- </c:otherwise>
- </c:choose>
- <td>${user.username }</td>
- <td><c:choose>
- <c:when test="${user.gender eq \"M\" }">
- 男
- </c:when>
- <c:when test="${user.gender eq \"F\" }">
- 女
- </c:when>
- <c:otherwise>
- 不确定
- </c:otherwise>
- </c:choose></td>
- <td>${user.dept.dname }</td>
- </tr>
- </c:forEach>
- </c:otherwise>
- </c:choose>
- </table>
输出结果为:
<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