0
点赞
收藏
分享

微信扫一扫

Web.16.域对象&EL表达式&JSTL标签

松鼠树屋 2022-04-17 阅读 75

域对象包括有哪几个?JSTL标签有哪些?


目录:

        一. jsp内置对象之域对象

        二. EL表达式

                使用EL表达式

                EL运算符

                补充

        三. JSTL标签

                JSTL基本标签

                使用操作

                JSTL便签库的使用


 一. jsp内置对象之域对象

在JSP中,包含四大域对象,分别为:

* pageContext  页面容器

* request  请求

* session 服务器存储

* application 公共存储

它们的存储大小按大到小的顺序为:

application>session>request>pageContext

下面是四种域对象的详细介绍:


二. EL表达式

        EL:表达式语言(全称:Expression Language)

        注意EL表达式必须要从域对象中取值,但不能完全替代<%= %>的取值方式


 使用EL表达式:    (举例)

 —先向域对象中存值:

request.setAttribute("msg1", "hello 你好");
	application.setAttribute("msg2", "hello word");
	session.setAttribute("msg3", "你好");

—再使用EL表达式取出:

application:${msg2}
	<br>
	session:${msg3}
	<br>
	request.${msg1}

 运行结果:

  

 

 当application,session,request,pageContext 四个对象存入时的属性名相同时:

application.setAttribute("msg", "hello word");
	session.setAttribute("msg", "你好");
	request.setAttribute("msg", "hello 你好");
	pageContext.setAttribute("msg", "hello");

  使用存入的属性名取值:

application:${msg}
	<br>
	session:${msg}
	<br>
	request.${msg}
	<br>
	page:${msg}

  结果取到的值都是最小的域pageContext里面的值:

  

 

  EL中的隐含对象:(使用它们在EL中的名称来取值 )

        * pageScope

        * requestScope

        * sessionScope

        * applicationScope

application:${applicationScope.msg}
	<br>
	session:${sessionScope.msg}
	<br>
	request.${requestScope.msg==null?"x":"y"}
	<br>
	page:${msg}

 效果如下:

  

 

 EL隐式对象小结:

    

 

  EL隐式对象介绍:

  


 EL运算符

 在使用EL表达式取值时,可以在${}中对取到的值进行一些运算符操作

 运算符可以是布尔类型的运算符或者三元运算符

 


补充:

存入集合:

List<User> list=new ArrayList<>();
	pageContext.setAttribute("us", list);

判断集合是否为空:

${us.size()==0}-${empty us}-${not empty us}


三. JSTL标签:

JSTL基本标签

使用操作

注意:在使用之前,一定要将 jstl标签库 导入到当前项目中

导入jstl 标签库的jar包,将它放到lib目录下方,并右键点击build Path,生成牛奶瓶,然后就算添加成功:

添加成功后就是这个样子的:

添加成功后,还需要在页面上编写导入jstl标签库的语句

<%--tag:标签  lib库,导入一个标签库  prefix:是标签库的名字 --%>  
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>


JSTL便签库的使用

 * 通用标签:set,out,remove

<%--pageContext.setAttribute("a", 1); --%>
	<c:set scope="page" value="1" var="a"></c:set>
	
	<c:out value="${a }"></c:out>
	
	<%--pageContext.setAttribute("a") --%>
	<c:remove var="a" scope="page"></c:remove>
	

 * 条件标签:if

<%--如果登录了 显示你好 --%>
	<%--如果没有登录了 显示一个登陆 --%>
	
	<c:if test="${yy!=null }">
	<h1>欢迎使用该系统</h1>
	<h1>哈哈哈哈嗝</h1>
	</c:if>
	
	<c:if test="${yy==null }">
	<h1>你现在还没有登陆,请点击下方的登陆</h1>
	<button>请进行登录</button>	
	</c:if>

 * 迭代标签:forEach

<%--商品的遍历 --%>
	<table border>
		<tr>
			<th>商品编号</th>
			<th>商品名称</th>
			<th>商品类型</th>
			<th>商品价格</th>
		</tr>
		<%
			for(Goods goods:list){
				goods.getGoodsName();
			}
		%>
		<c:forEach items="${list}" var="goods">
			<tr>
			<th>${goods.goodsId}</th>
			<th>${goods.goodsName }</th>
			<th>${goods.goodsType }</th>
			<th>${goods.goodsPrice}</th>
		</tr>
		</c:forEach>


感谢收看,后面的内容更精彩哦~

举报

相关推荐

0 条评论