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;
}
@Override
public 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的范围里面,则展示出 数字输入有误