EL using the JSP tags and JSTL

Preparation before using JSTL

Want to use JSTL, you first need to import JSTL packages (JSTL.jar and standard.jar) to the project.

JSTL Introduction

JSP Standard Tag Library (the JSTL) is a label set JSP, it encapsulates the common core application functionality JSP.

JSTL support generic, structured tasks such as iteration, conditional, XML document manipulation, internationalization tags, SQL tags. In addition to these, it also provides a framework for integrated use JSTL custom tags.

JSTL installation

Import jar package

Apache Tomcat installation JSTL library steps:

Official Download:  http://tomcat.apache.org/taglibs/standard/

Download  jakarta-taglibs-standard-1.1.2.zip  and unzip the  jakarta-taglibs-standard-1.1.2 / lib /  two jar files: the standard.jar  and  jstl.jar  copy files to  / WEB-INF / lib /  down.

JSTL tag libraries

Divided into the following five tags in JSTL

  1. Core tags
  2. Formatting tags
  3. SQL tag
  4. XML tags
  5. JSTL function

When using different labels you need to introduce different libraries in JSP

Core label (Core):

The introduction of tag libraries

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

Commonly used tags

Applications

  <c:set>

<C: set> setAttribute JSP equivalent of the session () method

Instructions:

1. <c: set var = "name" value = "value" />

2. <c: set var = "name"> value </ c: set>

<c:out>

<C: out> JSP equivalent of <% =%>

Instructions:

<C: out value = "required output value">

Here EL expression can be output in the session Attrbute

Example:

<c: set var = "test " value = " This is a test"> 

<C: OUT value = "$ {Test}">

 Output: This is a test

 

<c:remove>

<C: remove> and c: set opposite label, c: set tag is disposed setAttrbute 

This tag is deleted Attrbute value equivalent to the session set in removeAttrbute () method

Instructions:

 <C: remove var = "name" />

 

<c:if>

<C: if> if statement is equivalent to the java

Instructions:

<C: IF Test = "Condition"> JSP execution condition code meets </ c: if>

<c:catch>

<C: catch> similar to the try java

Instructions:

<C: the catch  var = "name"> JSP Code </ c: catch>

If during execution, JSP code error occurs, it will copy to the name, so we can be NULL if carried out by judges to determine whether there is abnormal name

Example:

<C: the catch  var = "testException"> 

<% int I = 5/0;%> 

</ C: the catch> 

<C: IF Test = "$ {testException =! null }"> 
    above code error occurs

  < / c: if>

Output Results: The above code error has occurred

If the above 0 to 3 without any output

<c:choose> 和 <c:when>

<C: choose> and <c: when> similar to the switch and case java

Usage: (c: choose may have a plurality of c: when tag)

<c:choose>

<c:when test=”条件”>处理</c:when>

<c:when test=”条件”>处理</c:when>

</c:choose>

Example:

<c:set var=”test” value=”10”/>

<c:choose>

<c:when test=”${test > 5}”>test 大于5</c:when>

<c:when test=”${test < 5}”>test小于5</c:when>

</c:choose>

Output Results: test greater than 5

<c:otherwise>

<C: otherwise> java switch is equivalent to the default

Instructions

<c:choose>

<c:otherwise>处理</c:otherwise>

</c:choose>

When you choose the conditions are not met when the content is executed in otherwise

Note: choose when and when and otherwise is a set of written and otherwise must choose the

<c:import>

Instructions:

<c:import var=”date” url=”http://www.baidu.com” />

The above wording will return through source code Baidu to date variable, a

<c:out value=”${date}”>

Export

<c:forEach> 和 <c:forTokens>

JAVA FOR loop is similar to the cycle and FOREACH

Instructions:

<C: forEach var = "name (i)" begin = "start numbers (1)" end = "End Digital (. 5)"> 

$ {I}

 </ C: forEach>

The output is 12345

Instructions:

<c:forTokens items=”a,b,c,d,e” delims=”,” var=”name”>

${name}

</c:forTokens>

The output abcde

forEach similar to JAVA is provided in a loop for the start end of a set number begin and end each loop digital values ​​are assigned to more setp var Another parameter which can be set several values ​​mean the number of increments

forTokens similar JAVA foreach in a group and placed in what settings delims then dividing the post-division cycles if var is assigned to items stored in a set so you can not write delims each cycle will remove the items in the collection var placed in one

