JSTL基础部分
在使用 JSTL时 记得 正确引入了 JSTL 标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
jstl if标签 判断
test 属性表示判断的条件(使用 EL 表达式输出)<br>
<c:if test="${12==12}">
正确 <br>
</c:if>
jstl多路判断
<%--
<c:choose> <c:when> <c:otherwise>标签
作用:多路判断。跟 switch ... case .... default 非常接近
choose 标签开始选择判断
when 标签表示每一种判断情况
test 属性表示当前这种判断情况的值
otherwise 标签表示剩下的情况
<c:choose> <c:when> <c:otherwise>标签使用时需要注意的点:
1、标签里不能使用 html 注释,要使用 jsp 注释
2、when 标签的父标签一定要是 choose 标签
--%>
<% request.setAttribute("h",179);%>
<c:choose>
<c:when test="${requestScope.h>180}">
<h2>180+好高啊</h2>
</c:when>
<c:when test="${requestScope.h>170}">
<h2>170+很棒</h2>
</c:when>
<c:otherwise>
<h2>160+也行啦</h2>
</c:otherwise>
</c:choose>
jstl foreach遍历Object数组**
<%-- 遍历 Object 数组
items 表示遍历的数据源(遍历的集合)
var 表示当前遍历到的数据
--%>
<% request.setAttribute("arr",new String[]{"黑暗","黎明","破晓"});%>
<c:forEach items="${requestScope.arr}" var="item">
${item} <br>
</c:forEach>
遍历Map集合
<%
Map<String, Object> map = new HashMap<>();
map.put("key1","v1");
map.put("key2","v2");
map.put("key3","v3");
request.setAttribute("map",map);
%>
<c:forEach items="${requestScope.map}" var="entry">
${entry}:<br> key: ${entry.key} value: ${entry.value} <br>
<%-- ${entry} <br>--%>
</c:forEach>
遍历List集合
Student类
点击查看代码
package com.hard.bean;
/**
* 哈哈哈嗨
* 功能:
*/
public class Student {
private Integer id;
private String name;
private String password;
private Integer age;
private Integer phone;
public Student() {
}
public Student(Integer id, String name, String password, Integer age, Integer phone) {
this.id = id;
this.name = name;
this.password = password;
this.age = age;
this.phone = phone;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", age=" + age +
", phone=" + phone +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getPhone() {
return phone;
}
public void setPhone(Integer phone) {
this.phone = phone;
}
}
<%--遍历List集合--%>
<%
List<Student> list = new ArrayList<>();
// ctrl+p 查看类的属性
list.add(new Student(1,"李白","123",18,1548));
list.add(new Student(2,"杜甫","14513",19,11554548));
list.add(new Student(3,"杜牧","14798",16,1348548));
request.setAttribute("l",list);
%>
<c:forEach items="${requestScope.l}" var="stu">
<%-- 通过调用getXxx方法访问对象属性--%>
${stu} <br>
${stu.id}
${stu.name}
${stu.password}
${stu.age}
${stu.phone} <br>
</c:forEach>