EL expression

The basic concept

  • EL (Expression Language) expressions provide a way to simplify expressions in JSPS, making it easy to access and output various kinds of data.

The main function

  • Access data stored in pageContext, Request, Session, and Application scoped objects in sequence.
  • Gets the request parameter value.
  • Access the properties of the Bean object.
  • Access the data in the collection.
  • Output simple operation results.

Access the data of built-in objects

access

  • <%=request.getAttribute(“varName”)%>
  • ${varName} ${varName}

Execute the process

Access the data for the request parameters

  • Request.getparameter (name) = request.getParameter(name); request.getParameterValues(name);
  • The data param of the request parameter is accessed in EL using the following method: the parameter is received with a single value. ParamValues: The accepted parameter has multiple values.
<! ${param.name} <! ${paramvalues.hobby [--> ${paramvalues.hobby [--> ${paramvalues.hobby [0]}
Copy the code

Access the properties of the Bean object

access

  • Method 1: Object name. Attribute name, for example: {object name. Attribute name}, for example: object name. Attribute name, for example: {user.name}
  • Method 2: Object name [” Attribute name”], for example: {object name [” Attribute name”]}, for example: {user[“name”]}

The main difference

  • Use [] instead of alphanumeric characters when accessing property names that contain special characters such as. Or, etc. The way of
  • You can use [] to dynamically value the value as follows:
<% 
    request.setAttribute("prop"."age"); % > <! ${user[prop]} ${user[prop]}Copy the code

Access the data in the collection

<! --> ${student[0].name}
Copy the code

Commonly used built-in objects

category identifier describe
JSP pageContext PageContext processes the current page
scope pageScope Map classes associated with page scoped property names and values
requestScope The Map class associated with the name and value of the request scope attribute
sessionScope The Map class associated with the name and value of the session scope property
applicationScope Map classes associated with the names and values of the application scope properties
Request parameters param Map class that stores the values of the request parameters by name
paramValues A Map class that stores all the values of the request parameters as a String array
Request header header Map class that stores the main value of the request header by name
headerValues A Map class that stores all the values of the request header as a String array
Cookie cookie Map class that stores the cookies that come with the request by name
Initialization parameter initParam Map class that stores Web application context initialization parameters by name

Commonly used operators

A commonly used arithmetic operator

Arithmetic operator Said Ming Fan case Operation results
+ add The ${1 + 2} 3
Reduction of 1} ${2 – 1
* take 2 * ${3} 6
/ or div In addition to 16/5 ${} or ${16 div5} 3.2
% or mod Take more than The ${5} 16% or ${16 mod5} 1

Commonly used relational operators

Arithmetic operator Said Ming Fan case Operation results
= = or eq Is equal to the ${1==2} or ${1 eq 2} false
! = or ne Is not equal to The ${2! ${1 ne 2} true
< or lt Less than ${2<3} or ${2 lt 3} true
> or gt Is greater than ${16>5} or ${16 gt 5} true
< = or le Less than or equal to ${16<=5} or ${16 le 5} false
> = or ge Greater than or equal to ${16>=5} or ${16 GE 5} true

A common logical operator

Arithmetic operator Said Ming Fan case Operation results
&& or the and With the operation ${true&&true} or ${true and true} true
| | or or Or operation Or $${true | | false} {true or false} true
! Or not The operation The ${! True} or ${not true} false

Conditional operator

${conditional expression? Statement 1: Statement 2}Copy the code

Validation operator

${empty expression} returns a Boolean value to determine whether the expression is "empty". Null values, collections or arrays without elements, and strings of zero length are considered null.Copy the code

JSTL tags

The basic concept

  • JSTL(JSP Standard Tag Library) is called the JSP Standard Tag Library.
  • Developers can use these tags to replace Java code on JSP pages, making programs more readable and less difficult to maintain.

use

  • Download JSTL jars and added to the project, download address is: tomcat.apache.org/download-ta…
  • Import the JSTL tag library using the taglib specification in the JSP page by:
<! The -- prefix attribute is used to specify the library prefix --> <! The uri attribute is used to specify the library's identity --> <% @taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"% >Copy the code

Common core tags

The output label

<c:out></c:out> The label used to output the specified contentCopy the code

Set up the label

<c:set></c:set> The label used to set the value of the property rangeCopy the code

Remove the label

<c:remove></c:remove> Is used to remove the label of the specified dataCopy the code

Single condition judgment tag

<c:if test ="EL conditional expression ">Copy the code

Multi-conditional judgment tag

<c:choose > <c:when test ="EL expression "> </c:when>... <c:otherwise> Execute </c:otherwise> </c:choose > when the above conditions are not metCopy the code

Loop label

<c:forEach var=" loop "items=" collection "> </c:forEach>Copy the code

Common function labels

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Copy the code

Common Formatting labels

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
Copy the code

Custom labels

  • If the above tags do not meet the requirements, the programmer can also customize the tags as follows:
  • Write a tag class that inherits the SimpleTagSupport or TagSupport class and overwrites the doTag or doStartTag methods.
public class HelloTag extends SimpleTagSupport { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void doTag() throws JspException, IOException { JspWriter out = this.getJspContext().getOut(); Out. println(" Custom tag argument: "+ name); }}Copy the code
  • Define the tag library file (TLD tag library file) and configure the tag description file to web-INF:
<tag>
    <name>helloTag</name> 
    <tag-class>com.lagou.demo02.HelloTag</tag-class> 
    <body-content>empty</body-content> 
    <attribute> 
        <name>name</name> 
        <required>true</required> 
    </attribute> 
</tag>
Copy the code
  • Add the taglib directive to the JSP to introduce the tag library using:
<%@ taglib prefix="hello" uri="http://lagou.com" %>
Copy the code