且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EL表达式

更新时间:2022-08-13 07:56:43

      EL表达式在je22开发中会为程序员节省不少的代码,因为他是jsp代码的简化操作  。  EL的格式 ${要输出的属性} ,

EL表达式可以方便的操作  从另一个网页传递过来的参数 、 javaBean  、 获取属性等等。

下面是一些简单的EL表达式操作:

1、EL表达式操作运算符

${1+2}             在html页面中会输出 3  ,其他的 -  *  /  % 都是一样的操作  。

${true||false}        结果在html页面中操作显示true 

${false&&true}     结果在html页面中显示false   

${!false}               结果显示true

${2>5}                操作关系运算符  结果是false 

${3==3}               结果true 

2、操作参数  EL的隐含对象我们很多时候都能用到。

${param.参数}    就可以输出接收到的参数   这个param是EL表达式隐含的对象 相当于调用调用了   request.getParameter("参数") ;

${paramValues.参数}  和上面的结果是一样的  。

3、操作JavaBean  属性。  JavaBean的属性命名有规范的 。

比如 我们有一个setXxx()方 法 那么就存在一个属性 xxx    。

如果 存在方法setXXX()    那么属性名字就是XXX  。

如果是setxxx    那么属性名字就是  xxx   。 这就是JavaBean属性的命名规范。 

我们知道JavaBean的每种属性都对应这一个内部成员变量  ,但是也有一种情况不对应内部变量 。

比如getArea()  我们只需要返回结果 不如setArea()这时候 这个area属性 就不对应内部的成员变量 ,但是我们依然可以像

操作javaBean属性一样操作,注意这里只能获取这个area属性的值,如果进行了设置操作那么就会抛出 异常 。

如下一个javaBean:

public class Box
{
 private int width ;
 private int height ;
 private int length ;
 public Box()
 {
  this.width= 0 ;
  this.height=0 ;
  this.length=0 ;
 }
 public  int getWidth()
 {
  return  this.width ;
 }
 public int getHeight() {
  return height;
 }
 public void setHeight(int height) {
  this.height = height;
 }
 public int getLength() {
  return length;
 }
 public void setLength(int length) {
  this.length = length;
 }
 public void setWidth(int width) {
  this.width = width;
 }
 
 public int getArea()
 {
  return this.length*this.height*this.width ;
 }
 
}

 

我们在jsp页面中利用EL=表达式进行操作 看下结果 :

<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="mvc.test.Box" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Box  box =new Box() ;
box.setHeight(10);
box.setWidth(10);
box.setLength(10);
 request.setAttribute("box", box);

%>
Box Area=${requestScope.box["area"]}  

BoxArea=${requestScope.box.area}     这里的area虽然在bean内部不存在 但是 我们存在getArea方法 。
</body>
</html>

 

另外对于JavaBean的使用和赋值也可以使用  JSP动作指令来完成  。  

<jsp:useBean id="box" class="mvc.test.Box"></jsp:useBean>
<jsp:setProperty property="height" name="box"  value="10"/>
<jsp:setProperty property="width" name="box"  value="10"/>
<jsp:setProperty property="length" name="box"  value="10"/>

 

4、显示指定范围的属性 。

sessionScope 

applicationScope

requestScope

pageScope 

这些EL隐含对象是用来指定我们所要利用EL表达式输出的属性的值所在的范围 。

例如我们要输出 applicationScope范围的  "username"属性  那么 就利用如下表达式

${application.username}  就可以了 。

当然 如果我们不写范围也是可以的  因为 JSP引擎会自动 按照     pageScope     requestScope   sessionScope   applicationScope的

顺序进行查找 。

 下面是EL表达式  11个隐含对象

EL表达式的隐含对象 Type 具体说明
pageContext javax.servlet.jsp.PageContext The context for the JSP page.
Provides access to various
objects, including servletContext, session, request,
and response.
pageScope java.util.Map Maps page-scoped variable names
to their values.
requestScope java.util.Map Maps request-scoped variable
names to their values.
sessionScope java.util.Map Maps session-scoped variable
names to their values.
applicationScope java.util.Map Maps application-scoped variable
names to their values.
param java.util.Map Maps a request parameter to a
single String parameter value
(obtained by calling
ServletReqwuest.getParameter
(String name)).
paramValues java.util.Map Maps a request parameter name to
an array of String values for
that parameter name (obtained by
calling
ServletRequest.getParameterValues
(String name)).
header java.util.Map Maps a request header name to a
single String header value
(obtained by calling
ServletRequest.getHeader(String
name)).
headerValues java.util.Map Maps a request header name to an
array of String values for that
header (obtained by calling
ServletRequest.getHeaders
(String)).
coookie java.util.Map Maps a cookie name to a single
Cookie object. Cookies are
retrieved according to the
semantics of
HttpServletRequest.getCookies().
If the same name is shared by
multiple cookies, an
implementation must use the first
one encountered in the array of
Cookie objects returned by the
getCookies() method. However, the
ordering of cookies is currently
unsspecified in the Servlet specification.
initParam java.util.Map Maps a context initialization
parameter name to a String
parameter value (obtained by
calling
ServletContext.getInitparameter
(String name)).