El表达式

62 篇文章 3 订阅
本文介绍了EL(Expression Language)表达式在JSP中的使用,包括其基本语法、作用域对象和输出方式。EL简化了JSP中的数据输出,提高了代码可读性。文章通过示例展示了如何替换out.println()方法,以及如何通过request、session等作用域对象存取和输出数据。此外,还提到了EL表达式内置的对象如param,用于方便地输出请求参数值。
摘要由CSDN通过智能技术生成

El表达式

EL(Expression Language)表达式语言,用于简化JSP的输出

EL表达式的基本语法:${表达式}

示例:<h1>学生姓名:${student.name}</h1>

在绝大多数场景下都是为了替换原有的out.print()语句,比原有的JSP输出提高了可读性

例子:

el.Student.java

package el;

public class Student {
	private String name;
	private String mobile;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	
}

el.StudentServlet.java

@WebServlet("/info")
//...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	Student stu = new Student();
	stu.setName("韩信");
	stu.setMobile(null);
	String grade = "A";
	
	request.setAttribute("student", stu);
	request.setAttribute("grade", grade);
	request.getRequestDispatcher("/info.jsp").forward(request, response);	//请求转发
	
}

info.jsp

out.plint()方法输出内容

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" import="el.Student" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<%
		Student stu = (Student)request.getAttribute("student");
		out.println("<h1>"+stu.getName()+"</h1>");
	%>
</body>
</html>

el表达式方式,不用考虑导入和字符串拼接的问题,一切都像html的书写方式,在原有的输出数据的地方也不需要增加jsp代码块

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<h1>姓名:${requestScope.student.name }</h1>
	<h2>手机:${requestScope.student.mobile }</h2>
	<h2>等级:${requestScope.grade }</h2>
</body>
</html>

EL表达式的作用域对象

  • El表达式内置四种作用域对象
  • 忽略书写作用域对象时,EL则按作用域从小到大依次尝试获取
作用域对象描述
pageScope从当前页面取值
requestScope从当前请求中获取属性值
sessionScope从当前会话中获取属性值
applicationScope从当前应用获取全局属性值

尝试将request中的属性存储到session中,之前的信息则获取不到了,将infoi.jsp只作用域对象requestScope改成sessionScope或者忽略便可获取到数据

el.StudentServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	Student stu = new Student();
	stu.setName("韩信");
	stu.setMobile(null);
	String grade = "A";
	
    //设置全局属性
    request.getServletContext().setAttribute("grade", "C");
    //设置session
	HttpSession session = request.getSession();
	session.setAttribute("student", stu);
	session.setAttribute("grade", grade);
	
//		request.setAttribute("student", stu);
//		request.setAttribute("grade", grade);
	request.getRequestDispatcher("/info.jsp").forward(request, response);	//请求转发
	
}

EL表达式输出

语法:${[作用域.]属性名[.子属性]}

El表达式支持将运算结果进行输出

${emp.salary+1000}
${1<=3&&2>4}

EL支持绝大多对象输出,本质是执行toString()方法

EL输出参数值

El表达式内置param对象来简化参数的输出

语法:${param.参数名}

el.jsp

<h2>讲师:${param.teacher }</h2>

网址加参数:localhost:8080/el/info?teacher=老头

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摘星喵Pro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值