一:JSTL表达式
EL表达式是JSTL1.0为了方便存取数据所定义的语言,到了JSP2.0以后,正式将EL纳入标准规划之一,只要是支持Servlet2.4/JSP的容器中,都可以在JSP网页中直接使用EL表达式,使用JSTL搭配EL可以减少代码量使得页面更加容易维护。
EL提供了多种运算符操作,如算术运算符,关系运算符,逻辑运算符和验证等。
二:算术运算符
算术运算符 | 说明 | 示例 | 结果 |
+ | 加 | ${1+1} | 2 |
- | 减 | ${1-1} | 0 |
* | 乘 | ${1*2} | 2 |
/或div | 除 | ${3/2} | 1.5 |
%或mod | 模 | ${3%2} | 1 |
注意:${1+'a'}会抛异常:java.lang.NumberFormatException:For input string:"a"
${1+'1'}结果为2,会将字符串1转型后再加,如果转型失败会抛异常。
${0/20}结果为0.0,程序在运算时,会自动先将运算的两个数转换成Double类型,然后进行除法操作,所以返回的是0.0而不是0.
${20/0}结果为Infinity,表示无穷大的意思。
${0/0}结果会为NaN,表示不是数字的意思。
三:关系运算符
关系运算符 | 说明 | 示例 | 结果 |
==或eq | 等于 | ${1==1} 或${1 eq 1} | true |
!=或ne | 不等于 | ${1 !=1} 或${1 ne 1} | false |
< 或 lt | 小于 | ${1<2} 或${1 lt 2} | true |
> 或 gt | 大于 | ${1>2} 或${1 gt 2} | false |
<= 或 le | 小于或等于 | ${1<=2} 或${1 le 2} | true |
>= 或 ge | 大于或等于 | ${1>=2} 或${1 ge 2} | false |
注意:${"b">="a"}结果为true,会调用String的compareTo来进行比较,显然b>a.
${"" == null}结果为false。
${null > null}结果为false.
${null == null}结果为true.
四:逻辑运算符
逻辑运算符 | 说明 | 示例 | 结果 |
&& 或 and | 并且 | ${a && b}或 ${a and b} | 两者都为true时为true |
|| 或 or | 或者 | ${a || b}或 ${a or b} | 两者之一为true时为true |
! 或 not | 取反 | ${ !a }或 ${not a} | 为true则false,为false则true |
五:验证运算符
EL验证运算符,对于验证数据特别有用,empty 运算符采用单个表达式作为其变量,并返回一个布尔值,多用于判断
如:判断集合是不是为空,其实用 ==null 也是一样的。empty会帮你判断size=0的情况,建议用empty。
//servlet页面
package com.action;
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;
public class Chapter3Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
List<String> list = new ArrayList<String>();
list.add("张三");
request.setAttribute("list", list);
request.getRequestDispatcher("chapter3.jsp").forward(request, response);
}
}
//jsp页面
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>chapter3.jsp</title>
</head>
<body>
<div>
<div>
${empty list}<br/>
${list == null}
</div>
</div>
</body>
</html>
三元运算符:
${1 > 1 ? "真":"假"} 结果为假
六:与输入有关的隐含对象
param
地址栏输入:http://localhost:8080/jstl/index.jsp?name=xiaowang
jsp页面:${param.name}
可以看到,可以取到传进来的xiaowang这个值,param隐含对象可以取到通过request.getParamter("")的值。
pageContext对象, 取得页面的详细信息。
${pageContext.request.queryString} 取得请求的参数字符串
${pageContext.request.requestURL} 取得请求的URL,但不包括请求之参数字符串
${pageContext.request.contextPath} 服务的web application 的名称
${pageContext.request.method} 取得HTTP 的方法(GET、POST)
${pageContext.request.protocol} 取得使用的协议(HTTP/1.1、HTTP/1.0)
${pageContext.request.remoteUser} 取得用户名称
${pageContext.request.remoteAddr } 取得用户的IP 地址
${pageContext.session.new} 判断session 是否为新的
${pageContext.session.id} 取得session 的ID
${pageContext.servletContext.serverInfo} 取得主机端的服务信息
JSTL表达语言定义了一系列隐含对象。这些隐含对象供JSP获取和使用其scoped attributes
(1)pageContext:指JSP的pageContext对象
(2)pageScope:一个Map对象,包括page范围的属性和值
(3)requestScope:一个Map对象,包括request范围的属性和值
(4)sessionScope:一个Map对象,包括session范围的属性和值
(5)ApplicationScope:一个Map对象,包括application范围的属性和值
(6)param:一个Map对象,包括Web请求参数(request parameter)的字符串值,对应了ServletRequest.getParameter(String)
(7)paramValues:一个Map对象,包括Web请求参数(request parameter)的多个字符串值,对应了ServletRequest.getParameterValues(String)
(8)header:一个Map对象,包括请求的头信息值,对应了ServletRequest.getHeader(String)
(9)headerValues:一个Map对象,包括请求的头信息多个值,对应了ServletRequest.getHeaders(String)
(10)cookie:一个Map对象,包括对应名称的Cookie,对应了HttpServletRequest.getCookie(String)
(11)initParam:一个Map对象,包括一个Web程序的初始化参数值,对应了ServletRequest.getInitParameter(String)
七:
(1).读取Map集合中的值
<%
HashMap hm = new HashMap();
hm.put("username","root");
hm.put("password","admin");
session.setAttribute("hm", hm);
%>
username:${hm.username}
password:${hm.password}
(2).读取数组中的数据
<%
String[] names = {"aaa","bbb","ccc"};
session.setAttribute("names",names);
%>
${names[0]}
${names[1]}
${names[2]}
(3).读取集合中的数据
<%
List<String> names = new ArrayList<String>();
names.add("aaa1");
names.add("bbb1");
names.add("ccc1");
session.setAttribute("names",names);
%>
${names[0]}
${names[1]}
${names[2]}