web.EL表达式与JSTL

一.首先来了解jsp的四个域对象

application 应用级储存:属性的作用范围仅限于当前web应用(范围最大)

session 会话:属性的作用范围仅限于一次会话,浏览器打开直到关闭称为一次会话(在此期间会话不失效)

request 转发:属性的作用范围仅限同一个请求(转发有效)

pageContext: 属性的作用范围仅限当前的jsp页面(范围最小)

大小顺序排列:application>session>request>page

EL(Expression Language)表达式:

功能:替代jsp页面中的复杂代码

语法:${EL expression}  
           ${bean.name}

EL中的隐含对象(pageScope、requestScope、sessionScope、applicationScope)
EL运算符(算术 关系 Empty运算符【判断集合中值是否为空 返回true或者false】)

二.代码展示

1.通过set将值存入这些对象中

<jsp:forward page="text.jsp"/>标准标签,跳转

<%
application.setAttribute("mgs","你好");
session.setAttribute("mgs","hello");
request.setAttribute("mgs", "hello world");
%>
<jsp:forward page="text.jsp"/>

2.跳转后页面的操作

 pageContext.setAttribute("mgs", "hello 你好");只有在本界面能调用

${user.name.equals("root")?"yes":"no"}三元运算

${us.size()==0}判断集合有无数据

${empty us}有数据

${not empty us}没有数据

<%
    pageContext.setAttribute("mgs", "hello 你好");
    int sum=100;
    pageContext.setAttribute("sum", sum);
    User u=new User("root","123");
    pageContext.setAttribute("user",u);
    List<User> list=new ArrayList<>();
//将集合存入page
    pageContext.setAttribute("us",list);
    %>
    
${applicationScope.mgs}
<br>
${sessionScope.mgs}
<br>
${requestScope.mgs}
<br>
${mgs}
<br>
${sum}
<br>
${user}
<br>
${user.name}
<br>
${user.pwd}
<br>
${user.name.equals("root")?"yes":"no"}
<br>
${us.size()==0}--${empty us}---${not empty us}

3.JSTL

通用标签:set out remove
条件标签:if
迭代标签:forEach 

需要导入两个jar包

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>导入标签

<c:if test="${yy==null}">if判断

<c:forEach items="${list}" var="user">forEach遍历

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String yy=request.getParameter("yy");
pageContext.setAttribute("yy", yy);
List<User> list=new ArrayList<>();
for(int i=0;i<5;i++){
	User u=new User();
	u.setName("好好"+i);
	u.setPwd("123"+i);
	list.add(u);
}
pageContext.setAttribute("list", list);

%>
<c:set scope="page" value="1" var="a"></c:set>
<c:out value="${a}"></c:out>
<c:remove var="a" scope="page"></c:remove>
<c:if test="${yy!=null}">
<h1>hhhh</h1>
</c:if>
<c:if test="${yy==null}">
<button>请登录</button>
</c:if>
<!-- 商品遍历 -->
<table border>
<tr>
<th>商品名字</th>
<th>商品价格</th>
</tr>
<c:forEach items="${list}" var="user">
<tr>
<th>${user.name}</th>
<th>${user.pwd}</th>
</tr>
</c:forEach>
</table>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值