栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

EL表达式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

EL表达式

一.EL表达式介绍
  • Expression Language表达式语言
  • 目的:为了使JSP写起来更加简单。
  • 表达式语言的灵感来自于 ECMAScript 和 XPath表达式语言,它提供了在 JSP 中简化表达式的方法。
  • EL表达式的格式     ${表达式}
二.常用的表达式 1.运算符 a.算术运算符:+, -, *, /
				${1+1 }
${2-1 }
${2*2 }
${2/1 }
${5%2 }
b.比较运算符:> < >= <= ==
				${1 > 2 }
${2 >= 2 }
${2 == 1 }

注:输出值为true或者是false

c.逻辑运算符: &&(and) ||(or) !(not)
				${true && true }
${true and false}
${true || false }
${true or false}
${!true }
${not true}
d.空运算符:empty

用来判断字符串,数组,集合中的数据是否为null或者是否长度为0

				<%
					//String s = null;
					//String s = "";
					//String s = "hello";
					//request.setAttribute("s", s);
					
					ArrayList al = new ArrayList<>();
					
					al.add("aaa");
					
					request.setAttribute("al", al);
				%>
				
				<%-- ${empty s}
${not empty s} --%> ${empty al}
2.获取值
  • EL只能从四大域中获取属性
  • 如果想要EL表达式来获取数据,必须将数据存储到域中,我们通过EL表达式从域中获取数据
域名称:

                request --> requestScope
                session --> sessionScope
                pageContext --> pageScope
                application --> applicationScope

方法:

a.  ${域名称.键名}

               <%

					String name = "jack";
					//request.setAttribute("aaa", name);
					//session.setAttribute("aaa", name);
					//pageContext.setAttribute("aaa", name);
					application.setAttribute("aaa", name);
				%>
				
				<%-- ${requestScope.aaa} --%>
				<%-- ${sessionScope.aaa } --%>
				<%-- ${pageScope.aaa} --%>
				${applicationScope.aaa }

b.${键名}

注意:这种写法会自动的从域中查询键名,有就获取,没有就不获取,但是查找也分先后顺序,              从小到大查找

pageContext < request < session < application

                <%
					
					
					//pageContext.setAttribute("name", "tom");
					//request.setAttribute("name", "jerry");
					//session.setAttribute("name", "rose");
					//application.setAttribute("name", "jack");
				%>

				<%-- ${requestScope.name} --%>
				${name }
3.获取对象中的数据 a.获取普通类对象的数据 

   ${键名.属性名}

				<%
					User user = new User();
					user.setName("tom");
					user.setAge(18);
					
					request.setAttribute("user", user);
				%>
				
				${user }
${user.name}
${user.age }
${user.aaa }
b.获取集合类对象的数据

    1.单列集合 

       ${键名[索引]}

                   <%
						ArrayList al = new ArrayList();
					
						al.add("aaa");
						al.add("bbb");
						al.add("ccc");
						
						application.setAttribute("list", al);
					%>
					
					${list }
${list[0] }
${list[1] }
${list[2] }

2.双列集合

                    ${域中的键名.集合中的键名}
                    ${域中的键名['集合中的键名']}

					<%
						Map map = new HashMap<>();
					
						map.put("name", "tom");
						map.put("age", "18");
						
						application.setAttribute("map", map);
					%>
					
					${map }
${map.name }
${map.age }
${map['name'] }
${map['age'] }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/895340.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号