In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use EL expression language in JSP2.0, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
EL (expression language) is a data access language that makes it easy to access and process application data without using scriptlet or the value of a request-time expression. (no need to use
< % 和%>To get the data, EL makes JSP page writers get rid of the java language and makes it easy for users to write JSP programs even if they don't understand JAVA.
Before JSP2.0, web writers could only use expressions
< %= aName %>Access the value of the system, such as:
< someTags:aTag attribute="< %= pageContext.getAttribute("aName") %>">
Or
< % = aCustomer.getAddress().getCountry() %>The expression language allows web writers to access objects using simple syntax. For example, to access a simple variable, you can write something like this:
< someTags:aTag attribute="${aName}">To access the JavaBeans property, you can use: ${aCustomer.address.country}
Tomcat support and configuration of EL extension expressions:
Tomcat5.0 supports EL by default, and users can set the entire web application in the Web.xml file or set the page directive separately in each page to control whether TOMCAT supports EL.
--
Setting of EL extended expression language in JSP2.0
With JSP2.0, you can use EL to extend the expression. For a single JSP page, you can use the define page directive to set whether the jsp page supports EL. EL is supported by default (if the page does not support EL, set it to isELIgnored=true
< %@ page isELIgnored="true|false"%>For the entire JSP application, modify the WEB.XML configuration (tomcat5.0.16 supports EL by default)
< jsp-property-group> < description>For config the ICW sample application
< /description> < display-name>JSPConfiguration
< /display-name> < url-pattern>/ jsp/datareset.jsp
< /url-pattern> < el-ignored>True
< / el-ignored> < page-encoding>ISO-8859-1
< /page-encoding> < scripting-invalid>True
< /scripting-invalid> < include-prelude>/ jsp/prelude.jspf
< /include-prelude> < include-coda>/ jsp/coda.jspf
< /include-coda> < /jsp-property-group>--
Expression operation symbols in JSP2.0:
EL expression operators include arithmetic operators (+-* /), relational operators (>)
< )和逻辑运算符(&& || !),还有empty 判断值是否为空,同时EL能够对字符串,数字等各种类型的数据之间的自动转换,使EL 兼容了JAVASCRIPT等脚本语言的优点,摆脱了JAVA语言严格的数据类型限制,使用起来简单,只需要几分钟就可以轻松掌握(对于不懂java的网页编写人员实在是一件好事)。 ---------------------------- JSP2.0中各种运算符运算实例 以下是EL运算符列表,其中empty可以检查某个值是否为空 运算符 说明 + 加 - 减 * 乘 /或div 除 % 或 mod 模(求余) == 或 = 等于 != 或 != 不等于 < 或 lt 小于 >Or gt is greater than
< =或le 小于等于 >= or ge greater than or equal to
& & or and logic and
| or or logic or |
! Or not logic is not
Empty checks whether it is null or not
A? B: C conditional operator
--
EL expressions are easy to use, and "${}" means EL expressions in a web page. ${1cm 1} is displayed as 2
--
Operation result of EL arithmetic expression in JSP2.0
${1} 1 ${1 + 2} 3 ${1.2 + 2.3} 3.5 ${- 4-2}-6 ${21 * 2} 42 ${10% 4} 2 ${10 mod 4} 2 ${1
< 2} true ${1 lt 2} true ${1 >(4 ge 2)} false ${4.0 > = 3} true ${4.0 ge 3} true ${100.0 eq 100} true ${(10x10)! = 100} false ${(10x10) ne 100} false ${(1Chron2)? 3: 4} 4
--
Built-in 11 implicit objects:
In order to easily obtain the relevant data of WEB applications, EL expression language defines some implicit objects. There are 11 implicit objects in total. (there are 9 implicit objects in JSP and more implicit objects in EL than in JSP, so it is more convenient for EL to obtain data than JSP.) using these objects can easily access Web program data.
The implied objects include Session (to obtain the session value of the current web program), cookie (to obtain the cookie value of the WEB program), header,headerValues (to obtain the user's Http data access header information), Param and ParamValues (to obtain the data parameters submitted by the user), and so on.
You can get this value using the ${implied object name ["element"]}, for example, ${header ("host")} can display the value of host in the http header, ${param ("username")}, you can get the user who shows the user form submission. Use ${empty (param ("username")} to determine whether the form submitted by the user is empty, and so on. (this is much simpler and more convenient than using request.getParamter ("username") in jsp1.2)
--
Implicit object and implied object access instance
The EL expression defines 11 implicit objects, which can be easily read to session,cookie,HttpHeader, user submitted form (param), etc.
Implied object content
A collection of scoped variables within the scope of an applicationScope application
A collection of all the cookie of cookie
Header HTTP request header, string
HeaderValues HTTP request header, string collection
A collection of all initParam application parameter names
PageContext the javax.servlet.jsp.PageContext object of the current page
A collection of all objects within the scope of a pageScope page
A collection of all request parameter strings in param
ParamValues all request parameters as a collection of strings
RequestScope collection of all request-scoped objects
SessionScope collection of all session-scoped objects
The following is an example of getting HTTP access header data and user-submitted data
EL expression operation result
${header ["host"]} get the host value of the HTTP connection header here
${header ["accept"]} get the hit value of HTTP header here
${header ["user-agent"]} get the user-agent value of the HTTP header here
--
Access to application data
In addition to convenient access to built-in hidden objects, EL can also easily access application data, which can be accessed in two ways. (dot operator) access the properties of an object, or you can use [] to access array elements (in fact, implicit object data is accessed in both ways in EL)
The expression ${data} represents a scoped variable named data. You can use the dot (.) or square bracket ([]) operators to retrieve attribute values from the collection: (two data access methods are demonstrated)
The dot operator is used to access named attributes, such as the expression ${customer.name} for the name attribute of the scoped variable customer
The square bracket operator can be used to retrieve named attributes, such as in ${customer ["name"]}. You can also access the * items in the collection customers in the form of ${customers [0]}.
The EL expression language also unifies the handling of dot and square bracket operators, so ${customer.name} is equivalent to ${customer ["name"]}.
--
The following can read the information submitted by the user, assuming that the information submitted by the user is? name=myname&alies=now.net.cn (there are two access methods, one is to use [] to access, the other is to use "." The effect of the two kinds of access is the same.)
${param ["name"]} myname ${param.name} myname ${param ["alies"]} now.net.cn ${param.alies} now.net.cn
--
Accessing application data requires the following steps:
1. Write the static public method of the public class, and only the method of static public can be accessed by the tag expression.
--
Define and use functions in JSP2.0
The expression language allows you to define functions that can be called in expressions. The function must be written as a public static method in the public class. At the same time, map the file to the TLD tag library file.
To illustrate the use of a function, let's give a simple example of adding two numbers. The first step is to write the Java method code for the sum of two numbers, as shown in the code example, where a static method is defined that takes two string parameters, parses them into integers, and returns their sum.
Example file: Compute.java
Package jsp2.examples.el; import java.util.*; public class Compute {public static int add (String x, String y) {int a = 0; int b = 0; try {a = Integer.parseInt (x); b = Integer.parseInt (y);} catch (Exception e) {} return a + b;}}
After compiling this code with javac, the next step is to map the signature of the function to the tag library.
--
two。 Map a method to a TLD table
--
Configuration tag library description file function descriptor
< function> < description>Add x and y
< /description> < name>Add
< /name> < function-class>Jsp2.examples.el.Compute
< /function-class> < function-signature>Int add (java.lang.String,java.lang.String)
< /function-signature> < /function>Now we can write a JSP page to use this function. Code example 5 is a form with two fields. When the user enters two numbers and presses the "sum" button, the above function is called and the two numbers are added, and the results are displayed on the same page.
--
3. Call the Taglib tag to be referenced in the JSP page, and then you can access it in the jsp page.
--
Code example 5: math.jsp
< %@ taglib prefix="my" uri="http://jakarta.apache.org/tomcat/jsp2-example-taglib %> < HEAD> < TITLE>Functions
< /TITLE> < /HEAD> < BODY> < H3>Add Numbers
< /H3> < P> < FORM action="math.jsp" method="GET">X =
< input type="text" name="x" value="${param["x"]}"> < BR>Y =
< input type="text" name="y" value="${param["y"]}"> < input type="submit" value="Add Numbers"> < /FORM> < P>The sum is: ${my:add (param ["x"], param ["y"])}
< /BODY> < /HTML>Thank you for reading this article carefully. I hope the article "how to use EL expression language in JSP2.0" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.