<c:redirect>

Similar service redirection

Instructions:

<c:redirect url=”http://www.baidu.com”/>

To redirect to Baidu

Another <c: url> and <c: param> two labels

 There are labels

<%--JSTL 核心标签--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--JSTL 格式化标签--%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--JSTL SQL标签--%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%--JSTL XMLb标签--%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%--JSTL 函数标签--%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

You can view specific usage other JSTL tutorial rookie Address : https://www.runoob.com/jsp/jsp-jstl.html

 

EL expressions introduction: 

EL rookie can view the detailed usage tutorial Address: https://www.runoob.com/jsp/jsp-expression-language.html

EL expressions define rules: start with $ contents written in the Example {}: $ {test}

Of course, because all the code EL expressions are written in {}, so we can write operation on the inside

such as:

${test+100}

The results of this test is the final output of the variable value obtained by adding the number 100

Another example

${test != null} 

The final result is a true output in this way we can be a good and used in conjunction with JSTL.

Into the EL operators. And [] two kinds of

If you want to use dynamic values ​​[] such as:

${session.user[date]}

Which is a date variable

The above code means is removed session.user the array element inside the first date

It should be noted, EL expressions limit our call JAVA method, if you want to call, you can customize a TAG

EL expression search order (for all Attrbute ()):

If a similar $ {username} username is not found in the time range which it will be:

  1. Page
  2. Request
  3. Session
  4. Application

To order lookup, find a way to join username it will return value return null if not found all the way

EL expressions hidden variables:

From here is the introduction of hidden variables EL expression by these hidden variables allow us to do a lot of simple and complex operations:

JSP EL implicit objects

Supports JSP EL implicit objects listed in the following table:

 

Example: Suppose we want to name the value of the session so we can use:

 ${sessionScope.name}

 Another example: We want to get the GET or POST parameter name passed over, we can only be used before:

 request.getParameter(name);

 EL expression may instead be used:

 ${param.name}

同样,如果我们想要得到一个参数的数组,可以使用:

${paramValues.name}

当然,返回的是一个数组对象.

注意EL 表达式中空值的判定

对NuLL的判断
Empty 对于 null 和”” 都会返回true
== null 则是对null 返回true 而对”” 则是返回false
Not empty 不等于空,包括不等于null 和不等于””
<c:if test="${rdinfo.isProprietaryShop eq '0' or rdinfo.isProprietaryShop eq null }">
<c:if test =”${empty arraylist}”></if>     // 判断对象是否为空对象

总结:

el(expression langauge):表达式语言     作用:获取对象    用法:1、${变量名}  2、${变量  运算符  变量}     使用el的条件:如果用的servlet-api.jar包低于javax.servlet-api-3.0.1版本,web版本必须是3.0之前的。

1、获取PageContext页面范围域的对象,${pageScope.user(或者是list)}

2、获取ServletRequest请求范围域的对象,${requestScope.user(或者是list)}

3、获取HttpSession会话范围域的对象,${sessionScope.user(或者是list)}

4、获取ServletContext应用范围域的对象,${applicationScope.user(或者是list)}

直接${user(或者是list)},也可以获取对象。仅有一个范围域对象时,获取的就是该范围域对象,

有多个对象时,获取的对象的优先顺序为:PageContext页面范围域的对象、获取ServletRequest请求范围域的对象、获取HttpSession会话范围域的对象、获取ServletContext应用范围域的对象。

 

 jstl(java server page standard tag library):

   jsp的标准标签库    

   作用:不让jsp页面出现java代码,实现简单的java程序语句,例如,表达式操作,流程控制,迭代操作,URL操作     使用jstl的条件:jstl.jar,standard.jar,<%@ taglib uri="http://java.sun.com/jsp/jstl.core"    profix="c" %>

使用方法:(举一个最常用的迭代操作)

<c:foreach items="${list}" var="user">
 <tr>
<td>${user.userId}</td>
<td>${user.userName} </td>
<td>${user.userAge} </td>
</tr> </c:foreach>

JSTL简介

JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。

JSTL支持通用的、结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标签。 除了这些,它还提供了一个框架来使用集成JSTL的自定义标签。

JSTL安装

导入jar包

