EL&JSTL表达式学习笔记 -- day01

来源:互联网 发布:js比较字符串大小 编辑:程序博客网 时间:2024/06/13 13:52

一、EL表达式

1、el 表达式语法

EL表达式的语法非常简单,都是以“${”符号开始,以“}”符号结束的,具体格式如:${表达式}

2、el 表达式的内置对象


3、el 表达式获取四个域对象中的值

(1)、存值

<%   //向JSP中的4个域对象赋予值pageContext.setAttribute("name", "pname");request.setAttribute("name", "rname");session.setAttribute("name", "sname");application.setAttribute("name", "aname");%>

(2)、取值

<h1>JSP传统脚本段的方式获取数据</h1><%=pageContext.getAttribute("name") %><br/><%=request.getAttribute("name") %><br/><%=session.getAttribute("name") %><br/><%=application.getAttribute("name") %><br/><%=application.getAttribute("myname") %><br/><h1>EL表达式获取4个域对象中的数据</h1>${pageScope.name}<br/>${requestScope.name}<br/>${sessionScope.name}<br/>${applicationScope.name}<br/>${name}<br/>${myname}<br/>

4、el 表达式获取表达或者url中参数,并回显

(1)、获取参数

<h1>通过EL获取到表单参数</h1>${param.username}<br/>${param.hobby}<br/>${paramValues.hobby}<br/>${paramValues.hobby[1]}<br/>

(2)、回显数值

<form action="/day40/LoginServlet" method="post">user:<input type="text" name="username" value="${param.username}"/><br/>pass:<input type="password" name="password"  value="${param.password}"/><br/><button>提交</button></form>

5、el 表达式获取请求头中的数据 -- 了解内容

${header.accept} <br/>${header.accept-Encoding} <br/> <%--非法的,有异常,“-”被解析成减号。使用"/"进行单个el表达式转义 --%>${header['accept-Encoding']} <br/>${headerValues['accept-Encoding'][0]} <br/>

6、el 表达式获取项目路径 -- 重点

<img  src="${pageContext.request.contextPath}/01_EL/a/b/images/11.bmp"/><br/><a href="${pageContext.request.contextPath}/a/b/b/login.jsp">登录页面</a>

7、el 表达式获取web.xml中初始化配置参数

${initParam.username }<br/>${initParam.password }<br/>

8、el 表达式获取cookie中的数据

(1)、获取cookie中数据

<!--   Cookie[] cks=new Cookie[3];  cks[0]<====>cookie对象  cks[1]<====>cookie对象 -->${cookie}<!-- 获取到所有的cookie --><br/>${cookie.name01}<br/>${cookie.name01.name}<br/>${cookie.name01.value}<br/>

(2)、记住用户名

<form action="/day40/LoginServlet02" method="post">User:<input type="text" name="username"  value="${cookie.username.value}"/><br/><input type="checkbox" name="remUser" value="true"/>记住用户名<br/><button>登录</button>

9、el 表达式获取集合、对象中数据

<title>el获取各种数据_字符串_数组_集合_MAP_普通JAVAbean对象</title></head><body><h1>PS:el获取的是指定范围内的数据(4个域对象,表单参数,cookie,请求头)</h1><hr/><h3>el获取字符串</h3><%String str02="my name is mary!";    pageContext.setAttribute("myStr02", str02);%>${myStr02}<hr/><h3>el获取java对象中的数据</h3><%User uu=new User("lucy","55555");    pageContext.setAttribute("myUser", uu);%><!--当JSP引擎 识别到  ${myUser} ,底层从4个域对象范围寻找属性名为myUser对应值(对象引用),当获取到这个对象时 ${myUser.username} 调用对象上的getUsername()方法. -->${myUser.username}${myUser.password}<hr/><h3>el获取数组中的数据</h3><%String[] arr={"aaaa","bbbb","cccc"};    request.setAttribute("myArr", arr);%>${myArr}<br/>${myArr[2]}<br/><hr/><h3>el获取集合中的数据</h3><%List<String> list=new ArrayList();    list.add("111111");    list.add("222222");    list.add("333333");    request.setAttribute("myList", list);%>${myList}<br/>${myList[2]}<br/><hr/><h3>el获取MAP中的数据</h3><%Map<String,User> map=new HashMap<String,User>();    map.put("uu01", new User("张三","2121"));    map.put("uu02", new User("土豪","2122"));    map.put("uu03", new User("李四","2123"));    request.setAttribute("myMap", map);%>${myMap} <= = => pageContext.findAttribute("myMap");     <br/>${myMap.uu01}<br/>${myMap.uu01.username}<br/>${myMap.uu01.password}<br/><hr/></body>

运算结果:


10、el 表达式的运算符

