Detailed tutorial on Java Web practice (XXIII) JSTL tags and El expressions

Miss Zhu 2022-02-13 08:36:58 阅读数:628

detailed tutorial java web practice

         Why use JSTL and EL expression ?
         Let's show the information of department employees through a table , Compare JSP and JSTL Different .
 Insert picture description here
         The same loop generates a table data , Using tradition jsp:
 Insert picture description here
         The same loop generates a table data , Use JSTL The way :
 Insert picture description here

EL The full name is Expression Language, The expression language .

Use loose type conversion , Simplify access to variables and objects .
EL The grammatical structure of is surface reach type , Than Such as say { expression } , for instance surface reach type , Than Such as say {emp.name}
The above variables emp Will follow from page、request、session、application Look for... In turn ,
If you find , Then stop looking for ; If not found , Then return to null
You can also set the range of variables to look for , The attribute range corresponding to the above four built-in objects is EL The writing format in is :pageScope,requestScope,sessionScope,applicationScope
EL Arithmetic operators can also be used in expressions 、 Relational operator 、 Logical operators .
for example :

${
4+5}
${
3<4}
${
3!=4&&5>6}

EL Use :
 Insert picture description here
above EL In fact, it is equivalent to <%=request.getAttribute(“age”)%>. But if so :${sessionScope.age}, from session In the range , You can't get .

JSTL The full name is JSP Standard Tag Library, namely JSP Standard label library

EL Expressions simplify the process of outputting information JSP Complexity , But it can't control the process 、 Unable to handle complex logic . Is there a technology , You don't have to write Java Code , And can control the process ?
JSTL Provides a method that does not need to be embedded Java code snippet , You can develop ways to display complex pages
JSTL Provides universal 、 Conditions 、 loop 、 format 、 Database operation and other labels
Use JSTL label , To add the following label to the instruction :

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

JSTL label —— Universal label
<c:out>
The function is to output , Function like <%= Variable %>、 change The amount language Law by : < c : o u t v a l u e = “ word operator strand often The amount ” / > or < c : o u t v a l u e = “ { Variable } The grammar is :<c:out value= “ String constant ”/> or <c:out value= “ change The amount language Law by <c:outvalue= word operator strand often The amount /> or <c:outvalue={ Variable }” />
<c:set>
The function is to define variables , Store the value of the variable in a JSP In the range , Or is it JavaBean Properties of .
<c:remove>
The functions and set contrary , Remove a variable from a range .

JSTL label —— Condition label
<c:if>
The function is to judge
<c:if test= “ Judge the condition ”>
</c:if> Insert picture description here
JSTL label —— Iteration tags
<c:forEach>
The function is to iterate over a collection containing multiple objects , Follow Java The unary cycle in is similar
<c:forEach [var= “ The current element ”] items= “ aggregate ”>
</c:forEach>
 Insert picture description here
JSTL label —— format
<fmt:formatDate value="${ date }" pattern=“yyyy-MM-dd” />
<fmt:parseDate value=" Date string "pattern=“yyyy/MM/dd”/>
 Insert picture description here
JSTL Labels and EL Expression summary :

  • JSTL And EL Use it with , It can simplify the development of view layer .
  • Need to master EL Expression usage .
  • Need to master common, such as general 、 Conditions 、 iteration 、 Formatting and other labels .
  • Database tags because they were later used MVC Architecture design , Therefore, in actual use, the display layer technology will not be used to access the database , So there is no need to learn database tags .
copyright:author[Miss Zhu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130836551099.html