Day20-jsp & el & jstl
2017-07-07 20:45
721 查看
EL表达式
${ EL表达式 }EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL 出现的目的是要替代jsp页面中脚本的编写。
Expression language表达式语言,借鉴了js、jquery的语法
作用:
1)简化jsp中的代码,主要是用来替换jsp脚本<%= %>
2)从域对象中获取数据;
3)依赖于pageContext.findAttribute()
4)执行运算
功能:
1)获取域中的对象
2)执行运算
3)获取常见的web对象
功能一:EL表达式执行运算
(一)执行运算 + - * / %
${ 4+6 }
需要注意的是:
${ 4+”7” }
结果为11
注意:+ - 的时候,必须是一个数字或者数字样子的字符串;
(二)逻辑运算符、比较运算符
2.1)逻辑运算符:&& || ! (and or not)
${ 3>4 && 4<5 } 或 ${ 3>4 and 4<5 } ${ ! (3>5)}
2.2)比较运算符: > >= == < <= !=
${ 4==4 }
也可以做字符比较:
<% pageContext.setAttribute("city", "上海"); %> ${ city=="上海" }
2.3)empty:判断是否为空;
1)判断内容是否为“”空串
2)判断对象是否为null
3)判断集合的长度是否为0
${ empty aaa} ${ empty city } <% List<String> list = new ArrayList<String>(); list.add("value1"); list.add("value2"); pageContext.setAttribute("list", list); //创建完list之后一定要添加进入pageContent,不然等同于没有创建 %> ${empty list } <br/> ${empty list2 }
2.4)三元运算符
${ 5>3 ? 5 : 3 }
功能二:EL表达式获取域中的数据
如果找到数据则返回,否则就返回“”
2.1)快捷方式获取数据
依次从pageScope、requestScope、sessionScope、applicationScope中查找指定的属性
若找到,就立即返回,不再向后继续查找
若找不到,就返回“”
若属性中出现“.”“+”“-”等特殊的符号,快捷方式获取不了的时候,得用 ${xxxScpoe["属性名"]}
获取简单的数据
${pageScope|requestScope|sessionScope|applicationScope.属性名}
获取复杂的数据
获取域中的数组中的值 ${数组属性名[index]}
获取域中list中的值 ${list属性名[index]}
获取域中map中的值 ${map属性名.键}
获取javabean中的数据 ${javabean的属性名.bean属性}
获取数组中数据:
<% String[] arr = new String[3]; arr[0] = "aaa"; arr[1] = "bbb"; arr[2] = "ccc"; pageContext.setAttribute("arr",arr); %> <%= ((String[])pageContext.getAttribute("arr"))[1] %> <br/> ${ arr[1] }
* 获取list集合中的数据
<% List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); pageContext.setAttribute("list", list); %> <%=((List<String>)pageContext.getAttribute("list")).get(1) %> <br/> ${ list[1] }
* 获取map集合中的数据
<% Map<String,String> map = new HashMap<String,String>(); map.put("akey","avalue"); map.put("bkey","bvalue"); map.put("ckey","cvalue"); pageContext.setAttribute("map", map); %> <%=((Map<String,String>)pageContext.getAttribute("map")).get("ckey") %> <br/> ${ map.ckey }
* 特殊的map写法:
<% Map<String,String> map = new HashMap<String,String>(); map.put("akey","avalue"); map.put("bkey","bvalue"); map.put("ckey","cvalue"); map.put("s.key","svalue"); pageContext.setAttribute("map", map); %> <%=((Map<String,String>)pageContext.getAttribute("map")).get("ckey") %> <br/> ${ map["s.key"]}
* 从对象中获取数据
<% User user = new User(); user.setUsername("zhangsan"); user.setPassword("123"); pageContext.setAttribute("user",user); %> <%=((User)pageContext.getAttribute("user")).getUsername() %><br/> ${user.username }
内省原理,和属性对应而不是与字段对应。
2.2)从指定范围获取数据
只写pageScope,就取出域中所有的属性,键值对形式;
${pageScope } ${requestScope } ${sessionScope } ${applicationScope }
包含特殊字符的时候:
EL的内置对象
11大内置对象- 域对象:
pageScope | requestScope | sessionScope | applicationScope
- 请求参数相关:
- param(理解)
- paramValues(了解)
- 请求头相关的(了解)
- header
- headerValues
全局初始化相关的(了解)
- initParam
cookie*
获取的是一个map集合: map< cookie的名称,Cookie对象 >
获取值 ${cookie.lastTime.value}
pageContext*
- pageContext.request.contextPath:在jsp页面中动态的获取项目的路径{pageContext.request.contextPath}
EL表达式代码参考:
<% pageContext.setAttribute("pkey", "pvalue"); request.setAttribute("rkey", "rvalue"); session.setAttribute("skey", "svalue"); application.setAttribute("akey", "avalue"); %>
老方式: <%=pageContext.getAttribute("pkey")%><br> EL方式:${pageScope.pkey } <hr> 老方式: <%=request.getAttribute("rkey")%><br> EL方式:${requestScope.rkey } <hr> 老方式: <%=session.getAttribute("skey")%><br> EL方式:${sessionScope.skey } <hr> 老方式: <%=application.getAttribute("akey")%><br> EL方式:${appl e50d icationScope.akey } <hr> 快捷的方式: ${ pkey } ,${ rkey },${ skey },${ akey } <h3>请求参数</h3> ${param } ${param.name } ${param.hobby } <br> ${paramValues } ${paramValues.name[0] } ${paramValues.hobby[1] } ${fn:length(paramValues.hobby) } <h3>请求头</h3> ${header }<br> ${header.cookie }<br> ${headerValues } <h3>全局初始化参数</h3> ${initParam.root } <h3>获取cookie相关的内容</h3> ${cookie }<br> ${cookie.JSESSIONID.value } <h3>获取其它的对象</h3> ${pageContext.request.contextPath }
jstl : jsp标准标签库
JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能。jstl出现的目的同el一样也是要代替jsp页面中的脚本代码。JSTL标准标准标签库有5个子库,但随着发展,目前常使用的是他的核心库使用步骤:
1)使用前需要导入:两个jar包
2)使用jsp的指令标签taglib指令导入core标签库:代码如下::
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
JSTL的5个字库:
core核心库:c
I18N国际化: fmt (几乎不用)
sql:SQL相关的 sql (过时了)
xml:xml相关的 x (过时了)
functions:通用函数 fn (很少使用)
Core标签库:if标签
该标签专门用于完成jsp页面中的条件判断
test属性用于设置逻辑表达式;
var属性用于指定逻辑表达式中变量的名字
scope属性用于指定var变量的作用范围,默认是page
<% pageContext.setAttribute("city", "shanghai"); %> <c:if test="${city=='shanghai'}"> 条件成立,输出上海 </c:if>
core标签库中没有else标签的。
Core标签库:choose标签
类似于switch语句
<% pageContext.setAttribute("city", "shanghai"); %> <c:choose> <c:when test="${city=='shanghai' }">上海 </c:when> <c:when test="${city=='shenzhen' }">深圳 </c:when> <c:otherwise>北京欢迎您 </c:otherwise> </c:choose>
Core标签库:forEach标签
该标签专门用于迭代集合对象中的元素,如set、list、map、数组等等。并能够重复执行标签体中的内容;
var属性用于指当前迭代到的元素保存到page域中的名称
items属性用于指定将要迭代的对象集合
varStatus:循环状态,例如当前循环了第几次;用于指定当前迭代状态信息的对象保存到page域中的名称
begin属性用于指定从集合中的第几个元素开始迭代;索引值从0开始,
end属性,到几结束
step属性用于指定迭代的补偿,就是迭代因子的增量;
<c:forEach var="i" begin="1" end="20" step="2" varStatus="status"> 这是第${ status.count}次循环,遍历出来的数据是${i };<br/> </c:forEach> forEach遍历list集合 <% List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); pageContext.setAttribute("list", list); %> <c:forEach var="i" items="${list }"> ${i } </c:forEach> forEach遍历map集合 <% Map<String,String> map = new HashMap<String,String>(); map.put("aaa","a"); map.put("bbb","b"); map.put("ccc","c"); pageContext.setAttribute("map", map); %> <c:forEach var="j" items="${map }"> ${j } </c:forEach> <c:forEach var="k" items="${map }"> ${k.key }:${k.value } </c:forEach>
案例:用jstl写一下展示商品信息案例:
<!-- 用jstl写一下 --> <table border="1px" align="center" style="background-color:green" > <tr> <th>商品id</th> <th>商品名称</th> <th>商品价格</th> <th>商品描述</th> </tr> <c:forEach var="list" items="${products }"> <tr> <td>${list.getId() }</td> <td>${list.getPname() }</td> <td>${list.getPrice() }</td> <td>${list.getPdesc() }</td> </tr> </c:forEach> </table>
或者:
<!-- 用jstl写一下 --> <table border="1px" align="center" style="background-color:green" > <tr> <th>商品id</th> <th>商品名称</th> <th>商品价格</th> <th>商品描述</th> </tr> <c:forEach var="list" items="${products }"> <tr> <td>${list.id }</td> <td>${list.pname }</td> <td>${list.price }</td> <td>${list.pdesc }</td> </tr> </c:forEach> </table>
MVC设计模式
Model-View-Controller简写MVC是软件工程中的一种软件架构模式,它是一种分离业务逻辑与显示界面的设计方法,它把软件系统分成三个基本部分:模型(model)、视图(view)、控制器(controller)
控制器controller:对请求进行处理,负责请求转发
视图view:界面设计人员进行图形界面设计;
模型model:编写程序应用的功能,实现算法等等、数据库管理
相关文章推荐
- SERVLETJSP Unit05:转发 、 JSP开发常见问题 & Unit09: EL、JSTL
- 1.3--Jsp学习之EL&JSTL
- jsp&el&jstl
- jsp & el & jstl
- JSP模式&EL&JSTL(2)
- jsp & el & jstl
- jsp & el & jstl
- day12-jsp&el&jstl
- JSP模式&EL&JSTL(3)
- jsp & el & jstl
- jsp & el & jstl
- jsp & el & jstl
- day_1_13 JavaWeb系列学习总结之jsp&el&jstl
- JSP模式&EL&JSTL(1)
- jsp^jstl&el
- JSP JSTL EL 删除空行 <%@ page trimDirectiveWhitespaces="true" %>
- jsp笔记整理(servlet&jstl&el&jsp标准动作)
- Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
- JSP标签 el jstl的初步
- Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"