Servlet-EL expressions and JSTL tags

EL expression

The role of EL expression: EL expression is mainly to replace the expression script in the jsp page for data output in the jsp page. Because EL expressions are more concise and multi-
format $(expression) when outputting data than jsp expression scripts

<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body >

    <%
        request.setAttribute("key","值");
    %>

    <%=request.getAttribute("key")==null?"":request.getAttribute("key")%>
    %{
    
    key}
</body>
</html>

EL expression is mainly to output data in jsp page, mainly to output data in domain (request, session, application, pageContext) objects.
When the four fields have the same key data, the EL expression will search in the order of the four fields from small to large, and output when found.

EL expression can output common attributes of Bean, array attributes, List collection attributes, map collection attributes.

<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.atguigu.java.Person" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body >

<%
    Person person = new Person();
%>

    ${
    
    person.name}
    ${
    
    person.Map}
    ${
    
    person.list}
</body>
</html>

The same EL expression also supports relational operations, logical operations, arithmetic operations, empty operations, dot operations and bracket operations

Eleven implicit objects in EL expressions

  • pageContext pageContextImpl can get nine built-in objects in jsp
  • pageScope Map<String,Object> It can get the data in the pageContext domain
  • requestScope Map<String,Object> It can get the data in the Request field
  • sessionScope Map<String,Object> It can get the data in the Session field
  • applicationScope Map<String,Object> It can get the data in the ServletContext domain,
  • param Map<String,String> It can get the value of the request parameter
  • paramValues ​​Map<String,String[]> It can get the value of the request parameter, which is used when getting multiple values
  • header Map<String,String> It can get the information of the request header
  • header Map<String,String[]> It can get the information of the request header
  • initParam Map<String,String> It can get the context parameters configured in web, xml

·EL gets the properties
pageScope, requestScope, sessionScope, applicationScope in 4 specific domains
${requestScope.key2}

The pageContext object is often used to obtain information

  1. protocol
  2. Server ip
  3. Server port
  4. Get project path
  5. Get request method
  6. Get client ip address
  7. Get the id number of the reply
<%@ page import="java.util.Map" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/1/16
  Time: 12:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%=request.getScheme()%>
    ${
    
    pageContext.request.scheme}<!--EL表达式不用谢get-->
</body>
</html>


JSTL tags

JSTL tag library is a continuously improving JSP tag library.
EL expression is mainly to replace the expression script in jsp, and the tag library is to replace the code script. This makes the entire jsp page more concise.

Insert picture description here

语法
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>

The core tag
<c:out> is used to display data in JSP, just like <%=…>
<c:set> is used to save data
<c:remove> is used to delete data
<c:catch> is used to handle errors abnormal conditions, and the error message is stored
<c: if> with us if used in the same general procedure
<c: choose> itself only as a <c: when> and <c: otherwise> parent tag
<c: when> The sub-tag of <c:choose> is used to determine whether the condition is established.
<c:otherwise> The sub-tag of <c:choose> is connected after the <c:when> tag, when the <c:when> tag is judged to be Executed when false
<c:import> retrieves an absolute or relative URL, and then exposes its content to the page
<c:forEach> Basic iteration tags, accepts multiple collection types
<c:forTokens> Separate the content according to the specified separator And iteratively output
<c:param> to pass parameters to the page that contains or redirect
<c:redirect> to redirect to a new URL.
<c:url> uses optional query parameters to create a URL
formatting tag
fmt:formatNumber uses the specified format or precision to format the number
fmt:parseNumber Parse a string representing a number, currency or percentage
fmt:formatDate Format the date and time using a specified style or pattern
fmt:parseDate Parse a string representing a date or time
fmt:bundle bind resource
fmt:setLocale specify region
fmt:setBundle bind resource
fmt:timeZone specify time zone
fmt:setTimeZone specify time zone
fmt:message display resource configuration file information
fmt:requestEncoding set the character encoding of request

SQL label
sql:setDataSource Specify the data source
sql:query Run SQL query statement
sql:update Run SQL update statement
sql:param Set the parameter in the SQL statement to the specified value
sql:dateParam Set the date parameter in the SQL statement to the specified java The .util.Date object value
sql:transaction provides nested database behavior elements in a shared database connection, and runs all statements as a transaction. The
XML tag
<x:out> is similar to <%=… >, but only Used for XPath expression
<x:parse> Parse XML data
<x:set> Set XPath expression
<x:if> Judge the XPath expression, if it is true, execute the content in the ontology, otherwise skip the ontology
<x: forEach> Iterate over the nodes in the XML document
<x:choose> <x:when> and the child tags of the parent tags of
<x: otherwise> <x:when> <x:choose>, used to make conditional judgments
<x:otherwise > The subtag of <x:choose> is executed when <x:when> is judged to be false.
<x:transform> The XSL transformation is applied to the XML document.
<x:param> is used together with <x:transform>. To set the XSL style sheet

JSTL function
fn:contains() test whether the input string contains the specified substring
fn:containsIgnoreCase() test whether the input string contains the specified substring, case-insensitive
fn:endsWith() test whether the input string End with the specified suffix
fn:escapeXml() skips characters that can be used as XML tags
fn:indexOf() returns the position where the specified string appears in the input string
fn:join() combines the elements in the array into a string and then Output
fn:length() Return the length of the string
fn:replace() Replace the specified position in the input string with the specified string and then return
fn:split() Separate the string with the specified separator and form a sub-character String array and return
fn:startsWith() test whether the input string starts with the specified prefix
fn:substring() returns a subset of the string
fn:substringAfter() returns the subset of the string after the specified
substring fn:substringBefore( ) Returns the subset of the string before the specified substring
fn:toLowerCase() Converts the characters in the string to lowercase
fn:toUpperCase() Converts the characters in the string to uppercase
fn:trim() Removes the leading and trailing blanks symbol


For example

<%@ page import="java.util.Map" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.atguigu.java.Person" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/1/16
  Time: 12:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        ArrayList<Person> people = new ArrayList<>();
        for(int i = 0;i < 10;i ++){
    
    
            people.add(new Person("1","username" + i,"pass" + i));
        }
        request.setAttribute("person",Person);
    %>

    <c:forEach begin="2" end="7" step="2" varStatus="status" items="${requestScope.stus}" var="stu">
        <tr>
            <td>${
    
    stu.id}</td>
            <td>${
    
    stu.username}</td>
            <td>${
    
    stu.password}</td>
            <td>${
    
    status.first}</td>
        </tr>
    </c:forEach>
</body>
</html>

Guess you like

Origin blog.csdn.net/m0_46656833/article/details/112724751