Use of El Expression language in JSP pages

Source: Internet
Author: User

The limitation of JavaBean in JSP
Gets the JavaBean property that must be instantiated
Forced type conversions

<%    Employee employee = (employee) request.getattribute ("employee");    Computer comp = Employee.getcomputer ();    String manufacturer = Comp.getmanufacturer ();%>

If the page is written with a logic error, it will cause code execution errors, and the JSP page will generate errors when compiling the page


Solutions
Use El expressions to simplify

What is El
El is expression Language (expressions language)
El's function
Replace complex code in a JSP page
El's Syntax
${EL Exprission}
${Bean.name} or ${bean[' name '}

${bean.name} is essentially the GetName () method that invokes the bean

Note: 1 El through. and [] to access data. For example ${applicationscope.user.username} or ${applicationscope.user["UserName"}

2 If the accessed property name contains some special symbols, such as "." or "-" such as non-alphanumeric or numeric symbols must be used []

3 applicationscope.user["User-name"]

4 If you use variables to pass in, you can only use []. For example

<%string Data="username";%>

${applicationscope.user[data]}

5 Of course there are four hidden objects: Applicationscope,sessionscope,requestscope,pagescope. With application, Session,request, PageContext corresponds to. For example, Applicationscope to get the value of a application range of properties

Automatic conversion type
An El can automatically convert a type when a data is obtained
More lenient restrictions on type
Easy to use
The EL application is simpler compared to embedding Java code in the JSP



El syntax



Start with "${" and End With "}"
Get the value directly using the variable name $
${Username}



Variable Property Scope name

Name in the property range El
Page Pagescope, for example ${pagescope.username}, indicates that a username variable is found in the page range and no return null
Request Requstscope
Session Sessionscope
Application Applicationscope


El operator
Operator "[]"
Operator "."
Role
Get Object Properties




Gets the data in the collection of objects
A (list) collection is saved in session users



Simple application: Output name display

<%
Map names = new HashMap ();
Names.put ("One", "Liyang");
Names.put ("Both", "Wanghua");
Request.setattribute ("names", names);
%>
Name:${names.one}<br/>
Name: ${names["}<br/>"



An introduction to El Implicit objects


Object Name description
Pagescope Returns the variable name of the page range, which is mapped to the corresponding value
Requestscope Returns the name of the variable for the requested range, which is mapped to the corresponding value
Sessionscope Returns the variable name of the session scope, which is mapped to the corresponding value
Applicationscope Returns the variable in the applied range and maps the variable name to the corresponding value
Param Returns the string value of the client's request parameter
Paramvalues Returns a set of values that map to the request parameters of the client
PageContext provides access to user requests and page information



Use of El Expression language in JSP pages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.