JSP标准标签库(Jsp Standarded Tag Library) ,使用标签取代JSP页面上的Java代码。如下代码就是JSTL标签

在pom.xml导入坐标:

<dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version>
</dependency>

由于standard.jar与jstl.jar需一起使用,但是jstl 1.2版本的就不需要这个standard.jar包了,去掉standard.jar文件后重启tomcat就不会报错。

// 下面这个当jstl版本为1.2的时候不需要导入
<dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version>
</dependency>

创建jsp文件,在JSP页面上引入JSTL标签库

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

创建java类文件,Brand.java

package com.itheima.web.pojo;/*** 品牌实体类*/public class Brand {// id 主键private Integer id;// 品牌名称private String brandName;// 企业名称private String companyName;// 排序字段private Integer ordered;// 描述信息private String description;// 状态:0:禁用  1:启用private Integer status;public Brand() {}public Brand(Integer id, String brandName, String companyName, String description) {this.id = id;this.brandName = brandName;this.companyName = companyName;this.description = description;}public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) {this.id = id;this.brandName = brandName;this.companyName = companyName;this.ordered = ordered;this.description = description;this.status = status;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getBrandName() {return brandName;}public void setBrandName(String brandName) {this.brandName = brandName;}public String getCompanyName() {return companyName;}public void setCompanyName(String companyName) {this.companyName = companyName;}public Integer getOrdered() {return ordered;}public void setOrdered(Integer ordered) {this.ordered = ordered;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}@Overridepublic String toString() {return "Brand{" +"id=" + id +", brandName='" + brandName + '\'' +", companyName='" + companyName + '\'' +", ordered=" + ordered +", description='" + description + '\'' +", status=" + status +'}';}
}

在jsp的脚本中写准备数据

<%List<Brand> brands = new ArrayList<Brand>();brands.add(new Brand(1,"三只松鼠","三只松鼠",100,"三只松鼠,好吃不上火",1));brands.add(new Brand(2,"优衣库","优衣库",200,"优衣库,服适人生",0));brands.add(new Brand(3,"小米","小米科技有限公司",1000,"为发烧而生",1));
%>

JSTL的使用

1.if 标签+el表达式

<c:if test=""></c:if>

(1)test为必须属性,接受boolean表达式

(2)如果表达式为true,则显示if标签体内容,如果为false,则不显示标签体内容

(3)" "内写逻辑判断的内容

(4)一般情况下,test属性值会结合el表达式一起使用,${} 是el表达式

(5)" "内的true 或者 false 决定了<c:if> </c:if>包含的信息能不能显示

(6)代码实例:

<%--  jsp文件下 直接输入以下内容  --%>
request.setAttribute("number",3);
<c:if="${number % 2 == 0}">这个是偶数
</c:if><c:if="${number % 2 != 0}">这个是奇数
</c:if>
request.setAttribute("status",1);
<c:if test="${status ==1}">启用
</c:if>
<%--  集合判断是否为空  --%>
<%List list = new ArrayList();list.add("abcd");// 把第二个list(上面定义的那个)的值,传递给一个叫list(第一个list)的对象request.setAttribute("list",list);
%><c:if test="not empty list">list不为空
</c:if>

2.forEach 标签

<c:forEach>
foreach:相当于java代码的for语句1. 完成重复的操作for(int i = 0; i < 10; i ++){}* 属性:begin:开始值end:结束值var:临时变量step:步长varStatus:循环状态对象index:容器中元素的索引,从0开始count:循环次数,从1开始2. 遍历容器List<User> list;for(User user : list){}* 属性:items:容器对象var:容器中元素的临时变量varStatus:循环状态对象index:容器中元素的索引,从0开始count:循环次数,从1开始

用法一:

* items:被遍历的容器* var:遍历产生的临时变量* varStatus:遍历状态对象
<c:forEach items="${brands}" var="brand"><tr align="center"><td>${brand.id}</td><td>${brand.brandName}</td><td>${brand.companyName}</td><td>${brand.description}</td></tr>
</c:forEach>

用法二:

* begin:开始数* end:结束数* step:步长
<c:forEach begin="0" end="10" step="1" var="i">${i}
</c:forEach>
// 输出 1-10
<c:forEach begin="0" end="10" step="1" var="i" varStatus="s">${s.index}// 从0开始${s.count}// 变量的次数
</c:forEach>
// 输出 1-10

3.choose+when+otherwise

choose相当于java里面的switch

when相当于java里面的case

otherwise相当于java里面的default

