JSTL

JSP Standard Tag Library JSP标准标签库,JSP 为开发者提供的一系列的标签,使用这些标签可以完成一些逻辑处理,比如循环遍历集合,让代码更加简洁,不再出现JSP脚本穿插的情况。

实际开发中EL和JSTL结合起来使用,JSTL侧重于逻辑处理,EL 负责展示数据。

JSTL的使用

1.需要导入jar包(两个jstl.jar standard.jar)存放的位置web/WEB-INF

2、在JSP页面开始的地方导入JSTL标签库

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

3、在需要的地方使用

<c:forEach items="${list}" var="user">

<tr>

<td>${user.id}</td>

<td>${user.name}</td>

<td>${user.score}</td>

<td>$ {user.address . value}</td>

</tr>

</c: forEach>

JSTL优点:

1、提供了统一的标签

2、可以用于编写各种动态功能


核心标签库常用标签:

set、out、remove、catch


set:向域对象中添加数据

requset.setAttribute (key,,value)

<c:set var=" name" value= ”tom" scope= " request"></c:set>

$ {requestScope . name }

<%

User user = new User(1, "张三”,66.6, new Address(1, “科技路"));

request setAttr ibute("user" ,user);

%>

${user.name}

<hr/>

<c:set target=" ${user}" property= "name" value= "李四"></c:set>

$ {user.name}

out:输出域对象中的数据

<c:set var=" name" value= ”tom" ></c:set>

<c:out value=" $ {name}" default=" 未定义"></c:out>

remove:删除域对象中的数据

<c:remove var= ”name" scope= " page "></c:remove>

<c:out value="$ {name}" default=" 未定义"></c:out>


catch:捕获异常

<c:catch var= ”error ">

<%

int a = 10/0;

%>

</c:catch>

${error}



条件标签: if choose

<c:set var=" numl" value="l"></c:set>

<c:set var=" num2" value="2"></c:set>

<c:if test="${numl>num2}">ok</c:if>

<c:if test="${num1<num2}">fail</c:if>

<hr/>

<c:choose>

<c:when test= "${numl>num2 }">ok</c when>

<c:otherwise>fail</c:otherwise>

</c:choose>


迭代标签 : forEach

<c:forEach items="${list}"  var="str"  begin="2" end="3" step="2" varStatus="sta">

${sta. count}-${str}<br/>

</c: forEach>


格式化标签库常用的标签:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。