表达式语言(EL)可以方便的访问标志位(JSP中有四种标志位 :page(pageContext), request, session, application)中的属性,可以避免许多scriptlet代码。
在JSP中,只有三种代码可以出现:
- 接收属性
- 判断语句
- 迭代输出
但是开发中这点很难做到,因为JSP的输出要接收VO对象,所以,要通过表达式来避免导入vo包
表达式语言
四种标识为就是四种属性范围,可以直接通过表达式语言访问:
语法格式:
${属性名称}
表达式语言的主要功能就是进行内容的显示,通过不同内置对象的设置,表达式语言可以输出不同的内容:
内置对象的定义:
本图来自《Java Web 开发实战经典 --基础篇》视频
如果在不同的属性范围中保存了同名属性,则EL的查找范围就是 page --> request ---> session--->application,这是就可以通过内置对象区分四种属性
代码如下:ELdemo.jsp
<%@ page contentType = "text/html" pageEncoding="GBK"%>
<html>
<head>
<title>My JSP 'ELdemo.jsp' starting page</title>
</head>
<body>
<%
pageContext.setAttribute("info", "page属性");
request.setAttribute("info", "request属性") ;
session.setAttribute("info", "session属性");
application.setAttribute("info", "application属性");
%>
<h3>${pageScope.info}</h3>
<h3>${requestScope.info}</h3>
<h3>${sessionScope.info }</h3>
<h3>${applicationScope.info }</h3>
</body>
</html>
显示结果:
接收请求参数:
这个功能与request.getParameter()类似,语法如下:
${param.参数名称}
<h3><%=request.getParameter("ref") %></h3>
<h3>${param.ref }</h3>
也可以通过 ${paramValues.参数名称}
接收一组参数
集合操作
集合有 Collection(List, Set), Map, Iterator
所有的集合都要通过Iterator方法进行输出,而且List和Set的本质区别在于List接口对Collection接口进行扩充,而Set接口并没有扩充
Map每次保存一对内容,而且都是用Map.Entry接口对象
在MVC中应用:
在:org.thystar.eldemo.vo包中写入Dept.java
package org.thystar.eldemo.vo;
public class Dept {
private int deptno;
private String dname;
private String loc;
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
在:org.thystar.eldemo.servlet包中写入ELServlet.java
package org.thystar.eldemo.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.thystar.eldemo.vo.Dept;
public class ELServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Dept> all = new ArrayList<Dept>();
Dept dept = null;
dept = new Dept();
dept.setDeptno(1);
dept.setDname("xxxx");
dept.setLoc("yyyy");
all.add(dept);
dept = new Dept();
dept.setDeptno(2);
dept.setDname("xxxxxx");
dept.setLoc("yyyyyyyy");
all.add(dept);
request.setAttribute("alldept", all);
request.getRequestDispatcher("ELdemo.jsp").forward(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
在:ELdemo.jsp中写入
<%@ page contentType = "text/html" pageEncoding="GBK" import = "java.util.*"%>
<html>
<head>
<title>My JSP 'ELdemo.jsp' starting page</title>
</head>
<body>
<%
List all = (List)request.getAttribute("alldept");
if(all != null){
%>
<table border = "1" width = "90%">
<tr>
<td>部门编号</td>
<td>部门名称</td>
<td>部门位置</td>
</tr>
<%
Iterator iter = all.iterator();
while(iter.hasNext()){
pageContext.setAttribute("dept", iter.next());
%>
<tr>
<td>${dept.deptno }</td>
<td>${dept.dname }</td>
<td>${dept.loc }</td>
</tr>
<%
}
%>
</table>
<%
}
%>
</body>
</html>
结果如下:
运算符:
简单的运算符可以用表达式完成,复杂的应该交给Servlet完成
主要用到的是三目运算符:
${ ? : }