JSTL是为了替代JSP中的代码脚本。
一. 使用步骤
1.导入JSTL标签库的jar包
2.使用taglib指令引入标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
二. core核心库使用
1.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%
request.setAttribute("key1", "value1");
%>
${requestScope.key1}
${requestScope.key2}
2.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
12等于12
3.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%
request.setAttribute("height",178);
%>
很高
高
还行
凑和
矮
其他情况
4.
①遍历普通数据
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
${i}
| 第${i}行 |
②遍历Object数组
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%
request.setAttribute("names",new String[]{"张三","李四","王二"});
%>
${name}
③遍历Map
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%
Map map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
request.setAttribute("map",map);
%>
${entry}
${entry.key}
${entry.value}
${entry.key} = ${entry.value}
④遍历List
<%@ page import="java.util.List" %>
<%@ page import="pojo.Person" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%
List personList = new ArrayList<>();
for(int i = 1; i <= 10; i++){
personList.add(new Person("name"+i, "password"+i, 18+i));
}
request.setAttribute("person",personList);
%>
${p}
姓名
密码
年龄
${p.name}
${p.password}
${p.age}
package pojo;
public class Person {
private String name;
private String password;
private Integer age;
public Person() {
}
public Person(String name, String password, Integer age) {
this.name = name;
this.password = password;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", password='" + password + ''' +
", age=" + age +
'}';
}
}
⑤forEach标签组合使用去遍历
<%@ page import="pojo.Person" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%
List personList = new ArrayList<>();
for(int i = 1; i <= 10; i++){
personList.add(new Person("name"+i, "password"+i, 18+i));
}
request.setAttribute("person",personList);
%>
姓名
密码
年龄
${p.name}
${p.password}
${p.age}