Apache Tomcat安装JSTL 库步骤如下:

官方下载地址: http://tomcat.apache.org/taglibs/standard/

下载 jakarta-taglibs-standard-1.1.2.zip 包并解压,将 jakarta-taglibs-standard-1.1.2/lib/ 下的两个 jar 文件:standard.jar 和 jstl.jar 文件拷贝到 /WEB-INF/lib/ 下。

JSTL标签库

在JSTL中分为以下五个标签

  1. 核心标签
  2. 格式化标签
  3. SQL标签
  4. XML标签
  5. JSTL函数

使用不同的标签的时候需要在JSP中引入不同的库

核心标签(Core):

引入标签库

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

常用标签

应用实例

  <c:set>

<c:set> 等同于JSP中的 session的setAttribute() 方法

使用方法:

1.<c:set var=”名字” value=”值”/>

2.<c:set var=”名字”>值</c:set>

<c:out>

<c:out> 等同于 JSP中的 <%= %>

使用方法:

<c:out value=”需要输出的值”>

在这里可以用EL表达式来进行输出session中的Attrbute

例:

<c:set var=”test” value=”这是一个测试”>

<c:out value=”${test}”>

 输出结果: 这是一个测试

 

<c:remove>

<c:remove> 和c:set标签正好相反,c:set标签是设置setAttrbute 

这个标签则是 删除 Attrbute中设置的值 等同于 session中的removeAttrbute()方法

使用方法:

 <c:remove var=”名字”/>

 

<c:if>

<c:if> 等同于 java 中的 if 语句

使用方法:

<c:if test=”条件”>满足条件执行的JSP代码</c:if>

<c:catch>

<c:catch> 类似于 java 中的 try

使用方法:

<c:catch var=”名字”>JSP代码</c:catch>

如果在执行过程中,JSP代码发生了错误,那么将会复制给名字,因此我们可以通过判断名字是否为NULL 来进行判断是否有异常发生

例:

<c:catch var=”testException”>

<% int i = 5/0; %>

</c:catch>

<c:if test=”${testException != null}”>
    上面的代码有错误发生

 </c:if>

输出结果: 上面的代码有错误发生

如果将 上面的 0 改为 3 则没有任何输出结果

<c:choose> 和 <c:when>

<c:choose> 和 <c:when> 类似于 java 中的 switch 和 case

使用方法:(c:choose中可以有多个c:when标签)

<c:choose>

<c:when test=”条件”>处理</c:when>

<c:when test=”条件”>处理</c:when>

</c:choose>

例:

<c:set var=”test” value=”10”/>

<c:choose>

<c:when test=”${test > 5}”>test 大于5</c:when>

<c:when test=”${test < 5}”>test小于5</c:when>

</c:choose>

输出结果: test大于5

<c:otherwise>

<c:otherwise> 等同于 java switch 中的 default

使用方法

<c:choose>

<c:otherwise>处理</c:otherwise>

</c:choose>

当choose 中的 when 没有满足条件的时候 会执行 otherwise 中的内容

注意: choose 和 when 和 otherwise 是一组 when 和 otherwise 必须写在choose中

<c:import>

使用方法:

<c:import var=”date” url=”http://www.baidu.com” />

通过上面的写法会将 百度 的源代码返回到 date 变量中,使用

<c:out value=”${date}”>

输出

<c:forEach> 和 <c:forTokens>

类似于JAVA中的 FOR循环 和 FOREACH循环

使用方法:

<c:forEach var=”名字(i)” begin=”开始数字(1)” end=”结束数字(5)”>

${i}

</c:forEach>

输出结果是1 2 3 4 5

使用方法:

<c:forTokens items=”a,b,c,d,e” delims=”,” var=”name”>

${name}

</c:forTokens>

输出结果 a b c d e

forEach类似于JAVA中 for 循环 设置一个开始数字 begin 设置一个结束数字 end 然后每次循环都会将值赋值给 var   另外还有个 setp参数 里面可以设置值 意思是每次增加几个数

forTokens类似于JAVA中的foreach 在items中放一个组 然后 delims设置以什么分割 然后 将分割后循环的值赋值给 var  如果 items中存放的是一个集合 那么可以不用写delims 这样每循环一次 就会取出集合中的一个放在 var中

<c:redirect>

类似于service中的重定向

使用方法:

<c:redirect url=”http://www.baidu.com”/>

重定向到百度去

另外还有<c:url> 和 <c:param>两个标签

 标签主要有

<%--JSTL 核心标签--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--JSTL 格式化标签--%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--JSTL SQL标签--%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%--JSTL XMLb标签--%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%--JSTL 函数标签--%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

其他JSTL具体用法可以查看菜鸟教程地址https://www.runoob.com/jsp/jsp-jstl.html

 

EL表达式介绍: 

EL详细用法可以查看菜鸟教程地址:https://www.runoob.com/jsp/jsp-expression-language.html

EL表达式定义规则:以 $ 开头 内容写在 {} 中 例: ${test}

当然,因为EL表达式所有的代码都是写在{}中的,所以我们也可以在里面写运算

比如:

${test+100}

最终输出的结果就是test这个变量加上数字100后的值了

再比如

${test != null} 

最终输出的结果就是一个true  通过这种方式  我们可以很好的和JSTL结合起来一起使用.

EL中的运算符分为  .  和  []  两种

如果要动态取值 可以使用[]  比如:

${session.user[date]}

其中的date是一个变量

上面代码的意思是 取出 session.user这个数组里面的第 date 个元素

需要注意的是,EL表达式限制了我们调用JAVA方法,如果想要调用,可以自定义一个TAG

EL表达式查找顺序(都是针对Attrbute()):

如果使用类似于 ${username} 的时候没有在哪一个范围查找username 那么它会以:

  1. Page
  2. Request
  3. Session
  4. Application

为顺序来进行查找,加入中途找到了 username 那么就会返回值  如果一路没找到 返回 null

EL表达式的隐性变量:

从这里开始就是介绍EL表达式的隐性变量,通过这些隐性变量可以让我们简便完成很多复杂的操作:

JSP EL隐含对象

JSP EL支持下表列出的隐含对象:

 

例:假设我们是想要session中的name值 那么我们可以使用:

 ${sessionScope.name}

 再例:我们想要获得GET或则POST传递过来的name参数,在以前我们只能使用:

 request.getParameter(name);

 使用EL表达式可代替为:

 ${param.name}

同样,如果我们想要得到一个参数的数组,可以使用:

${paramValues.name}

当然,返回的是一个数组对象.

注意EL 表达式中空值的判定

对NuLL的判断
Empty 对于 null 和”” 都会返回true
== null 则是对null 返回true 而对”” 则是返回false
Not empty 不等于空,包括不等于null 和不等于””
<c:if test="${rdinfo.isProprietaryShop eq '0' or rdinfo.isProprietaryShop eq null }">
<c:if test =”${empty arraylist}”></if>     // 判断对象是否为空对象

总结:

el(expression langauge):表达式语言     作用:获取对象    用法:1、${变量名}  2、${变量  运算符  变量}     使用el的条件:如果用的servlet-api.jar包低于javax.servlet-api-3.0.1版本,web版本必须是3.0之前的。

1、获取PageContext页面范围域的对象,${pageScope.user(或者是list)}

2、获取ServletRequest请求范围域的对象,${requestScope.user(或者是list)}

3、获取HttpSession会话范围域的对象,${sessionScope.user(或者是list)}

4、获取ServletContext应用范围域的对象,${applicationScope.user(或者是list)}

直接${user(或者是list)},也可以获取对象。仅有一个范围域对象时,获取的就是该范围域对象,

有多个对象时,获取的对象的优先顺序为:PageContext页面范围域的对象、获取ServletRequest请求范围域的对象、获取HttpSession会话范围域的对象、获取ServletContext应用范围域的对象。

 

 jstl(java server page standard tag library):

   jsp的标准标签库    

   作用:不让jsp页面出现java代码,实现简单的java程序语句,例如,表达式操作,流程控制,迭代操作,URL操作     使用jstl的条件:jstl.jar,standard.jar,<%@ taglib uri="http://java.sun.com/jsp/jstl.core"    profix="c" %>

使用方法:(举一个最常用的迭代操作)

<c:foreach items="${list}" var="user">
 <tr>
<td>${user.userId}</td>
<td>${user.userName} </td>
<td>${user.userAge} </td>
</tr> </c:foreach>

Guess you like

Origin www.cnblogs.com/2019wxw/p/10947699.html