--JSTL review study notes JSP tag library

Original link: http://www.cnblogs.com/fingerboy/p/5174106.html

  Written before the time of jstl jsp tag library is somewhat contradictory, because I think java code embedded in the way almost always work, no need to use tag libraries, but this review is good to learn a bit and found that this is still very useful, can save a lot of things well, JSTL is an extension of el expressions, is a markup language, more convenient to use, he does not belong jsp built-in label, you need to guide package before use, and specify the tag library, in Myeclipse in built jstl jar package, it is not necessary that the leader packet operation.

  There are four JSTL tag libraries, only a common Core, almost do not use the other, the first prior to use to import tag library, as follows:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

  The following describes the common label, since the label is multi-core library, it is generally known as tags used c:

  • <C: OUT>   : Output
    1.  value: can be a string, such expression may be el <c: out value = "hello" /> or <c: out value = "$ {name}" />
    2.  default: If value is null, then the value of the default output, such as the inside of the <c: out value = "$ {name}" default = "zhangSan" /> $ {name} If not present, the output Joe Smith.
    3.  escapeXML: represents the escape, the default is true

  Example:

  < C: OUT value = "AAA" />          output string AAA
  < C: OUT value = "$ {AAA" />        output field attribute aaa, wherein the same with $ {aaa}
    < C: OUT value = "$ {AAA } " default =" xxx " />    If $ {aaa} does not exist, then the output string xxx
    < C: OUT value =" $ {aaa} " the escapeXml =" to true " /> If $ {aaa} contains special characters then escape it. This prevents javascript attack

 

  • <C: SET> : setting (creating domain properties)
    1.   var: variable name
    2.   value: variable values
    3.   scope: Specifies a default page select field requset, session, application

      E.g:

<c:set var="name" value="zhangSan" scope="request"/>


<% request.setAttribute("name");   %>

 

      These two lines down effect is the same, both in the request to create a domain name attribute value of "zhangSan".

  •  <c: the Remove> : Delete variable domain
    1. var: variable name
    2. scope: the specified domain, if not specified, the default delete all domains
< C: Remove var = "a" />        delete a domain attributes named
 < C: Remove var = "a" scope = "page" /> Delete page domain is a domain property named
  • <C: url> : output a url or saved to a domain url
  1. value: Specifies a path, he will automatically add the project name before the path
  2. var: Specify a variable name, once the property is added, it will not output, but to save the output to the specified variable name
  3. scope: use with var, the specified storage domain
< C: url value = "/ aServlet" />                         Output URL: / project name / aServlet
 < C: url value = "/ aServlet" var = "url" scope = "page" />   to save the generated url to the page domain rather not output
 < c: url value = "/ aServlet" >             : output URL:? / project name / aServlet username =% xx% xx % xx% xx% xx% xx, where seating will be URL encoded
    < C: param name = "username" value = "John Doe" /> 
</ C: URL / >
  • <c: if>: corresponding to the if statement in Java
  1. <C: if test = "$ {condition}"> ... </ c: if>: When the test is true, the content execution tag body
  • <c: choose>: corresponds java statement if / else

  Example:

<c:set var="score" value="${param.score }"/>
<c:choose>
    <c:when test="${score > 100 || score < 0}">错误的分数:${score }</c:when> 
    <c:when test="${score >= 90 }">A级</c:when> 
    <c:when test="${score >= 80 }">B级</c:when>
    <c:when test="${score >= 70 }">C级</c:when>
    <c:when test="${score >= 60 }">D级</c:when>
    <c:otherwise>E级</c:otherwise> 
</c:choose>
  • <C: forEach> :! used to loop through the array, or in a set of counting cycle corresponds java for loop
  1. Press cycle counting manner:
    <c:forEach begin="1" end="10" var="i">
     ${i}
    </c:forEach>
    Equivalent to
    for(int i = 1; i <= 10; i++) {
      out.println(i);
    }
  2. Traverse an array or collection:
    <% 
            String [] STR = { " Ah " , " good " , " the hanging " };
              request.setAttribute("array", str);
    %>
        <c:forEach items="${array }" var="i">
        -->${i }<br>
        </c:forEach>

    The output is:

-> Whoops
-> Yes
-> This hanging

  Special note is required, there is a property tag forEach: varStatus, this property is used to specify receiving "circulation state" variable name, for example: <forEach varStatus = "vs" ... />, then you can use this variable vs to get the status of the cycle, he has several attributes are as follows:

  1.  count: int type, to traverse the current number of elements;
  2.   index: int type, index of the current element;
  3.  first: boolean type, whether the first element;
  4.  last: boolean type, whether it is the last element;

In the example above, we traverse all the elements of the array, if we want to take the first element of the array "Ah," how should I do?

<c:forEach items="${array }" var="i" varStatus="index">
    <c:if test="${index.first }">
    --->${i }
    </c:if>
</c:forEach>

So that the output becomes:


---> ouch

Reproduced in: https: //www.cnblogs.com/fingerboy/p/5174106.html

Guess you like

Origin blog.csdn.net/weixin_30633405/article/details/94790473