EL学习笔记

本文介绍了JSPEL的各种运算符,包括算术型、逻辑型、关系型和条件型等,并展示了如何在JSP页面中使用JSPEL来访问JavaBeans、数组、List和Map等类型的对象,还详细说明了JSPEL的内容对象以及如何设置JSP不使用JSPEL。

一、 JSP EL 的运算符

类型

定义

算术型

+ - * / div % mod

逻辑型

and && or || not !

关系型

== eq != ne > gt < lt >= ge <= le

条件型

a?b:c

empty


二、 JSP EL 的基本用法

类型

实例

基本调用方法

JavaBeans

${user.username}

${user["username"]}

${user['username']}

user.getUsername()

数组

${sport[1]}

${sport["1"]}

${sport['1']}

sport[1]

List

${phone[2]}

${phone["2"]}

${phone['2']}

phone.get(2)

Map

${phone.home}

${phone["home"]}

${phone['home']}

phone.get("home")


三、 JSP EL 的内容对象

pageContext  当前页面上下文件对象
pageScope  page
对象
requestScope  request
对象
sessionScope  session
对象
applicationScope  application
对象
param  
得到页面传来的参数
paramValues  
得到页面传来的多个参数,返回一个数组
header  
获取头信息
headerValues  
获取头信息的值
cookie  
获取cookie对象的值
initParam  
获取设定初始的参数值

例:

< %=session.getAttribute( "phone")%>

        
等价于${sessionScope.phone}


四、如何设置 JSP 不使用 JSP EL

1 、当前页面不要用 JSP EL

< %@page isELIgnored ="true" %>

2 、整个 web 应用都不使用 EL ,修改 web.xml 文件

< web-app... >

        < jsp-config >

             < jsp-property-group >

                     < url-pattern > *.jsp</ url-pattern >

                     < el-ignored > true</ el-ignored >

             </ jsp-property-group >

        </ jsp-config >

</ web-app... >


五、实例

1 、基本运算符的实例

< %@ page language ="java" pageEncoding ="UTF-8" %>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < title > My JSP 'elExample1.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 >
    < !--
以下为JSP EL的算术运算实例 -- >
    ${10+10 }< br >
    ${10-10 }< br >
    ${10*10 }< br >
    ${10/10 }< br >
    ${10 div 10 }< br >
    ${10%10 }< br >
    ${10 mod 10 }< br >
    < !--
以下为想输入原样的表达式,需要用/或者'进行转义 -- >
    /${10+10 }< br >
    '$'{10+10 }< br >
    
    < !--
以下为JSP EL的关系运算实例 -- >
    ${100>200 }< br >
    ${100 gt 200 }< br >
    ${100< 200 }<br>
    ${100 lt 200 }< br >
    ${100>=200 }< br >
    ${100 ge 200 }< br >
    ${100< =200 }<br>
    ${100 le 200 }< br >
    ${100==200 }< br >
    ${100 eq 200 }< br >
    ${100 !=200 }< br >
    ${100 ne 200 }< br >
    < !--
以下为比较字符,字符用单引号,字符串用双引号引起 -- >
    ${'e' eq 'h' }< br >
    ${"hit" > "him" }< br >
    
    < !--
以下为逻辑运算符的实例 -- >
    ${(10>2) && (34>25) }< br >
    ${(10>2) and (34>25) }< br >
    ${(10>2) || (34>25) }< br >
    ${(10>2) or (34>25) }< br >
    ${!(10>2)}< br >
    ${not(10>2)}< br >
    
    < !-- empty
