Day07JavaWeb [Jsp, el, jstl, comprehensive case] jstl tag library***

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 significance of jstl?
    The purpose of jstl is to replace the script code in the jsp page the same as el.
  • (3) JSTL standard standard tag library has 5 sub-libraries, and the core library is often used at present
    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 the only commonly used tags are if and foreach tags.
  • (2) The <c:if> tag
    plays the role of judging the java code-
    (3) Introduction to the attributes of the if tag
    Insert picture description here
    web\demo6_jstl_demo.jsp
<%--使用JSTL来简化以上代码--%>
    <%
        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>
        <div></div>
    </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) Introduction to forEach tag attributes
    Insert picture description here
<%--
     for标签:
        1:普通for
         for(int i=0; i<5; i++)
         begin: 表示索引开始
         end  :表示索引结束,包含结束值
         var  :循环变量  i,  jsp会自动的将该值存放在pageContext域中
         step :每一次循环的增量
        2:增强for
 --%>
        <%
            request.getParameter("username");

            int num = 10;
            request.setAttribute("num", num);
        %>
        <c:forEach begin="0" end="${num}" var="i" step="1">
                <h1 style="color: red;">helloword${
    
    i}</h1>
        </c:forEach>

        <%
            ArrayList<String> list = new ArrayList<String>();
            list.add("baoqiang1");
            list.add("baoqiang2");
            list.add("baoqiang3");
            request.setAttribute("list", list);
        %>
        <%--
           for( String str: list)

           items="${list}" 从域中根据list这个键获取集合对象
           var="str"       每次循环时,jstl会自动将集合中的元素赋给str
                           每次循环时,jstl会自动将str的值存入pageContext域
           varStatus="vs"  这个参数会记录当前循环的一些状态信息
                    vs.count  可以获取当前循环的次数
        --%>
        <c:forEach items="${list}" var="str" varStatus="vs">
             <%--${
    
    str}--%>
            现在是第${
    
    vs.count}次循环<br/>
        </c:forEach>

        <%
            ArrayList<User> list2= new ArrayList<User>();
            list2.add(new User("liuyan1",33,"female",new Birthday(1991,12,21)));
            list2.add(new User("liuyan2",34,"female",new Birthday(1992,12,21)));
            list2.add(new User("liuyan3",38,"female",new Birthday(1993,12,21)));
            request.setAttribute("list2", list2);
        %>
        <c:forEach items="${list2}" var="user">
                ${
    
    user.name} ----${
    
    user.birthday.year}<br/>
        </c:forEach>

Guess you like

Origin blog.csdn.net/u013621398/article/details/108561107