JavaWeb Speedthrough EL and JSTL

Directory

1. EL expression

1. Quick Start :

1.1 Basic introduction

1.2 Getting Started Case

2. Commonly used output formats:

2.1 Create JavaBean class

2.2 Create JSP file

3.empty operator:

3.1 Introduction

3.2 Examples

4. EL object:

4.1 EL11 built-in objects

4.2 Domain Object Demonstration

4.3 Get HTTP information

Two, JSTL tag library

1. Basic introduction:

2.Core core library commonly used tags:

2.1

2.2

2.3

2.4 /c:forEach>


1. EL expression

1. Quick start:

1.1 Basic Introduction

EL The full name of expression: Expression Language, which is Expression Language
The main function of EL expression is to replace the expression script of JSP page <% =request.getAttribute("...") %>

EL expression basic syntax: ${key}, which is more concise in form than traditional JSP expression scripts.

1.2 Getting Started Case

Taking intro.jsp as an example, the code is as follows:

<%--
    User: Cyan_RA9
    Version: 21.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL expression</title>
</head>
<body>
    <%
        request.setAttribute("color", "pink");
    %>
    <%--
        PS: If the attribute is null, when retrieving data, the JSP method returns "null", and the EL expression returns "".
     --%>
    <h1>Traditional JSP takes out data----</h1>
    color : <%= request.getAttribute("color") %> <br/>
    author : <%= request.getAttribute("author") %><br> <%--You can use the ternary operator to optimize--%>

    <h1>Use EL expression to retrieve data----</h1>
    color : ${color} <br/>
    author : ${author}
</body>
</html>

Run results:

2. Commonly used output formats:

2.1 Create JavaBean class

Movie class code is as follows:

package el;

import java.util.List;
import java.util.Map;

/**
 * @author : Cyan_RA9
 * @version: 21.0
 * @what: Standard JavaBean class
 */
public class Movie {
    private String name;
    private int length;
    private List<String> platforms;
    private Map<String, String> comments;

    public Movie() {
    }

