您的位置:首页 > 编程语言 > Java开发

JavaWeb小项目涉及的基础知识02

2017-05-22 23:49 531 查看
1 EL语法

首先,EL,Jsp,和JSTL中各有四个域可以设置以及更改删除值,通常以键值对的方式,其中四个域分别为:

JSP 中的四个域: pageContext request session application
EL 中的四个域: pageScope requestScope sessionScope applicationScope
JSTL中的四个域: page request session application


(1)使用EL表达式访问域中的数据:

${xxxScope.yyy}

注意:
语法1:对象.字段名 的方式访问 (常用)
语法2: 对象['字段名']的方式访问  ---- 字段名有特殊符号或数组访问时,使用该语法


(2)获取 Cookie

${cookie } //获取所有的 Cookie ,存储在 Map 集合 键=Cookie 的键 值=Cookie对象
${cookie.键 } //获取到 Cookie 对象
${cookie.键.name } //cookie 对象的键
${cookie.键.value }//cookie 对象的值


(3)获取JavaBean中的封装的数据

request.setAttribute("u", new User(XXX,YYY));
//获取
${u.name}----------》XXX
S{u.age}----------->YYY


2 JSTL语法

JSTL主要在JSP页面中起着流程控制的作用,可以有着判断和循环输出等功能,实现页面的动态显示。

判断语句:<c:if test="布尔表达式"> 值 </c:if>
如:
<c:set var="num" value="1"></c:set>
<c:if test="${num > 0 }">正数 </c:if> ----》正数
判断语句:<c:choose>
<c:when test=""></c:when>
<c:when test=""></c:when>
<c:when test=""></c:when>
....
<c:otherwise></c:otherwise>
</c:choose>

如:

<c:choose>
<c:when test="${num < 0 }">
负数!
</c:when>
<c:when test="${num == 0 }">
0!
</c:when>
<c:otherwise>
正数!
</c:otherwise>
如JSP分页:
<c:choose>
<c:when test="${pb.pageNumber>1 }">
<a href="${pageContext.request.contextPath}/FindAll05?pageNum=${pb.pageNumber-1}">上一页</a>
</c:when>
<c:otherwise>
<a>上一页</a>
</c:otherwise>
</c:choose>


循环输出语句:

<c:forEach
var="" ---- 声明当前遍历到的元素的变量名
items="" -- 被遍历的容器
step="" --- 设置步进值
begin end -- 设置起止范围
varStatus -- 获取当前元素信息
index ---- 索引
count ---- 个数
first ---- 是不是第一个
last ----- 是不是最后一个
>

注意:循环输出集合------》
<c:forEach var="p" itmes="${list}" > 输出语句</c:forEach>
如循环输出JSP页面:

<c:forEach var="p" items="${pb.list }">
<tr>
<td><input type="checkbox" value="${p.pid }"/></td>
<td>${p.pid }</td>
<td>${p.pname }</td>
<td><img src="${p.pimage }"/></td>
<td>${p.shop_price}</td>
<td>${p.pdesc }</td>
<td width="100px;">
<a href="${pageContext.request.contextPath}/FindProduct2UI?pid=${p.pid}">修改此商品</a>    <br/>
<a href="${pageContext.request.contextPath}/DeleteProduct?pid=${p.pid}" onclick="return test()">删除此商品</a>
</td>
</tr>
</c:forEach>
循环输出数字:
<c:forEach begin="1" end="10" var ="current">
如JSP分页:

<c:forEach begin="1" end="${pb.totalPage }" var="current">
<a href="${pageContext.request.contextPath}/FindAll05?pageNum=${current}">${current }</a>
</c:forEach>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: