java基础开发—jstl标签库

时间:2023-03-09 16:37:03
java基础开发—jstl标签库

在DRP项目中。接触到了JSTL标签库。

在未使用Jstl之前,我们使用JSP脚本实现一些声明或是表达式任务,做一些业务相关操作时,须要在页面中嵌入大量的java代码。在DRP项目开发前期。使用jsp页面给我的感受就是。jsp页面什么活都能干,权利过高。导致的结果就是,jsp页面提高了程序的复杂性、增强了代码维护的复杂度。以及代码阅读的困难。对程序猿的要求也相对较高。比如,虽然精通页面设计的开发者来维护jsp。也经常须要依赖于经验丰富的程序猿。所以须要一种技术来解决这些问题,以下简介一下jstl:

Jstl(JSP Standard Tag Library)是由SUN公司提出的JSP的一个标准标签库。将经常使用功能包装到定制标记库集合中。

这些标记库跟HTML标签不同。它主要应用与server端。

JSTL包含五类标准标记库:核心库、格式库、XML库、SQL库、函数库。同一时候Jstl是与EL表达式一同使用的,E L(Expression
Language) 目的:为了使JSP写起来更加简单。

个人觉得其并不属于jstl,而是jstl使用了el表达式,一起更方便的服务于jsp。

EL表达式 

语法:$和{}

内置对象: pageScope,requestScope,sessionScope,applicationScope

Empty操作符:决定对象、集合或字符串变量是否为空或null,比如:

servlet中定义:request.setAttribute("v2", "");

jsp:v1:${empty v1}<br>

“.“和“[ ]“两种运算符来存取数据:比如:

${user.username}

${users[5].username }

运算符:【==/eq   !=/ne   </lt     >/gt    &&/and    ||/or   !/not    //div    %/mod】 比如:

<li>el表达式对运算符的支持</li><br>
1+1=${1+1 }<br>
10/5=${10/5 }<br>
10 div 5=${10 div 5 }<br>
10 % 3=${10 % 3 }<br>
10 mod 3 = ${ 10 mod 3 }<br>

Jstl标签库:

在使用jstl前提须要引入jstl.jarstandard.jar两个jar包。

1.核心库:引入<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="c"%>   

c:out标签:hello(使用标签):<c:out value="${hello}"/><br>

c:set标签:<c:set value="root" var="userid"/>

c:remove标签:<c:remove var="userid"/>

条件控制标签:c:if、c:choose、c:when

<c:choose>
<c:when test="${v1 gt v2}">
v1大于v2<br>
</c:when>
<c:otherwise>
v1小于v2<br>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${empty userList}">
没有符合条件的数据<br>
</c:when>
<c:otherwise>
存在用户数据<br>
</c:otherwise>
</c:choose>

循环控制标签

<c:choose>
<c:when test="${empty users}">
<tr>
<td colspan="3">没有符合条件的数据</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.username }</td>
<td>${user.age }</td>
<td>${user.group.name }</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>



2.函数库:引入<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>   

<body>
hello.length(使用JSTL的length函数, 函数必须放到EL表达式中,使用方法:前缀+冒号+函数名称):${fn:length(hello) }<br>
<p>
<c:forEach items="${fn:split(str, \"#\") }" var="v">
${v }<br>
</c:forEach>
<p>
自己定义函数:${my:say("张三") }<br>
</body>



3.格式化库:引入<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fm"%>   

以下是获取时间和日期的三种形式

today(type="date"):<fmt:formatDate value="${today}" type="date"/><br>
today(type="time"):<fmt:formatDate value="${today}" type="time"/><br>
today(type="both"):<fmt:formatDate value="${today}" type="both"/><br>

DRP项目中遍历下拉框实例:

1.未使用Jstl

<tr>
<td height="26">
<div align="right">
<font color="#FF0000">*</font>类别:
</div>
</td>
<td>
<%
List<ItemCategory> itemCategoryList = (List)request.getAttribute("itemCategoryList");
%>
<select name="category" class="select1" id="category">
<%
for(Iterator<ItemCategory> iter = itemCategoryList.iterator();iter.hasNext();){
ItemCategory ic = iter.next();
%>
<option value="<%=ic.getId()%>"><%=ic.getName()%></option>
<%
}
%>
</select>
</td>
</tr>

2.使用jstl

<tr>
<td height="26">
<div align="right">
<font color="#FF0000">*</font>类别:
</div>
</td>
<td>
<select name="category" class="select1" id="category">
<c:forEach items="${itemCategoryList }" var="itemCategory">
<option value="${itemCategory.id }">${itemCategory.name }</option>
</c:forEach>
</select>
</td>
</tr>

总结:

对于程序猿来说。不将就是发现问题的动力源泉。有什么样的需求。就会创造出什么样的技术和工具。Jstl封装了web应用程序的一些通用功能。我们在使用的时候,不必须要理解其内部是怎样实现的,仅仅须要了解其标签每一个标签代表的功能就可以。

jstl的出现削弱jsp页面过高的权利。使jsp页面分层更加清楚而出现的。