表达式语言EL

 

表达式语言

  EL(Expression Language,表达式语言)主要是用在JSP页面中,用来辅助我们产生无脚本的JSP页面,此处的脚本指的是JSP中的Java代码。

  EL的语法是这样的:

${expr}

  如果你想在页面上直接输出上面的内容,需要进行转义,加上反斜杠即可:

\${expr}

  再比如:

${sessionScope.user.sex}

 

  所有EL都是以 ${ 为起始、以} 为结尾的。

 

  Expressions can be used in two ways:

  1.Attribute values in custom and standard actions

  2.Within template text

 

EL可以用来访问Java Bean

  Beans within the namespace available to the JSP can be accessed easily using EL.

  1.Beans can be accessed by way of dot notation:

  ${bean.attribute}

  2.Beans can be located by searching through the scopes:

  page, request, session and application.

  3.Bean scope can be specified by preceding the bean name with the scope.

  ${sessionScope.cust.firstName}

 

实例1

  JSP页面el1.jsp里有一个表单:

  <body>
    <form action="el/el2.jsp">
    username: <input type="text" name="username">
    <input type="submit" value="submit">
    </form>
  </body>

  在页面el2.jsp里面获取这个参数显示:

  之前可以:

  <%= request.getParameter("username") %>

  现在可以:

${ param.username }

  两种效果是一样的。唯一的不同是使用EL打出点之后即便Alt+/也没有任何的输入提示。

 

实例页面

  也可以显示Session中的属性值:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'el1.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <form action="el/el2.jsp">
    username: <input type="text" name="username"><br>   
    interest: <input type="text" name="interest"><br> 
    interest: <input type="text" name="interest"><br> 
    interest: <input type="text" name="interest"><br>
    interest: <input type="text" name="interest"><br>
    interest: <input type="text" name="interest"><br> 
    <input type="submit" value="submit">
    </form>
    
    <% session.setAttribute("hello", "world");%>
  </body>
</html>
el1.jsp

相关文章: