Day07 Dynamic Page Technology (JSTL)

Overview of JSTL expressions

  • (1) What is jstl
    JSTL (JSP Standard Tag Library), the JSP standard tag library, which can be embedded in the JSP page to complete business logic and other functions in the form of tags.
  • (2) What is the meaning of jstl?
    The purpose of jstl is to replace the script code in the jsp page the same as el.
  • (3) The JSTL standard standard tag library has 5 sub-libraries, and currently his core library is often used
    Insert picture description here

JSTL expression-environment preparation

  • (1) Import the jar package
    Insert picture description here
  • (2) Import tag library
<%--引入jstl--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"   prefix="c"%>

JSTL expression-if tag

  • (1) There are many core tags of jstl, and currently the only commonly used tags are if and foreach tags.
  • (2) The <c:if> tag
    plays a role in the judgment of java code
  • (3) Introduction to if tag attributes
    Insert picture description here
  • Example
<%
    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>

JSTL expression-for tag

  • (1) forEach tag
    plays the role of for loop of java code
  • (2) forEach tag attribute introduction
    Insert picture description here
<%--
     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>

Guess you like

Origin blog.csdn.net/qq_43639081/article/details/109382051
Recommended