<title>EL对运算符支持</title></head><body>PS:EL表达式: 用运算符将常亮,变量,表达式连接起来的符合EL语法规则的式子<br/>三大运算符: 算术,关系,逻辑,三元,非空<br/><%User user=new User();       pageContext.setAttribute("myUser", user);        pageContext.setAttribute("n1", "10");pageContext.setAttribute("n2", "20");pageContext.setAttribute("n3", "30");pageContext.setAttribute("n4", "40");    %><h1>el 算术运算符</h1>${ n1 + n2 + n3 }<br/>${1+1} <br/>${"1"+1} <br/> <%--将字符串转换成数字,然后进行计算 --%>${'1'+1} <br/> <%--el没有字符 --%>\${'a'+1} <br/> <%-- 将a转换数字,异常 --%>${a + 1} <br/> <%--a将从作用域获得数据,如果没有 --%><h1>el 关系运算符</h1>${ n1 < n2 } - ${ n1 lt n2 } <!-- less than --><br/>${ n1 > n2 } - ${ n1 gt n2 } <!-- great than --><br/>${ n1 <= n2 } - ${ n1 le n2 } <!-- less equal --><br/>${ n1 >= n2 } - ${ n1 ge n2 } <!-- great equal --><br/>${ n1 == n2 } - ${ n1 eq n2 } <!-- equal --><br/><h1>el 逻辑运算符</h1>${ n1<n2 && n3<n4 } - ${ n1<n2 and n3 < n4 }<br/>${ n1<n2 || n3 < n4 } - ${ n1<n2 or n3 < n4 }<br/>${ !(n1 < n2) } - ${ not(n1<n2) }<h1>el 三元运算符</h1>${n1>n2?"AAAA":"BBBB"}<h1>el 非空运算符</h1>${empty  myUser}<br/>${not empty  myUser}<br/></body>
运算结果:

二、JSTL表达式

1、什么是JSTL

标准标签库(JavaServer Pages Standard Tag Library),简称JSTL。

2、JSTL标签库

三、JSTL的使用

1、导入jar包


2、引入标签

<%@ taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   

3、if 标签的使用

<h1>测试if标签</h1><c:if test="${3>2}">  AAAAAAAAAAAAAA</c:if><c:if test="${3<=2}">  BBBBBBBBBB</c:if>

4、forEach 标签的使用

var:属性用于指将当前迭代到的元素保存到page域中的名称; 

items:属性用于指定将要迭代的集合对象;

varStatus:用于指定当前迭代状态信息的对象保存到page域中的名称;

begin:属性用于指定从集合中第几个元素开始进行迭代,begin的索引值从0开始,如果没有指定items属性,就从begin指定的值开始迭代,直到迭代结束为止;

step:属性用于指定迭代的步长,即迭代因子的增量。

(1)、方式1

<h1>测试for each标签_方式1</h1><!--  以下代码:代替了JSP脚本段 for循环   含义: 从1循环到50,将每次的值放入i中,步长为2 , ${i}将i中的值,依次输出     varStatus="status" :代表循环过程中的一些中间状态值    status.index: 正在输出数字下标   status.count: 当前循环的次数   status.first: 当前正在输出元素是否是第1个元素   status.last: 当前正在输出元素是否是最后一个元素 --><c:forEach begin="1" end="50" var="i" step="2" varStatus="status">  ${i} <==> ${status.index} <===> ${status.count}<===> ${status.first}<===> ${status.last}<br/></c:forEach>

(2)、方式2

<h1>测试for each标签_方式2(频繁使用)</h1><!--    items="${allUsers}": 说明当前我们要遍历数据范围    var="uu"  将正在遍历的数据依次赋值给变量uu --><c:forEach items="${allUsers}" var="uu" varStatus="status">   ${status.count}<===>${uu.username}<===>${uu.password }<br/></c:forEach>

5、遍历字符串数组和map集合中的数据

<h1>测试for each标签_方式2(频繁使用)</h1><!--   测试JSTL遍历字符串数组_MAP中的数据 --> <% String[] arr={"1111","22222","33333"};    pageContext.setAttribute("myArr", arr);        Map<String,User> map=new HashMap<String,User>();    map.put("uu01", new User("tom1","1234"));    map.put("uu02", new User("tom2","1235"));    map.put("uu03", new User("tom3","1236"));    map.put("uu04", new User("tom4","1237"));        pageContext.setAttribute("myMap", map);         %> <c:forEach items="${myArr}" var="str" varStatus="status">  ${status.count} <___> ${str} <br/></c:forEach><hr/><c:forEach items="${myMap}" var="entry" varStatus="status">${status.count }<___> ${entry.key}<___> ${entry.value.username}<___>${entry.value.password} <br/></c:forEach>

原创粉丝点击