TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
JSP EL
部分
一、
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"));
%>
<!-- 输出cookie中user的值,此处输出meixin -->
${cookie.user.value }
6、实例:initParam用于获取web.xml中初始的参数值
1)web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaEE"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 这里context-param标记中设置初始参数repeat的值为100 -->
<context-param>
<param-name>repeat</param-name>
<param-value>100</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2)JSP EL代码
${initParam.repeat} |
|