EL与JSTL笔记小结

来源:互联网 发布:jsonarray清空数据 编辑:程序博客网 时间:2024/04/29 20:56
EL表达式的使用(5个 问题)
JSTL标签的使用(5个问题)
什么是EL,它能做什么用的?
EL全名为Expression Language在JSP页面使用
格式${一个表达式}
例子${requestScope.customer.id}
功能:
1.才四个域对象中取出属性数据显示


(pageContext.request.session.application)


2.取出请求参数数据显示


<%
request.setAttribute("person",new Person("xfzhanag",18));
%>
Map<String,Person> map=new HashMap<String,Person>();
map.put("A",new Person("AA",12));
map.put(""B",new Person("BB",13));
map.B.age${requestScope.B.age};
map.B.age${requestScope.['B'].age};


${requestScope.person.name};
${requestScope.person['name']};
List里面的第二个数据
<%


<%
List list=new ArrayList();
list.add("a");
list.add("b");
list.add("c");
request.setAttribute("List",list);


%>


${requestScope.List[1]};
${requestScope.List['name']};


我的年龄:<requestScope.person.name><br/>
//什么方便?为什么有个中括号?
/*
一种情况?
${requestScope['my person'].name};
map.put("my person",new Person("Xxx",12));


*/
EL能进行的运算?
算数运算(+,-, * ,/ ,%)
关系运算(>,<,==,!=)
逻辑运算(&&,||)
empty运算(判断一个数据是否是空)
(null,空字符串,空集合)


${empty name}<br>
${empty list}<br>
${empty person}<br>


三目条件运算
${requestScope.person.age>18 ? '成年' : '未成年' }
${person.age>18 ? '成年' : '未成年' }
<!--不加隐含对象,怎样找到的呢?
pageScope,requestScope,
sessionScope,applicationScope
从小到大的一个范围!
-->
<%
request.setAttribute("list",new ArrayList());
request.setAttribute("name","");
request.setAttribute("person","new Pseron("Xxx",19));
%>
隐含对象
PageContext javax.servlet.ServletContext
PageScope java.uti.Map
RequestScope java.util.Map
sessionScope java.util.Map
applicationScope java.util.Map
param java.util.Map
EL如何获取对象的内部数据?
获取的方式
通过点(.)来取数据
通过中括号[]来取数据
对象的类型
一般对象
Map对象
数组/List/Set
EL的不足在哪?
不能遍历,逻辑判断差
JSTL(5个问题)
什么是JSTL,它能做什么?
JSTL为jsp stardard tag library在jsp页面使用
功能:实现数据基本输入输出,流程控制,循环,国际化等功能
JSTL  前置名称               URI 范例
核心标签库     c http://sun.com/jsp/jstl/corec:out
I18格式标签库 fmt http://sun.com/jsp/jstl/xml


fmt:formDate
SQL标签库     sql http://sun.com/jsp/jstl/sqlsql:query
xml标签库     xml http://sun.com/jsp/jstl/fmtx:forBach
函数标签库     fn http://sun.com/jsp/jstl/functionfn:split
为什么使用JSTL?
在jsp中使用jsp脚本+jsp表达式也可以做循环输出,太麻烦
EL不能做遍历工作
JSTL能做这些而且与jsp和html的标签可以很好的结合
JSTL快速入门
1.导入JSTL相关的jar包:
jstl.jar
standard.jar


2.在jsp文件中导入JSTL的c标签库
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" perfix="c" 


%>


3
<%
List<String> list=new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
request.setAttribute("list",list);


%>
<c:forEach items="${requestScope.list}" var="item">
${item}<br/>
</c:forEach>


jstl如何做流程控制?
c:if(一重条件判断)
c:choose 
c:when


//需求1:如果我的年龄小于18就输出未成年成(红色字体)
<c:if test="${person.age<18}">
<font color="red">未成年人</font>
</c:if>


需求2:如果年龄大于60,就输出’老头子‘
如果年龄小于18,就输出"小屁孩"
其他,就输出"成年人"
<br/>
<c:choose>
<c:when test="${person.age>60}">老头子


</c:when>
<c:when test="${person.age<18}">小屁孩


</c:when>
<c:otherwise>成年人</c:otherwise>
</c:choose>
jstl如何做遍历?
<%
Map<String,Person> map=new 


HashMap<String,Person>();
map.put("1",new Person("AA",23));
map.put("2",new Person("BB",23));
map.put("3",new Person("CC",23));
map.put("4",new Person("DD",23));
map.put("5",new Person("EE",23));
map.put("6",new Person("FF",23));
request.setAttribute("personMap",map);
%>
<h3>将数据用表格显示出来</h3>
<table border="1" style="width:300px">
<tr>
<td>ID</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<c:forEach items="${personMap}" var="item">
<!--${itm.class}-->
<tr>
<td>${item.key}</td>
<td>${item.value.name}</td>
<td>${item.value.age}</td>
</tr>
</table>
0 0
原创粉丝点击