运算符的应用 empty判断时,若对象为""或是null,则都为true-- >
    <%
      pageContext.setAttribute("username",null);
      pageContext.setAttribute("password","");
      pageContext.setAttribute("city","
北京");
      pageContext.setAttribute("date",new java.util.Date());
     %>
     < !--
判断username变量是否为空,以下返回true-- >
     ${empty username }< br >
     < !--
判断password变量是否为空,以下返回true -- >
     ${empty password }< br >
     < !--
判断city变量是否为空,以下返回false-- >
     ${empty city }< br >
     < !--
判断date变量是否为空,以下返回false -- >
     ${empty date }< br >
    
  </ body >
</ html >

2 、用 JSP EL 读取 JavaBean 中的值

< %@ page language ="java" pageEncoding ="UTF-8" %>
< %@ page import ="java.util.*,com.meixin.beans.*" %>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < title > My JSP 'elExample1.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 >
  < !--
使用User Bean,设置属性值username -- >
  < jsp:useBean id ="user" class ="com.meixin.beans.User" > </ jsp:useBean >
  < jsp:setProperty name ="user" property ="username" value ="meixin" />
  <%
    //
建立Profile对象,设置邮件地址
    Profile p = new Profile();
    p.setEmail("wnight88@sina.com");
    
    //
将不同的电话存入Map中,并设置在p对象的属性中
    Map< String,String > phone = new HashMap< String,String > ();
    phone.put("office","8383838");
    p.setPhone(phone);
    
    //
建立地址对象,设置城市名
    Address address = new Address();
    address.setCity("
北京");
    Address[] addresses = {address};
    p.setAddress(addresses);
    user.setProfile(p);
    %>
    < !--
JSP EL的级连方式输入值 -- >
    < !--
输出user对象中的username属性值,三种写法等价 -- >
    ${user.username }< br >
    ${user["username"] }< br >
    ${user['username'] }< br >
    < !--
输出user对象中profile属性对象中的phone属性Map中键值为office的值 -- >
    ${user.profile.phone.office }< br >
    ${user['rofile']['phone']['office'] }< br >
    < !--
输出user对象中profile属性对象中address数据属性中第0个元素对象中的city的属性值 -- >
    ${user.profile.address[0].city }< br >
  </ body >
</ html >

以下为对象的JavaBean的内容

1) Profile

package com.meixin.beans;
import java.util.Date;
import java.util.Map;
public class Profile
{
  private String email;
  private Date birthday;
  private Address[] address;
  private Map<String, String> phone;
  public String getEmail()
  {
    return email;
  }
  public void setEmail(String email)
  {
    this .email = email;
  }
  public Date getBirthday()
  {
    return birthday;
  }
  public void setBirthday(Date birthday)
  {
    this .birthday = birthday;
  }
  public Address[] getAddress()
  {
    return address;
  }
  public void setAddress(Address[] address)
  {
    this .address = address;
  }
  public Map<String, String> getPhone()
  {
    return phone;
  }
  public void setPhone(Map<String, String> phone)
  {
    this .phone = phone;
  }
}

 

2)User

package com.meixin.beans;
public class User
{
  private Long userID;
  private String userName;
  private String password;
  private Profile profile;
    
  public Long getUserID()
  {
    return userID;
  }
  public void setUserID(Long userID)
  {
    this .userID = userID;
  }
  public String getUserName()
  {
    return userName;
  }
  public void setUserName(String userName)
  {
    this .userName = userName;
  }
  public String getPassword()
  {
    return password;
  }
  public void setPassword(String password)
  {
    this .password = password;
  }
  public Profile getProfile()
  {
    return profile;
  }
  public void setProfile(Profile profile)
  {
    this .profile = profile;
  }
    
}

3)Address

package com.meixin.beans;
public class Address
{
  private String city;
  public String getCity()
  {
    return city;
  }
  public void setCity(String city)
  {
    this .city = city;
  }    
}

3 、实例:输出页面不同范围内属性的值

 

< %@ page language ="java" pageEncoding ="UTF-8" %>

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >

    < title > My JSP 'elExample1.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 >
    <%
      pageContext.setAttribute("username","meixin");
      request.setAttribute("username","meixinRequest");
      session.setAttribute("username","meixinSession");
      application.setAttribute("username","meixinApplication");
     %>
     < !--
输出meixin -- >
     ${pageScope.username }< br >
     ${pageScope['username'] }< br >
     < !--
输出值为meixinSession -- >
     ${sessionScope.username }< br >
     < !--
输出值为meixinRequest -- >
     ${requestScope.username }< br >
     < !--
输出值为meixinApplication -- >
     ${applicationScope.username }< br >
     < !--
输出值为meixin,此变量系统根据pageContext,request,session,application依次查找 -- >
     ${username }< br >
    
  </ body >
</ html >


4
、实例:param 用于获取上一页面传递的参数值

< !-- param用于获取上一页面传递来的参数值-- >
${param.username}< br >
${param.password}< br >

 

5 、实例: cookie 用于获取 cookie 参数的值

<% 
      response.addCookie(new Cookie("username","meixin"));
%>
< !--
输出cookieuser的值,此处输出meixin -- >
${cookie.user.value }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值