0
点赞
收藏
分享

微信扫一扫

JSP——EL表达式

JSP——EL表达式_前端

本文目录

  • ​​1.1 概念​​
  • ​​1.2 作用​​
  • ​​1.3 EL的应用(获取基本类型、字符串)​​
  • ​​1.3.1 EL应用案例​​
  • ​​1.3.2 EL和JSP脚本的区别​​
  • ​​1.4 EL的应用(获取引用类型)​​
  • ​​1.5 EL的应用(获取数组、集合的元素)​​
  • ​​1.6 EL的运算符​​
  • ​​1.6.1 EL表达式执行运算​​
  • ​​1.6.2 empty关键字​​
  • ​​1.7 隐式对象​​
  • ​​1.7.1 获得应用上下文​​
  • ​​1.7.2 获取Cookie对象​​

JSP——EL表达式_作用域_02

1.1 概念

EL使JSP写起来更简单、简洁。主要用于获取作用域中的数据

1.2 作用

用于替换作用域对象.getAttribute(“name”);

1.3 EL的应用(获取基本类型、字符串)

  • ${scope.name}获取具体某个作用域中的数据
  • ${name}获取作用域中的数据,主机查找{pageContext、request、session、application}

1.3.1 EL应用案例

<%
//存储在request作用域
request.setAttribute("name","zhansgan");
request.setAttribute("age",18);
%>

<%-- 获取request作用域中name对应的值,找到就返回,没找到就返回"" --%>
${requestScope.name}

<%--从最小作用域逐级查找name对应的值,找到就返回,没找到就返回"" -->
${name}

1.3.2 EL和JSP脚本的区别

  • <%=request.getAttribute() %>没有找到返回null
  • ${requestScope.name}没找到返回""

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
String username = "";
String password = "";
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
username = cookie.getValue();
}
if (cookie.getName().equals("password")) {
password = cookie.getValue();
}
}
}
%>
<%=username%><br/>
<%=password%>

<hr/>

<h1>${cookie.username.value}</h1>
<h1>${cookie.password.value}</h1>

<input type="text" name="username" value="<%=username%>"><br/>
<input type="text" name="password" value="<%=password%>"><br/>
<input type="text" name="username" value="${cookie.username.value}"><br/>
<input type="text" name="username" value="${cookie.password.value}"><br/>
</body>
</html>

1.4 EL的应用(获取引用类型)

使用EL获取作用域中的对象调用属性时,只能访问对象的get方法,必须遵守命名规范定义

<%
Emp emp = new Emp();
emp.setName("zhangsan");
emp.setAge(20);
request.setAttribute("emp",emp);
%>
<%--调用getName()方法 --%>
${requestScope.emp.name}

1.5 EL的应用(获取数组、集合的元素)

EL可以获取Array、List、Map中的元素,Set由于没有下标,无法直接访问元素,后续可遍历

<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.cxyzxc.www.entity.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
int[] array = new int[]{11,22,33,44,55};
request.setAttribute("array",array);

List<User> users = new ArrayList<User>();
users.add(new User("zhangsan","123456"));
users.add(new User("lisi","654321"));
users.add(new User("wangwu","112233"));
request.setAttribute("users",users);

Map<String,String> maps = new HashMap<>();
maps.put("CN","中国");
maps.put("US","美国");
maps.put("IT","意大利");
request.setAttribute("maps",maps);
%>

<!--EL访问数组 -->
${array[1]}<br/>
${array[2]}<br/>
${array[3]}<br/>

<hr/>

${users[0]}<br/>
${users[1]}<br/>
${users.get(2)}

<hr/>

${maps["CN"]}<br/>
${maps["US"]}<br/>
${maps.IT}

</body>
</html>

1.6 EL的运算符

操作符

描述

.

访问一个Bean属性或者一个映射条目

[]

访问一个数组或者链表的元素

+


-

减或负

*


/ or div


% or mod

取模

== or eq

测试是否相等

!= or ne

测试是否不等

< or lt

测试是否小于

> or gt

测试是否大于

<= or le

测试是否小于等于

>= or ge

测试是否大于等于

&& or and

测试逻辑与

|| or or

测试逻辑或

! or not

测试取反

empty

测试是否空值

1.6.1 EL表达式执行运算

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("nums", 1234);
%>

<h1>算术运算符</h1>
<h1>${nums+5} </h1>
<h1>${nums - 5 } </h1>
<h1>${nums * 5 } </h1>
<h1>${nums div 5 } </h1>
<h1>${nums mod 5 } </h1>

<hr/>

<h1>关系运算符</h1>
<h1>${nums eq 1234}</h1>
<h1>${nums ne 1234}</h1>
<h1>${nums gt 1234}</h1>
<h1>${nums lt 1234}</h1>
<h1>${nums ge 1234}</h1>
<h1>${nums le 1234}</h1>

<hr/>

<h1>逻辑运算符</h1>
<h1>${nums > 1000 and nums !=1200}</h1>
<h1>${nums > 1000 or nums == 2000}</h1>
<h1>${not(num > 1234)}</h1>

</body>
</html>

1.6.2 empty关键字

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- 值设置为“”或者null,返回结果都为true,其它情况但会false--%>
<%
request.setAttribute("ss", "");
%>

<h1>empty运算符</h1>
<h1>${empty ss}</h1>

</body>
</html>

1.7 隐式对象

EL表达式语言定义了11个隐式对象

隐含对象

描述

pageScope

page作用域

requestScope

request作用域

sessionScope

session作用域

applicationScope

application作用域

param

Request对象的参数,字符串

paramValues

Request对象的参数,字符串集合

header

HTTP信息头,字符串

headerValues

HTTP信息头,字符串集合

initParam

上下文初始化参数

cookie

Cookie值

pageContext

当前页面的pageContext

1.7.1 获得应用上下文

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String path = request.getContextPath();
%>
<%=path%>
<br/>
${pageContext.request.contextPath}


<%-- <br/>--%>
<%-- <a href="<%=request.getContextPath()%>/manager/safe/xxxController">Click me</a><br/>--%>
<%-- <a href="${pageContext.request.contextPath}/manager/safe/xxxController">Click target</a>--%>
</body>
</html>

1.7.2 获取Cookie对象

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
String username = "";
String password = "";
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
username = cookie.getValue();
}
if (cookie.getName().equals("password")) {
password = cookie.getValue();
}
}
}
%>
<%=username%><br/>
<%=password%>

<hr/>

<h1>${cookie.username.value}</h1>
<h1>${cookie.password.value}</h1>

<input type="text" name="username" value="<%=username%>"><br/>
<input type="text" name="password" value="<%=password%>"><br/>
<input type="text" name="username" value="${cookie.username.value}"><br/>
<input type="text" name="username" value="${cookie.password.value}"><br/>
</body>
</html>

举报

相关推荐

0 条评论