A first glimpse of jsp/servlet learning five jsp expression language

  EL expressions are one of the most commonly used expressions in jsp. EL expressions start with ${ and end with }, eg ${1+1}. The el expression can return any type of value. If the result of the el expression is an object with an attribute, you can use the [] or . operation to access the attribute, such as:

  ${object.["propertyName"]} or ${object.propertyName}

  There are some commonly used operators in EL expressions (+ - * % etc.), and logical operators (&& || etc.) rules are equivalent to java.

  JSTL or JSP Standard Tag Library is a collection of custom tag libraries to solve common problems like traversing a Map or collection, conditional testing, xml processing, and even database access and data manipulation.

  To use the jstl tag library, you must enter the jstl-related jar file, and the jsp page needs to have the relevant statement used <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"% >. (ps: The version of the jstl tag library in this blog is 1.2)

  Labels in JSTL1.2 can be divided into 5 types of areas.

area subfunction URL prefix
core Variable support http://java.sun.com/jsp/jstl/core c
flow control
URL management
other
xml core http://java.sun.com/jsp/jstl/xml x
flow control
convert
globalization locale http://java.sun.com/jsp/jstl/fmt fmt
message formatting
Number and date formatting
database SQL http://java.sun.com/jsp/jstl/sql sql
function set length http://java.sun.com/jsp/jstl/functions fn
String manipulation

  Core library

  When the out tag evaluates the expression, it outputs the result to the current JspWriter. <c:out value="value" [escapeXml="{true|false}"] [default="defaultValue"] /> or

  <c:out value="value"  [escapeXml="{true|false}"] >default value </c:out>

  set is used to create a bounded variable

  <c:set target="${adress}" property="city">beijing</c:set> assigns the string "beijing" to the city property of the bounded object address.

  The remove tag is used to remove bounded variables, eg: <c:remove var="job" scope="page" /> removes the page-scoped variable job.

  The if tag is a judgment test for a certain condition,

  <c:if test="testCondition">xxxxx</c:if> where testCondition is boolean type. The syntax is similar to if(xx){xx} in java

  choose, when, otherwise labels choose and when labels are similar to switch case labels in java

  <c:choose>

    <c:when test="${1==1}">xxxx</c:when>

    <c:when test="${1==2}">xxxx</c:when>

    <c:otherwise>xxxx</c:otherwise>

  </c:choose>

  The forEach tag traverses the specified collection of objects.

  <c:forEach var="x" begin="1" end="5">

    <c:out value="${x}" />

       </c:forEach>

  <c:forEach var="phone" items="${address.phone}" varStatus="status">

    ${phone}

  </c:forEach>

  status indicates the current number of traversals.

  (ps:forEach tags can be nested with each other and need to be used flexibly according to actual scenarios.)

  formatting behavior

   JSTL provides tags for formatting and parsing numbers and dates, they are formatNumber, formatDate, timeZone, setTimeZone, parseNumber, parseDate

   formatNumber is used to format numbers,

   formatDate is used to format dates

  function

    The contains function is used to test whether a string contains the specified string, <c:if test="${fn:contians("aaaaa","aa")}"></c:if>,

    endsWith function, used to test whether it ends with a certain string,

    indexOf returns the index of the first occurrence of the specified string,

    join joins the specified string with the specified symbol,

    length returns the length of the string,

    replace replace string,

    split cuts the string,

    substring intercepts the string,

    ......

    (ps: For more functions or label usage, please see the related jstl usage api)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325244616&siteId=291194637