EL表达式的用法

1.创建一个动态web项目:目录结构如下所示

EL表达式的用法_第1张图片

2.编写index.jsp

<%@page import="com.hy.beans.Department"%>
<%@page import="com.hy.beans.Employee"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


	
	<% 
		Date date = new Date();
		pageContext.setAttribute("time", date+"-");
	 	request.setAttribute("time",date+"--");
	 	session.setAttribute("time",date+"---");
	 	application.setAttribute("time",date+"----");
	 	
	 	//创建employee对象
	 	Employee employee = new Employee(1,"杨绿坤",new Department(1001,"出轨门"));
	 	//将Employee对象放入page域中
	 	pageContext.setAttribute("star", employee);
	%>
	通过JSP表达式输出当前的时间:<%=date %>
通过EL表达式输出当前的时间:${time }
通过EL表达式输出request域中当前的时间:${requestScope.time }
通过EL表达式输出Employee对象的lastName:${pageScope.star.lastName }
通过EL表达式输出Employee对象的getOutName方法的返回值:${pageScope.star.outName }
通过JSP表达式输出项目的虚拟路径:<%=request.getContextPath()%>
通过EL表达式获取项目的虚拟路径:${pageContext.request.contextPath }

 

3.编写bean:Department.java 和 Employee.java

package com.hy.beans;

public class Department {
	private Integer id;
	private String name;
	public Department() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Department(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + "]";
	}
	
}
package com.hy.beans;

public class Employee {
	private Integer id;
	private String lastName;
	private Department dept;
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Employee(Integer id, String lastName, Department dept) {
		super();
		this.id = id;
		this.lastName = lastName;
		this.dept = dept;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public Department getDept() {
		return dept;
	}
	public void setDept(Department dept) {
		this.dept = dept;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", lastName=" + lastName + ", dept=" + dept + "]";
	}
	
	//获取绿了他的名字
	public String getOutName() {
		return "情书女主角";
	}
	

}

特别注意:Employee类中的最后一个getOutName 方法,只要有get方法EL表达式就能读取对应get方法所获得的属性值

你可能感兴趣的:(web,前端)