    public Movie(String name, int length, List<String> platforms, Map<String, String> comments) {
        this.name = name;
        this.length = length;
        this. platforms = platforms;
        this.comments = comments;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public List<String> getPlatforms() {
        return platforms;
    }

    public void setPlatforms(List<String> platforms) {
        this. platforms = platforms;
    }

    public Map<String, String> getComments() {
        return comments;
    }

    public void setComments(Map<String, String> comments) {
        this.comments = comments;
    }

    @Override
    public String toString() {
        return "Movie{" +
                "name='" + name + ''' +
                ", length=" + length +
                ", platforms=" + platforms +
                ", comments=" + comments +
                '}';
    }
}

2.2 Create a JSP file

The output.jsp file code is as follows:

<%@ page import="el.Movie" %>
<%@ page import="java. util. ArrayList" %>
<%@ page import="java. util. HashMap" %><%--
    User: Cyan_RA9
    Version: 21.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL output</title>
</head>
<body>
    <h1>Output the information of the Movie object</h1>
    <%
        Movie movie = new Movie();
        movie.setName("Matrix");
        movie.setLength(150);

        ArrayList<String> platforms = new ArrayList<>();
        platforms.add("Bilibili");
        platforms.add("YouTube");
        platforms.add("Youku");
        movie. setPlatforms(platforms);

        HashMap<String, String> comments = new HashMap<>();
        comments. put("Cyan", "It's a so nice film!");
        comments. put("Rain", "I like it!");
        comments. put("Ice", "Pretty good!");
        movie. setComments(comments);

        //Put the movie object into the request domain object
        request.setAttribute("movie", movie);
    %>
    <%-- Get data --%>
    Movie.name = ${movie.name}<br/>
    Movie.length = ${movie.length}<br/>

    Movie.platforms = ${movie.platforms}<br/>
    Movie. platforms[2] = ${movie. platforms[2]}<br/>
    Movie.platforms[2] = ${movie.platforms.get(2)}<br/>

    Movie.comments = ${movie.comments}<br/>
    Movie.comments_Cyan = ${movie.comments.get("Cyan")}<br/>
    Movie.comments_Rain = ${movie.comments["Rain"]}<br/>
</body>
</html>

Run result:

3.empty operator:

3.1 Introduction

The empty operation can judge whether a data is empty, if it is empty, return true, otherwise return false

2° The following situations are empty–

● value = null

● value = “”

● value = new Object[] {} (Object array with length 0)

● List collection with 0 elements

● A Map collection with 0 elements

3.2 Examples

The empty.jsp code is as follows:

<%@ page import="java.util.ArrayList" %>
<%@ page import="java. util. HashMap" %><%--
    User: Cyan_RA9
    Version : 21.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>empty expression</title>
</head>
<body>
    <%
        request.setAttribute("key1", null);
        request.setAttribute("key2", "");
        request.setAttribute("key3", new Object[]{});
        request.setAttribute("key4", new ArrayList<>());
        request.setAttribute("key5", new HashMap<>());
    %>
    <%-- judge whether it is empty --%>
    Whether key1 is empty: ${empty key1} <br/>
    Whether key2 is empty: ${empty key2} <br/>
    Whether key3 is empty: ${empty key3} <br/>
    Whether key4 is empty: ${empty key4} <br/>
    Whether key5 is empty: ${empty key5}
</body>
</html>

Run results:

4.EL object:

4.1 EL11 built-in objects

As shown in the table below:

Variable Type Description
pageContext PageContextImpl Get the nine major JSP Built-in objects
pageScope Map Get data in pageContext domain
requestScope Map Get the data in the request field
sessionScope Map Get the data in the Session field
applicationScope Map Get the object in the ServletContext domain
param Map Get the value of the request parameter
paramValues Map Get multiple values
header Map Get request header information
headerValues Map Get multiple information of the request header
cookie Map Get the cookie information of the current request
initParam Map Get the < configured in web.xml context-param>Context parameters

4.2 Domain Object Demonstration

elScope.jsp code is as follows:

<%--
    User : Cyan_RA9
    Version : 21.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL scope</title>
</head>
<body>
    <%
        pageContext.setAttribute("color", "cyan");
        request.setAttribute("color", "pink");
        session.setAttribute("color", "cornflower_blue");
        application.setAttribute("color", "lightyellow");
    %>
    pageContext domain_color = ${pageScope.color} <br/>
    request domain_color = ${requestScope.color} <br/>
    session domain_color = ${sessionScope.color} <br/>
    ServletContext domain_color = ${applicationScope.color}
</body>
</html>

Run result:

4.3 Get HTTP information

http.jsp code is as follows:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>pageContext demo</title>
</head>
<body>
<h1>pageContext object obtains HTTP information</h1>
<hr/>
<% //Simplified form (put the request domain object into the pageContext domain) --- JSP
    pageContext.setAttribute("req", request);
%>
Protocol: ${ pageContext.request.scheme }<br>
Protocol: ${ req.scheme }<br>
Server ip: ${ req.serverName }<br>
Server port: ${ req.serverPort }<br>
Project path: ${ req.contextPath }<br>
Request method: ${ req.method }<br>
Client ip address: ${ req.remoteHost }<br>
session id: ${ pageContext.session.id }<br>
</body>
</html>

Run result:

2. JSTL tag library

1. Basic introduction:

JSTL tag library refers to JSP Standard Tag Library, is the JSP standard tag library.



EL expression is to replace the expression script <%=%> in JSP,
JSTL
then
is to replace the code script <%%> in JSP
.



JSTL consists of five tag libraries —

Function Area URI prefix
core tag library http ://java.sun.com/jsp/jstl/core c
Formatting http://java.sun.com/jsp/jstl/fmt fmt
Function http://java.sun.com/jsp/jstl/functions fn
Database (not used) http://java.sun.com/jsp/jstl/sql sql
XML (not used) http ://java.sun.com/jsp/jstl/xml x

Using JSTL requires importing jar packages (impl and spec). PS: The dependency of Tomcat10 on jar packages has changed(jakarta). After importing the jar package, restart Tomcat. The URL to download the jar package is as follows:

Apache Taglibs – Apache Standard Taglib: JSP[tm] Standard Tag Library (JSTL) implementations

taglibIntroduce tags, put them at the beginning of the line.

2. Common tags of core library:

“> 2.2

(1) tag is used to do
if
Judgement.

(2)
The test attribute indicates the conditions for judgment
(Use
EL
Expression output)

(1) tagused
Iterate through the output,
There are mainly four traversal forms–

● Ordinary traversal outputs i to j (front-closed and back-closed)

● Iterate over the array

● Traversing the Map

● Traversing the List

(2)
Related attributes–

1> Normal traversal:

The begin attribute sets the index at which to start

end attribute setting end index

The step attribute indicates the step value of the traversal
The var attribute represents the variable of the loop (also the data currently being traversed)

2> Traverse the array:

items the collection of traversed
The data traversed by var (similar to enhanced for; output will automatically lock the domain)


eg :

<%
request.setAttribute(“books”, new String[]{“Harry Potter”, “Lord of the Rings”});
%>


items
=”${requestScope.books}”
var
=”book”>

bookName = ${book}

3> Traverse the Map collection:

eg:

<%
Map students = new HashMap<>();

map. put(“Cyan”, “425”);

map. put(“Rain”, “400”);

map. put(“Ice”, “430”);

request. setAttribute(“students”, students);
%>

Student’s Info =
${student.key}

${student. value}

4> Traverse the List collection:
ΔTraversing the List collection is similar to the steps when traversing the Map collection, both of which are like the enhanced for loop structure in Java.
The varStatus attribute indicates the status of the currently traversed data, and you can get attribute values such as step, begin, end, etc.

PS :
The corresponding JavaBean class needs to provide the corresponding getXxx method
.

System.out.println(“END————————————— ————————————————– —————————–“);

syntaxbug.com © 2021 All Rights Reserved.