Descripción general de las expresiones JSTL
(1) ¿Qué es jstl JSTL (JSP Standard Tag Library), la biblioteca de etiquetas estándar JSP, que se puede incrustar en la página JSP para completar la lógica empresarial y otras funciones en forma de etiquetas?
(2) ¿Cuál es el significado de jstl? El propósito de jstl es reemplazar el código del script en la página jsp de la misma forma que el.
(3) La biblioteca de etiquetas estándar estándar de JSTL tiene 5 subbibliotecas, y actualmente su biblioteca principal se usa con frecuencia
Preparación del entorno de expresión JSTL
(1) Importe el paquete jar
(2) Importar biblioteca de etiquetas
< % -- 引入jstl-- % >
< % @ taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "c" % >
Etiqueta de expresión-if de JSTL
(1) Hay muchas etiquetas principales de jstl, y actualmente las únicas etiquetas que se usan comúnmente son las etiquetas if y foreach.
(2) La etiqueta <c: if> juega un papel en el juicio del código java
(3) Introducción a los atributos de etiqueta if
Ejemplo
< %
int a = 200 ;
int b = 500 ;
request. setAttribute ( "a" , a) ;
request. setAttribute ( "b" , b) ;
% >
< % --
test:测试条件成立
var: 用来保存条件的结果,true 或者false
scope: 表示将结果存到哪个域中
-- % >
< c: if test= "${a > b }" var= "bl" scope= "session" >
< h1 style= "color: green" > a大于b< / h1>
< / c: if >
< c: if test= "${!(a > b) }" >
< h1 style= "color: red" > a小于b< / h1>
< / c: if >
Etiqueta de expresión para JSTL
(1) forEach etiqueta juega el papel de bucle for de código java
(2) para cada introducción de atributo de etiqueta
< % --
for 标签:
1 : 普通for
for ( int i= 0 ; i< 5 ; i++ )
begin: 表示索引开始
end : 表示索引结束,包含结束值
var : 循环变量 i, 与begin+ step一同增长, jsp会自动的将该值存放在pageContext域中
step : 每一次循环的增量
-- % >
< %
int num = 10 ;
request. setAttribute ( "num" , num) ;
% >
< c: forEach begin= "1" end= "${num}" step= "1" var= "i" >
< h1 color= "green" > HelloWorld + ${
i} < / h1>
< / c: forEach>
< hr/ >
< % --
2 : 增强for
for ( String str: list)
items= "${list}" 从域中根据list这个键获取集合对象
var= "str" 每次循环时,jstl会自动将集合中的元素赋给str
每次循环时,jstl会自动将str的值存入pageContext域
varStatus= "vs" 这个参数会记录当前循环的一些状态信息
vs. count 返回值为number 可以获取当前循环的次数
vs. index 返回值为number 获取集合成员的索引( 下标从0 开始)
vs. first 返回值为boolean 现在指到的集合成员是否为第一个成员
vs. last 返回值为boolean 现在指到的集合成员是否为最后一个成员
-- % >
< %
ArrayList< String> list = new ArrayList < > ( ) ;
list. add ( "str1" ) ;
list. add ( "str2" ) ;
list. add ( "str3" ) ;
request. setAttribute ( "list" , list) ;
% >
< c: forEach items= "${list}" var= "str" varStatus= "vs" >
< h1 color= "red" > ${
str} , 现在是第${
vs. count} 次循环< / h1>
< / c: forEach>