Dynamic page technology EL, dynamic page el
1. EL Overview:
EL expressions can be embedded inside JSP pages to reduce the compilation of JSP scripts.
EL appears to replace the compiling of scripts on JSP pages.
EL's most important role is to retrieve data from the domain:
Introduction:
<% = Request. getAttribute (name); %>
EL only needs to write like this: $ {requestScope. name}
Details:
Create a User class:
package domain;public class User { private int id; private String name; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
View Code
Create a JSP file as an example:
Prepare some data:
<! -- Simulate data in the Domain --> <% pageContext. setAttribute ("company", "Microsoft"); // stores the string request. setAttribute ("company", "Microsoft"); // stores an Object User user = new User (); user. setId (1); user. setName ("zhangsan"); user. setPassword ("123"); session. setAttribute ("user", user); // store a collection List <User> list = new ArrayList <User> (); User user1 = new User (); user1.setId (2); user1.setName ("lisi"); user1.setPassword ("123"); list. add (user1); User user2 = new User (); user2.setId (3); user2.setName ("wangwu"); user2.setPassword ("123"); list. add (user2); application. setAttribute ("list", list); %>
View Code
JSP retrieves the value in the field:
<! -- JSP retrieves the value in the field --> <% = request. getAttribute ("company") %> <% User sessionUser = (User) session. getAttribute ("user"); out. write (sessionUser. getName (); %>
EL mode:
<! -- Use the EL expression to obtain the value in the field -- >$ {requestScope. company }$ {sessionScope. user. name }$ {applicationScope. list [1]. name}
Global Search (commonly used ):
<! -- Use the el expression for global search -- >$ {company }$ {user. name }$ {list [1]. name}
The global search sequence is the same as that of findattricontext: pageContext-> request-> session-> application
EL built-in object:
These are outdated and are not used in actual development.
Pay attention to the last one: other objects such as request and response can be obtained.
Example: $ {pageContext. request. contextPath}
EL execution expression:
<! -- El can execute expression operations -- >$ {1 + 1 }$ {1 = 1? True: false} <! -- Empty: determines whether an object is null. If it is null, true is returned. -- >$ {empty list}