1 EL expression
EL expression provides a simplified expression in JSP method, can easily access a variety of data and output, the main functions are:
- Access the data stored in the pageContext, Request, Session, and Application-scoped objects in sequence
- Get request parameters
- Access the properties of the Bean object
- Access the data in the collection
- Output simple operation results
1.1 Accessing data of built-in objects
The syntax for traditional access is: <%= request.getAttribute(“varName”)%>
${varName} ${varName}
1.2 Accessing data for request parameters
Access request parameter data need to use the traditional way: request. The getParameter (name) or request. GetParameterValue (name)
To access request parameters using an EL expression, use the following commands: Param accepts only one value for parameters. ParamValues accepts multiple values for parameters
${param.name}
${paramValues.hobby[0]}
Copy the code
1.3 Accessing properties of Bean objects
There are two ways to access a Bean object in an EL expression:
- Object name. Property name, for example {object name. Attribute name}, for example, object name. Attribute names, such as {user.name}
- Object name [” attribute name “], for example [” attribute name “]} {object name, such as object name (” attribute name “), such as {user} [” name “]
The difference between these two methods is that when the attribute name to be accessed contains some special characters, such as:. For non-subtitle or numeric symbols, use [] instead of. The way of
In addition, the [] mode can also be used to dynamically value the value, as follows:
<%
request.setAttribute("prop"."age"); % > <! ${user[prop]} ${user[prop]}Copy the code
1.4 Accessing the data in the collection
<! --> ${student[0].name}
Copy the code
Built-in objects commonly used in 1.5 EL expressions
category | identifier | describe |
---|---|---|
JSP | pageContext | PageContext processes the current page |
scope | pageScope | Map classes associated with page scoped property names and values |
requestSCope | Map class associated with request scope attribute names and values | |
sessionScope | Map classes associated with session scoped property names and values | |
applicationScope | Map classes associated with the names and values of the program scope properties | |
Request parameters | param | Map class that stores the values of the request parameters by name |
paramValue | 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 |
headerValue | A Map class that stores all the values of the request header as a String array class | |
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 |
1.6 Common Operators
Common arithmetic operators are + addition, -subtraction, * multiplication, div or/division, and % or mod mod, which are the same as Java arithmetic operators
Common relational operators are: == equals,! = Not equal to, < less than, > greater than, <= less than or equal to, and >= greater than or equal to
Commonly used logical operators are: && and operation, | | or operation and! The operation
The conditional operator is: ${conditional expression? Statement 1: Statement 2}, similar to the ternary operator in Java
Validation operator: ${empty expression} returns a Boolean value that determines whether the expression is null or null, a collection or array without elements, and a string of length 0
2 JSTL tags
JSTL is a LIBRARY of JSP identification tags that developers can use to replace Java code on JSP pages, making programs more readable and easier to maintain.
Before using JSTL, you need to import the JSTL JAR package and import the JSTL tag library in the JSP page with the taglib specification as follows:
<% taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Copy the code
Here are some common tags used in JSTL:
The output label
<c:out></c:out>Prints the specified contentCopy the code
Set up the label
<c:set></c:set>Sets the standard property range valueCopy the code
Remove the label
<c:remove></c:remove>Delete specified dataCopy the code
Single condition judgment tag
<c:if test="EL conditional expression">Conditional execution</c:if>
Copy the code
Multi-conditional judgment tag
<c:chose>
<c:when test="EL conditional expression">Conditional execution</c:when>...<c:otherwise>If all the preceding conditions are not met, the command is executed</c:otherwise>
</c:chose>
Copy the code
Loop label
<c:forEach var="Cyclic variable" items="Set">.</c:forEach>
Copy the code
Common function tags
The < % taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" > % import functionCopy the code
Common formatting labels
The < % taglib prefix = "FMT" uri = "http://java.sun.com/jsp/jstl/fmt" > % import functionCopy the code
Of course, in addition to the above tags, we can also customize the tag, the steps are 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(a){ return name; } public void setName(String s){ name=s; } @Override public void doTag(a) throws JspEXception,IOException{ JspWriter out=this.getJspContext().getOut(); out.println("The parameters of the custom tag are :"+name); }}Copy the code
-
Define the tag library file and configure the tag description file in web-INF
<tag> <name>helloTag</name> <tag-class>com.bjx.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 JSP to introduce the tag library to use
<% taglib prefix="hello" uri="http://bjx.com" %> Copy the code