在jsp页面中输入以下案例:

    <%request.setAttribute("number",51);%><c:choose><c:when test="${number == 1}">星期一</c:when><c:when test="${number == 2}">星期二</c:when><c:when test="${number == 3}">星期三</c:when><c:when test="${number == 4}">星期四</c:when><c:when test="${number == 5}">星期五</c:when><c:when test="${number == 6}">星期六</c:when><c:when test="${number == 7}">星期天</c:when><c:otherwise>数字输入有误</c:otherwise></c:choose>

当number为1的时候,jsp界面上展示出星期一。

若number不在test的范围里面,则展示出 数字输入有误

JSP之JSTL标签相关推荐

  1. jsp导入jstl标签库_EE JSP:使用JSTL标记库生成动态内容

    jsp导入jstl标签库 除了在JSP中编写自己的定制标记之外,您还将发现Java EE实际上提供了一组Java标准标记库(JSTL)供您使用. 这些内置标签包括重复(for-loop)标签,条件标签 ...

  2. jsp判断语句_Java的web展现层JSP的JSTL标签详细总结

    <大数据和人工智能交流>头条号向广大初学者新增C .Java .Python .Scala.javascript 等目前流行的计算机.大数据编程语言,希望大家以后关注本头条号更多的内容. ...

  3. jsp中jstl标签的类似 if - else 语句 的语法

    在jsp中引入jstl的标签库和函数库 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c&q ...

  4. 随笔-jsp 利用jstl标签分页

    ${pager.totalpage}是总页数 <c:forEach begin="1" end="${pager.totalpage}" var=&quo ...

  5. JSP标签和JSTL标签注意点

    1.转发和重定向问题 当前项目:/Test 转发路径:"/"根目录表示当前项目"/Test","/login.jsp"就是"/Te ...

  6. jsp基础、el技术、jstl标签、javaEE的开发模式

    一.jsp技术基础 1.jsp脚本和注释 jsp脚本: 1)<%java代码%> ----- 内部的java代码翻译到service方法的内部 2)<%=java变量或表达式> ...

  7. JSP页面中使用JSTL标签出现无法解析问题解决办法

    JSP页面中使用JSTL标签出现无法解析问题解决办法 参考文章: (1)JSP页面中使用JSTL标签出现无法解析问题解决办法 (2)https://www.cnblogs.com/xdp-gacl/p ...

  8. JSP标签:jsp内置标签、jstl标签、自定义标签

     一.jsp标签的分类: 1)内置标签(动作标签): 不需要在jsp页面导入标签 2)jstl标签: 需要在jsp页面中导入标签 3)自定义标签 : 开发者自行定义,需要在jsp页面导入标签    1 ...

  9. JSP常用标签——JSTL标签和EL表达式

    一.JSTL简介 1.什么是JSTL JSTL是Java中的一个定制标记库集.(这个标记库集不需要自己编写,可以直接使用) 2.为什么要使用JSTL 实现了JSP页面中的代码复用(基于标签库原理,重复 ...

最新文章

  1. 【转】MySQL常用命令总结
  2. iOS网络编程之同步、异步、请求队列
  3. Redirecting to /bin/systemctl restart sshd.service
  4. 记一次httpclient调用失败
  5. 计算机专业office,2011 级计算机专业 Office 办公软件期末考试.doc
  6. pcl_openmap_OpenMap教程–第1部分
  7. this指向问题(2)
  8. JavaScript 编程精解 中文第三版 六、对象的秘密
  9. python进阶22再识单例模式
  10. 返回json格式的编写(Msg)
  11. Docker使用link建立容器之间的连接
  12. (转)使用XMind打开.mmap文件不显示问题
  13. ubuntu 14.04 Dell 台式机 无线网卡驱动安装问题,无线网卡:Qualcomm Atheros Device [168c:0042] (rev 31)
  14. html5 调用unity,Unity调用UniWebView打开H5界面脚本
  15. 目标检测NMS非极大值抑制及改进
  16. 中兴盒子B860AV2.1-A-B-M-U,通刷,线刷刷机固件
  17. 什么是 Docker ?
  18. IDEA配置Tomcat时总是提示:Error running ‘Tomcat 8.5.73‘ 问题
  19. linux内核irq,linux kernel的中断子系统之(四):High level irq event handler
  20. 区块链最好的编程语言是什么?

热门文章

  1. 选择隔离电源还是非隔离电源?
  2. svn分支开发与主干合并(branch merge)
  3. jstree的简单使用例子
  4. 任务栏广告弹窗源头查找与处理方法
  5. Android-0. Android studio在导航栏增加自己的功能图标(如小扳手)
  6. RabbitMQ延时队列
  7. DSP CCS12.00 芯片:TMS320F28335 PWM 的设计
  8. Surround360 README文档——中文翻译
  9. 吴恩达机器学习课后作业ex1(python实现)
  10. Android 更换应用图标无效