目录:
(1)EL介绍
(2)EL表达式的简单使用
(3)EL表达式输出高级 对象属性
(4)EL表达式简化版
(5)EL表达式支持运算
(6)EL表达式其他工具对象
(7)相对路径 绝对路径
(8)EL表达式的缺陷
在未来的实际项目开发中,jsp 与servlet都是以相结合的方式来开发,servlet主做后端业务逻辑处理,jsp主做前端数据显示。
在jsp数据显示的过程中,我们难免会遇到以java脚本的拼接的方式来结合前后端代码。如果java脚本拼接过多,这就涉及到了代码的可读性低与可维护性低的问题。
使用el表达式和jstl标签库可以有效的简化jsp的开发,目的就是为了减少jsp开发中的代码量,避免脚本拼接问题,所以el和jstl是我们开发jsp必用的操作。
(1)EL介绍
index.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 15:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--制造测试数据--%>
<%
request.setAttribute("key","abc");
%>
<%--Java命令读取request数据并写入到响应体中--%>
<%
String value= (String)request.getAttribute("key");
out.write(value);
%>
Java命令写入的结果:<%=value%>
EL表达式写入的结果:${requestScope.key}
结果:
可以看出使用EL表达式的代码得到了简化,提高了开发效率
(2)EL表达式的简单使用
index_2.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 16:53
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
Title
<%--制造测试数据--%>
<%
application.setAttribute("sid",10);
session.setAttribute("sname","mike");
request.setAttribute("sex","man");
pageContext.setAttribute("home","BJ");
%>
学员编号:${applicationScope.sid}
学员姓名:${sessionScope.sname}
学员性别:${requestScope.sex}
学员籍贯:${pageScope.home}
(3)EL表达式输出高级 对象属性
创建Student类:
package com.bjpowernode.model;
public class Student {
private Integer sid;
private String sname;
public Student(Integer sid, String sname) {
this.sid = sid;
this.sname = sname;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
}
index_3.jsp:
<%@ page import="com.bjpowernode.model.Student" %><%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 17:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
Title
<%
Student stu=new Student(20,"allen");
session.setAttribute("stuKey",stu);
%>
学员编号:${sessionScope.stuKey.sid}
学员姓名:${sessionScope.stuKey.sname}
(4)EL表达式简化版
index_4.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 17:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
session.setAttribute("key","小美姑娘");
pageContext.setAttribute("key","猪八戒");
%>
session心仪的女孩名字:${key}
(5)EL表达式支持运算
数学运算:
index_5.jsp:EL表达式会自动的做类型转换
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 17:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
pageContext.setAttribute("key1","100");
pageContext.setAttribute("key2","200");
%>
<%--Java--%>
<%
String num1=(String)pageContext.getAttribute("key1");
String num2=(String)pageContext.getAttribute("key2");
int sum=Integer.valueOf(num1)+Integer.valueOf(num2);
%>
Java命令的运行结果:<%=sum%>
使用EL表达式简化上面的方式:${key1+key2}
关系运算:
在EL表达式里面不支持if else处理,没有控制语句这个选项,它可以通过三元运算符来处理
index_5,jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 17:53
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--制造测试数据--%>
<%
request.setAttribute("age",23);
%>
<%--Java命令处理--%>
<%
Integer age=(Integer)request.getAttribute("age");
if (age>18){
out.write("欢迎光临");
}else{
out.write("谢绝入内");
}
%>
EL表达式简化处理:
${age ge 18?"欢迎光临":"谢绝入内"}
(6)EL表达式其他工具对象
index_7.jsp:发送请求,地址带的参数,用param接收:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 18:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--
发送请求时带的参数
http://localhost:8080/index_7.jsp?uname=smith&password=123
--%>
登录名:${param.uname}
密码:${param.password}
index_8.jsp:接收一组信息
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 18:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--
http://localhost:8080/index_8.jsp?empNo=10&empNo=20
--%>
第一个职员编号:${paramValues.empNo[0]}
第二个职员编号:${paramValues.empNo[1]}
全局作用域当中存在两种共享数据,一种是由Sevlet写入的共享数据。,一种是由tomcat写入的共享数据
在web.xml:中声明一个由tomcat写入的共享数据
driver
com.mysql.jdbc.Driver
index_9.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 18:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
来自web.xml中的共享数据:${initParam.driver}
pageContext对象:
(7)相对路径 绝对路径
这里设置了/myWeb是网站根目录的别名,如果不设置 下方Application Context中 是:/
在代码中访问的网站的根目录web:如果设置了网站的别名,根目录就是/myWeb,没有设置就是 : /
one.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 18:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
Title
one.jsp页面
index.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 18:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
$Title$
index.jsp页面
two.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/19
Time: 18:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
Title
相当路径定位
one.jsp
index.jsp
绝对路径定位
one.jsp
index.jsp