




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、单位代码 01 学号 082501170 分 类 号 TP315 密 级 文献翻译院(系)名称 专 业名 称 计算机科学与技术学 生姓 名 指 导教 师 2012 年 3 月 10 日黄河科技学院毕业设计(文献翻译) 第 17 页 汉语译文:这些秘决通过使用Java2企业版1.4标准开发包开发的。你能在htttp:/JSP 2.0语言2003年12月22号题目为Java服务器页标准标签库的技术秘决展示了怎样使用JSP 2.0的新的特点中的一些,包括新的表达式语言。以下的秘决涵盖了这种表达式语言更多的细节。伴随这个秘决的示例代码是一种包含了一个JSP页的网络应用。该页展示了表达式和一些奇特条件的
2、几种类型。为什么用另一种语言呢?JSP 2.0之前,你可以在JSP页输出中使用只有一个脚本段,JSP表达式,或者包括服务器状态的自定义标记。尽管这些解决措施是有用的,但是他们需要一个相对大量工作,甚至是简单的工作访问服务器端状态。JSP2.0包含了首先在JSTL1.0.EL被介绍的表达式语言,它可以更加容易的整合服务器端状态与演示输出。让我们开始快速的审查脚本段,JSP表达式和自定义标签。考虑到Web应用程序,它包含了在一个类 ServerStats的对象中使用统计的信息。类ServerStatse有一个方法getUserCount,这种方法返回一个当前用户登录的号码。这种应用程序包含了一个在
3、ServletContext属性中名叫stats的ServerStats的实例。这儿有一个你怎样用脚本段,一个JSP表达式和自定义标签创建用法统计输出的比较。创建一个脚本段输出:使用JSP版本1,你能打印使用脚本段进行用户登录到系统的这个号码。任何输出脚本段程序不得不明确的被写到输出流中。一个JSP页可以通过以下的脚本段和模板文本来报告这个用户号码:· There are currently <%· ServerStats ss =· (ServerStats)application.getAttribute("stats");·
4、; out.print(ss.getUserCount();· %> userslogged on.创建一个JSP表达式的输出:在使用一个JSP表达式时你能靠置换代码来简化东西。此表达式文本是被事先评测的,和结果是被强迫为字符串输出。这个表达式结果替换了在JSP页输出时的表达式标签。写这个结果文本到JSP页输出是隐含的。· There are currently.· <%= (ServerStats)(application.getAttribute("stats").· getUserCount() %> user
5、s logged on.去写有点简单,但是去读和保持仍旧有点困难。创建一个自定义标签输出:在JSP1中,使动态网页内容更具可读性和可重复使用的方式是去创建一个自定义标签。自定义标签是被一个程序员所界定的JSP页面。此标签的行为是在一个通常在Java里写的标签处理类实施的,一个标签库描述符文件定义了在自定义标签和它的处理类之间的通信。一个Web容器使用标签库描述符文件和此自定义标签动态地产生代码。自定义标签产生一个更清洁的JSP网页和使代码重用。请记住,实现查找的实际代码是用的处理类和用JSP代码实现的例子信息看起来如: There are currently <myTags:userCo
6、unt/> users logged in. 当然,为了得到一个更加清晰的介绍,开发商不得不用Java类和XML格式化的TLD文件写。JSP 2.0现在提供了一个使访问服务器端状态更加简单的表达式语言,这种表达式语言的语法比一个自定义标签更加简单和通常需要没有相关的Java代码。此外,表达式语言自动处理类型转换,空值和错误处理。JSP 2.0表达式语言提供了访问服务器端的一个方法。这种方法比上面显示的方法做到事半功倍。JSP 2.0打印“用户号码”信息的方式看起来像这些:There are currently $ stats.userCount users logged in.当然,要取
7、得这样一个彻底的介绍,开发商将不得不写一个Java类和一个XML格式的TLD文件。EL表达式可以被用到哪呢?JSP 2.0规格详细地描述表达式语言。表达式语言是增加了一些新特点的JSTL 1.0 EL的扩展。一个JSP 2.0 EL表达式始终是在分隔符元“”和“”之间被写的。在JSP页的回应中,表达式评价的结果取代在表达式和在模板文本中表达式的分隔符。拥用标签,表达式只能在属性值中使用。例如,来自样本页中的以下代码是合法的,因为它仅仅在模板文本和属性中使用标签。<ul> <c:forEach var="k" items="$colors"
8、;> <li><font color="$k">This line is $k</font> </c:forEach> </ul>这儿是非法使用标签的一个例子:<$tag var="x"/> <%- INVALID SYNTAX -%>在一个EL表达中的变量是Web-tier状态。它们是能在任凭范围内有值的,这个范围包括页,请求,会话或应用。除非你指定,否则变量是在页的范围内。一般来讲,EL变量的值是对象。当一个变量的值是一个企业beans参考时,来自企业bean的
9、数据可以没有困难的被包含在网页中。例如,一个商务应用可以在一个HttpSession属性里和在购物车的名义下保持一个参考到一个ShoppingCart状态会话bean。在购物车里打印物品的数量将如下:You have $cart.itemCount items in your cart.EL语法EL表达式是由字母、操作数和变量组成。表达式也能包含函数,但是这些都不包括在这里。看JSP 2.0规格和J2EE 1.4详细指导。字母可以是布尔类型(“真”或“假”),整型,浮点数或者字符串。指示“null” 指示了空字母。这语言担供了以下操作符(在这儿括号仅仅用于分组):二元逻辑运算符(<, &
10、lt;=, =, >=, >, lt, le, eq, ge, gt)二元算术运算符(+, -, *, /, div, %, mod)索引运算符(.) and ()(相等)一元非运算符( !, not)一元算术 ()(空)运算符,如果表达为空或者评估为一个包含没有任何项目的容器对象,它评估为真值。三元运算符if-then-else(?:)括号为控制评价优先具有最高优先权的运算符是“”和“”,次之是()。剩下运算符的优先顺序是传统的:看JSP 2.0规格详细说明。字母表达包含仅字母和运算符的任何一个表达试是作为一个算术表达式来评价的。这是作为秘决显示在样本页中。相对于这个平台的一些其
11、它重要的变化包括:在表达式的变量名评测一种类型的值时,这种类型和和它相关的运算联系在一起。比如,在执行加法之前表达式 $X+1试图强制X成为一个数字。变量是靠名字访问的。强制类型是自动的从变量的本类型到一种和要求运算可兼容类型。变量或者是被网页所定义或者是JSP隐含的对象。(看JSP 2.0规格和J2EE 1.4指导为一个隐含对象的完全清单)。变量名字由涉及运算符“”和“”的表达式组成。这些运算符提供了大量EL的权限,因为它们为所有收集的类型以及Java语言,图形,清单,和JavaBeans属性访问者而工作。此运算符允许你通过JavaBeans或者企业beans网络进行导航。你能从一
12、个bean移到下一个bean,每次你使用此运算符时,这是靠参考或者索引它的属性中的一个而移动的。“”运算符“”运算符是为调用一个JavaBeans属性访问者而速记的,此属性的名字是在此运算符的右边例如,以下是示例页的表达式:$pageContext.servletContext.servletContextName实际上执行为:pageContext.getServletContext().getServletContextName()无论表达式是否空值, EL是比Java编程语言更加宽容的。在Java编程语言中,如果a.getB()返回空值时,表达式a.getB().getC()抛出一个Nu
13、llPointerException异常。在EL中并非如此。如果$ab是空,EL表达式$abc返回空值,但它不抛出一个异常。这使得写表达式更加简单。为了明确地核查空值,或者使用空运算符,或者使用= =或者是关键字把此表达式比喻为空值,像这样: <c:if test="$a.b is null"> <c:if test="$empty a.b"> <c:if test="$a.b = null">如果被访问的对象是一个图形,“”运算符使用作为字符串字母的右边的名字和使用它作为取结果的关健。这是在以下两
14、人表达式的示例代码中所演示的。此表达式是相等的(两者都参考主机HTTP标头): $header"host" $header.host“”运算符“”运算符是一个多索引运算符,它可以用于索引集合(包括图和清单)和阵列。在括号内的值是作为成为一个图或者列表或者阵列索引的关键被使用的。在示例页中,运算符是用于存取一个列表和一个图的:$colors1orange$colors5violet$colors1 > colors5false$colors1024 $colors1024 = nulltrue注意到$colors1024没有值(因为数组不是长的)。评估表达产
15、生空,不是一个越界错误。运算符也可以被用于调用JavaBeans属性访问者。在那种情况下,在空号内的字符串是属性的名字。例如,给一个JavaBean a,表达式$a"b"和 $a.b两者都通过用a.getB()评估Java方法。“”和“”运算符在访问图时几乎但不是完全相等的。两者的不同之处说明如下:$header"host"localhost:8080$header.hostlocalhost:8080$header"user-agent"Mozilla/5.0 (Macintosh;.)$header.user-agent0注意到$
16、header“host”和$header.host在所谓标头的地图上寻找关键主机(这是一个JSP隐含的包含HTTP标头请求的对象),但是,尽管$头部“用户代理”产生一个浏览器的名字,$头部用户代理产生0,这是什么意思呢?这里的问题是用户代理不是一个容易形成的Java标识符值为0因为表达评估者解析表达式,此表达式作为变化标头“用户”减掉(不存在)变化代理的值区别用的。这些值都是空的,因此结果为0(这是公约所界定的规格)。这里的经验是:在检索地图时最好总是使用和在调用属性存取器时使用“”。在“”和“”之间最后的区别是一个表达式的值可以用“”来表明一个名字。示例代码看起来像这:<c:set v
17、ar="headerName" value="host"/>$headerheaderName这组结果是和其它两个表达式一样的,这两个表达式寻找主机头部:“本地:8080”。第一行把页的变量头部名称设置为字符串主机。第二行使用变量的值寻找HTTP头部主机。尽管此代码简单使用一个变量的名字,“”的内容可以是产生一个HTTP头部名字的任何EL表达。控制变量范围除非定义为其它的,在一个有分配运算符“=”或者一个JSTL<c:set>的页上定义的变量是在页的范围内(换句话说,它们是PageContext属性)。如果一个变量没有指明它的范围,表达
18、预测器寻找属性页,请求,会话和应用范围,并返回第一个找到的。如果没有那个名字的变量是找到的,表达式评价为空值。一个明确范围内的变量可以用四个隐含范围对象中的一个来访问,这四个隐含对象包括:页范围,请求范围,会话范围或者应用范围。举例来说,$sessionScopemyVariable返回会话属性myVariabler的值。一个变量的范围可以用<c:set>范围属性来定义,比如:<c:set var="myVariable" value="1" scope="session"/>像许多脚本语言,EL是宽容空值。当
19、它可以的时候,EL只不过代用一个空子符串或者0。伴随着这个行为的问题是如果值是空的时候有时你像有特殊的行为。如果你正在使用JSTL,<c:out>标签有一个默认的属性,你能使用此属性以防你正在使用的这个值为空。这个来自样本页的事例展示怎样使用它:<c:out value="$colors1024" default="transparent"/>结果是字符串透明。使用纯EL做同样的事情,你能使用三元 ?:运算符,像这样:$colors1024 = null ? "transparent" : colors1024
20、这个表达式出于以前同样的理由也透明返回。当使用JSTL标签<c:forEach>在一个图上的值进行重复时,这重复变量是java.util.Map.Entry的一个类型。访问地图入口的关键字和值,在重复变量名字上使用.key 和.entry。示例页展示这方面的一个例子,转储HTTP标头到页:<c:forEach var="hdr" items="$header">$hdr.key=$hdr.value<br></c:forEach> 摘自:Mark JohnsonSun Developers Newslett
21、er.http:/developers/etters.html, 2003 December 22. 附:英文原文JSP 2.0 Expression LanguageMark JohnsonThese tips were developed using Java 2, Enterprise Edition, v 1.4 SDK. You can download the SDK at This issue of the Tech Tips is written by Mark Johnson, president of elucify technical communications, an
22、d co-author of Designing Enterprise Applications with the J2EE Platform, 2nd Edition. Mark Johnson runs an open forum for discussion of the tips. You can download the sample archive for these tips. The context root for the application is ttjan2004, and the index.html welcome file indicates how to us
23、e the sample code. Any use of this code and/or information below is subject to the license terms. JSP 2.0 EXPRESSION LANGUAGEThe December 22, 2003 Tech Tip titled The JavaServer Pages Standard Tag Library (JSTL) showed how to use some of the new features of JSP 2.0, including the new Expression Lang
24、uage. The following tip covers the expression language in more detail. The sample code accompanying the tip is a Web application that contains a single JSP page. The page demonstrates several types of expressions and some odd conditions. Why Another Language? Before JSP 2.0, you could use only a scr
25、iptlet, JSP expression, or a custom tag to include server state in the JSP page output. Although these solutions are useful, they require a relatively large amount of work for even the simple job of accessing server-side state. JSP 2.0 incorporates the Expression Language (EL) first introduced in JS
26、TL 1.0. EL makes it easier to integrate server-side state with presentation output. Let's start with a quick review of scriptlets, JSP expressions, and custom tags. Consider a Web application that maintains information about usage statistics in an object of class ServerStats. Class ServerStats h
27、as a method getUserCount, which returns the number of users currently logged on. The application maintains a single instance of ServerStats in a ServletContext attribute called stats. Here's a comparison of how you could create usage statistics output using a scriptlet, a JSP expression, and a c
28、ustom tag: · Creating output with a scriptlet: With JSP version 1, you can print the number of users logged on to the system using a scriptlet. Any output the scriptlet produces has to be explicitly written to the out stream. A JSP page could report the number of users with the following script
29、let and template text: · There are currently <%· ServerStats ss =· (ServerStats)application.getAttribute("stats");· out.print(ss.getUserCount();· %> userslogged on.The result would look something like this: There are currently 15 users logged on. · Creat
30、ing output with a JSP expression: You can simplify things a bit by placing the code in a JSP expression. The expression text within the JSP expression is evaluated, and the result is coerced to a String for output. The result of the expression replaces the expression tag in the JSP page output. Writ
31、ing the resulting text to the JSP page output is implicit. · There are currently· <%= (ServerStats)(application.getAttribute("stats").· getUserCount() %> users logged on.This is somewhat easier to write, but is still a bit difficult to read and maintain.· Creating
32、 output with a custom tag: In JSP 1, the way to make dynamic content on a page more readable and reusable is to create custom tags. Custom tags are JSP page tags defined by a programmer. The behavior of the tag is implemented in a tag handler class, which must be written (usually in Java). A tag lib
33、rary descriptor (TLD) file defines the correspondence between the custom tag and its handler class. A Web container uses the TLD file and the custom tag class to generate code dynamically. Custom tags produce a cleaner JSP page and enable code reuse. Remembering that the actual code that implements
34、the lookup is in the handler class, the JSP code for the example message would look like this: · There are currently <myTags:userCount/> users logged in.Of course, to get to such a clean presentation, a developer would have to write both a Java class and an XML-format TLD file. JSP 2.0 no
35、w offers a expression language that makes accessing server-side state even easier. The syntax of the expression language is simpler than a custom tag, and usually requires no associated Java code. Also, the expression language automatically handles typecasting, null values, and error handling. The J
36、SP 2.0 expression language provides a way to access server-side state with less effort than the approaches shown above. The JSP 2.0 way to print the "number of users" message would look like this: There are currently $stats.userCount users logged in.Where EL Expressions Can Be Used The JSP
37、 2.0 specification describes the expression language in detail. The expression language is an extension of the JSTL 1.0 EL, that adds several new features. A JSP 2.0 EL expression is always written between the delimiters $ and . In the JSP page response, the result of expression evaluation replaces
38、the expression and its delimiters in the template text. Within tags, expressions can be used only in attribute values. For example, the following code from the sample page is legal, because it uses tags only in template text and in attributes. <ul> <c:forEach var="k" items="$
39、colors"> <li><font color="$k">This line is $k</font>. </c:forEach> </ul>Here's an example of a tag that is used illegally: <$tag var="x"/> <%- INVALID SYNTAX -%>The variables in an EL expression are Web-tier state. They are va
40、lues that can be in any scope: page, request, session, or application. Unless you specify otherwise, variables are in page scope. In general, the values of EL variables are objects. When a variable's value is an enterprise beans reference, data from the enterprise bean can be included in the pag
41、e with little difficulty. For example, a shopping application could keep a reference to a ShoppingCart stateful session bean in an HttpSession attribute, under the name "cart". Printing the number of items in the cart would then be as simple as the following: You have $cart.itemCount items
42、 in your cart.EL Syntax EL expressions are comprised of literals, operators, and variables. Expressions can also contain functions, but these are not covered here. See the JSP 2.0 specifications and the J2EE 1.4 Tutorial for details. Literals can be of type boolean ("true" or "false&q
43、uot;), integer, floating point, or string. The token "null" denotes the null literal. The language offers the following operators (parentheses here are used only for grouping): · binary logical operators(<, <=, =, >=, >, lt, le, eq, ge, gt)· binary arithmetic operator
44、s(+, -, *, /, div, %, mod)· indexing operators(.) and () · (equivalent) unary negation operators(!, not)· arithmetic unary minus(-)· (empty) operator, which evaluates to true if the expression evaluates to null, or evaluates to a container object that contains no items.· ter
45、nary operator for simple if-then-else(?:)· parentheses for controlling evaluation precedence The operators with the highest precedence are "" and ".", in that order, followed by (). The precedence for the rest of the operators is conventional: see the JSP 2.0 specifications
46、for details. Literal Expressions Any expression containing only literals and operators are evaluated as arithmetic expressions. This is shown in the sample page for the tip: ExpressionValue$11$1=1true$1=2false$22/73.1428.$123*23427872Some other important changes to the platform include: Variable nam
47、es in expressions evaluate to values of a type appropriate for the operation in which it is involved. For example, the expression $x + 1 tries to coerce x to a number before performing the addition. EL Variables Variables are accessed by name. Type coercion is automatic from the variable's nativ
48、e type to a type that is compatible with the requested operation. Variables can be either defined by the page, or can be JSP implicit objects. (See the JSP 2.0 specifications and the J2EE 1.4 tutorial for a complete list of implicit objects.) Variable names can be composed with expressions involving
49、 the operators "." and "". These operators provide a great deal of the power of EL, because they work for all collection types, as well as for Java language arrays, maps, lists, and JavaBeans property accessors. The operators allow you to navigate through a network of JavaBeans o
50、r enterprise beans. You can move from one bean to the next by simply dereferencing or indexing one of its properties each time you use the operator. The "." Operator The "." operator is shorthand for calling a JavaBeans property accessor for the property whose name is on the righ
51、t-hand side of the operator. For example, the following expression in the sample page: $pageContext.servletContext.servletContextNameactually executes as: pageContext.getServletContext().getServletContextName()EL is more forgiving than is the Java programming language regarding null values in expres
52、sions. In the Java programming language, the expression a.getB().getC() throws a NullPointerException if a.getB() returns null. Not so in EL. The EL expression $a.b.c returns null if $a.b is null - it does not throw an exception. This makes writing expressions easier. To check for null explicitly, u
53、se either the empty operator, or compare the expression to null using = or the is keyword, like this: <c:if test="$a.b is null"> <c:if test="$empty a.b"> <c:if test="$a.b = null">If the object being accessed is a Map, the "." operator uses th
54、e name of the right-hand side as a string literal, and uses it as a key to fetch the result. This is demonstrated in the sample code in the two following expressions. The expressions are equivalent (both reference the host HTTP header): $header"host" $header.hostThe "" Operator T
55、he operator is a polymorphic indexing operator that can be used to index collections (including Maps and Lists) and arrays. The value inside the brackets is used as a key into a map, or as a List or array index. On the sample page, the operator is used to access a List and a Map: $colors1orange$colo
56、rs5violet$colors1 > colors5false$colors1024 $colors1024 = nulltrueNote that $colors1024 has no value (because the array isn't that long). Evaluating the expression produces null, not an "out of bounds" error. The operator can also be used to call JavaBeans property accessors. I
57、n that case, the string in the bracket is the property name. For example, given a JavaBean a, both of the expressions $a"b" and $a.b evaluate to the Java method call a.getB(). The "." and operators are almost, but not quite, equivalent for accessing Maps. The difference between t
58、he two is illustrated below: $header"host"localhost:8080$header.hostlocalhost:8080$header"user-agent"Mozilla/5.0 (Macintosh;.)$header.user-agent0Notice that $header"host" and $header.host both look up the key host in the Map called header (which is a JSP implicit object
59、 containing HTTP headers for the request). But, while $header"user-agent" produces the name of a browser, $header.user-agent produces 0. What does this mean? The problem here is that user-agent isn't a well-formed Java identifier. The value is 0 because the expression evaluator parses the expression as the difference of the variable header"user" minus the value of the (nonexistent) variable agent. Both of these values are null, so th
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度产品召回风险承担协议书
- 2025年度生物科技私下股份分配与成果转化协议书
- 2025年度再婚家庭婚姻和解及子女抚养协议
- 2025年度企业年鉴图文编纂及出版协议
- 2025年度安防系统智能化升级与维护合同
- 2025年度企业内部控制体系建设咨询合同模板
- 旅游景区民宿租赁居间合同
- 2025年度保险销售人员劳动合同解除与赔偿规范
- 2025年度三年劳动合同涨薪与员工职业规划辅导合同
- 2025年度双方经济纠纷一次性解决及确认协议
- 设备维修绩效考核方案
- 《宏观经济管理研究》课件
- 凤凰卫视中文台节目表
- 2025届广东省佛山一中、石门中学高考数学考前最后一卷预测卷含解析
- DCMM初级认证知识考点练习试题
- 2025年职业卫生工作计划
- 做账实操-农贸市场的账务处理示例
- 余华《活着》解读课件
- 关于纳粹德国元首希特勒的历史资料课件
- 新媒体运营说课CHAPTER课件讲解
- 护理带教老师述职报告
评论
0/150
提交评论