JSP-EL expression & JSTL tag libraries

Copyright: [Beijing] Java Youth: 456588754 https://blog.csdn.net/Amen_Wu/article/details/53510790

EL expression

存在目的:尽量消除JSP页面中的java脚本
取代java脚本的原因:java脚本会影响jsp页面的阅读

Before: java script with staggered Jsp page elements appear. Corresponds to switch between program ape java language specification and html language specification, affect the development efficiency.

JSP脚本的不足:
1.	代码结构混乱;
2.	脚本与html混合,容易出错;
3.	代码不易于维护。
EL表达式简介:
1.	什么是EL表达式?
Expression Language(表达式语言)
EL定义了一系列的隐含对象和操作符,使开发人员能够很方便的访问页面的上下文,以及不同作用域的对象。
2.	EL表达式的特点
EL得到某个数据时可以自动转换类型;使用简单。
3.	EL表达式的作用
页面结构清晰;代码可读性高、易于维护。

EL expression syntax
to open beginning With Knot bundle {Beginning to end}, {} the EL expression

  1. Operator: obtaining object attributes, $ {news.nTitle}
  2. Operator []: acquiring object properties, n e w s [ n T i t l e ] Obtain take Collection Close in of Correct Like {News [ "nTitle"]}; get the object set, {List [0]}

Relational Operators
Logical Operators
Empty operator

$ {Empty a}, then the variable is not present a true, or false;
n o t e m p t y a {not empty a}或 {! empty a} contrary.

EL implicit object

1.	作用域访问对象:pageScope、requestScope、sessionScope、applicationScope;
2.	参数访问对象:param、paramValues;
3.	JSP隐式对象:pageContext。

JSTL

什么是JSTL?
JSP标准标签库(JavaServerPages Standard Tag Library),通常与EL表达式合作实现JSP页面编码。
优点:1.提供一组标准标签;2.可用于编写各种动态JSP页面(实现JSP页面中的逻辑控制)。

核心标签库:http://java.sun.com/jsp/jstl/core 前缀(prefix):c
国际化/格式化标签库:http://java.sun.com/jsp/jstl/fmt 前缀:fmt
XML标签库:http://java.sun.com/jsp/jst/xml 前缀:x
JSTL标签库环境搭建
环境准备:
1.	获取jstl.jar和standard.jar文件;
2.	将jar文件导入项目;
3.	在JSP页面导入标签命令。
//根据使用的标签库类型,导入相应资源URL,并指定前缀
<%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c”%>

JSTL标准标签库内标签:
核心标签库:
1.	通用标签:set、out、remove;
2.	条件标签:if、choose;
3.	迭代标签:forEach。

将value值存储到scope范围的变量variable中
<c:set var=”index” value=”8” scope=”request”/>
将value值设置到对象属性中
<c:set value=”value” target=”target” property=”property”/>
输出显示out,指定默认值
<c:out value=”value” default=”defaultName”/>

Integrated use:

<%
User user=new User();
Request.setAttribute(“user”,user);
%>
<c:set target=”${user}” property=”name” value=”defaultName”/>
<c:out value=”${user.name}” default=”noUserName”/>

If:实现java语言中if语句的功能
//test=”” 判断条件表达式
//var=”” 该变量用于保存返回的true/false
//scope=”” 该var变量作用域
<c:if test=”codition” var=”name” scope=”applicationArea”>
</c:if>

Choose:实现java语言中if-else if-else语句的功能
<c:choose var=”varName” scope=”scope”>
<c:when test=”condition”>

</c:when>
<c:otherwise>

</c:otherwise>
</c:choose>

forEach:实现对集合中对象的遍历
//items 指定要遍历的集合对象
//var 指定当前成员的引用
//begin 指定从集合的第几位开始
//end 指定迭代到集合的第几位结束
//step 指定循环的步长
//varStatus 用于存放var引用的成员的相关信息,索引等
<c:forEach items=”collection” var=”name” begin=”start” end=”end” step=”count” varStatus=”status”>

</c:forEach>

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/Amen_Wu/article/details/53510790