The ultimate goal of both EL expressions and JSTL tags is to eliminate Java code in JSPS, explicit Java code, of course

El expressions were created to simplify reading data and writing pages in JSPS.

El expressions have few features and are easy to remember

Read the value from the domain object and write it to the page.

 

<%
    pageContext.setAttribute("pk", "1");
    request.setAttribute("rk", "2");
    session.setAttribute("sk", "3");
    application.setAttribute("ak", "4");
%>

${pageScope.pk}
${requestScope.rk}
${sessionScope.sk}
${applicationScope.ak}
Copy the code

We use scope.key to get the value directly from the domain object

We can also omit the scope

<%
    pageContext.setAttribute("key", "1");
    request.setAttribute("key", "2");
    session.setAttribute("key", "3");
    application.setAttribute("key", "4");
%>

${key}
Copy the code

Note that if we omit the scope, the expression queries for matching keys in the order pageScope >requestScope >sessionScope >applicationScope. If there is a domain query. No other domains are being queried.

No exception will be thrown if it does not get it

Next we use el expressions to manipulate data of type Object

public class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}Copy the code
<% request.setAttribute("user", new user ("user", 22)); %> ${requestScope.user.name} ${requestScope.user.age}Copy the code

Pay attention! If you read the properties of an object. Then the object must implement the property corresponding to the get() method. Otherwise throw exception!!

El expressions also support manipulation of data

${(1+2*3+1)/ %3} ${(1+2*3+1)/ %3}Copy the code
<%-- relational operation --%> ${1>4} <%--> >= < <= =! = gt ge lt le eq ! =--%> // We don't usually use symbols, we use characters instead. Prevent collisions with HTML tagsCopy the code
< % - logic - % > ${true | | false}Copy the code
<%-- ternary expression --%> ${3>1? > ": < "}Copy the code

Other uses of the EL expression

Read request parameters

<%-- get the value in the request parameter --%> ${param.age}Copy the code

If the argument is an array type

< % - the value is a list of arguments - % > ${paramValues. Age [0]} ${paramValues. Age [1]}Copy the code

JSTL (Core Class Library)

Two jars need to be imported

JSTL. Jar standard. The jar

The < % @ taglib uri = “java.sun.com/jsp/jstl/co…” prefix=”c”%>

Import label constraint Specifies the label library namespace

The first is to set the domain properties

<c:set scope="session" var="cskey" value="c tag set session"/>// set attribute <c:remove scope="session" Var = "cskey" / > / / remove the attributeCopy the code

Scope is domain scope: page, request, session, application. Var is a property name. The value is the attribute value

If statement, note that only el expressions can be used in test

<c:if test="${3>2}">
    3>2
</c:if>
Copy the code

If the else statement

<c:choose>
    <c:when test="${crage==1}">
        when1
    </c:when>
    <c:when test="${crage==2}">
        when2
    </c:when>
    <c:otherwise>
        when3
    </c:otherwise>
</c:choose>
Copy the code

Looping statements

The < c: forEach var = "I" begin = "1" end = "5" step = "1" > current loop: ${I} < / c: forEach >Copy the code

Var Specifies the name of the current variable,begin,end, and end. It’s also worth noting that variables declared in var are stored in the pageContext field. That’s why the expression el can be evaluated

Set of cyclic operations

<% List<User> users = new ArrayList<User>(); Users.add (new User(" 三",1)); Users.add (new User(" User ",2)); Users.add (new User(" wang5 ",3)); request.setAttribute("users",users); % >Copy the code
< c: forEach items = "${users}" var = "user" > cycle users: ${user. The name} ${user. Age} < / c: forEach >Copy the code

Loop operation map

<% Map<Integer,User> maps = new HashMap<>(); Maps. put(1,new User(" zhang SAN ",1)); Maps. Put (2,new User(" lI Si ",2)); Maps. Put (3,new User("王五",3)); session.setAttribute("maps",maps); % >Copy the code

Var stores a key-value pair. In el, key is the value of the map. Value is the value of the map

<c:forEach items="${sessionscope. maps}" var="entry"> Loop user Map:${entry.key}${entry.value.name}${entry.value.age} </c:forEach>Copy the code

These are common operations for EL expressions and JSTL tags.