摘要: jsp 2.0是对jsp 1.2的升级,增加了几个有趣的新特性,可以使web应用程序设计人员和开发人员的工作更容易一些。jsp 2.0的目标是比以前更易于使用,更重要的是无须学习java语言本身就可以使用它。它增加了一种称为simpletag的新扩展机制来简化标签api(tag api)。
____________________________________________________
除了其他一些改进外,jsp 2.0引入的最主要的新特性包括:
1.一种简单的表达式语言(el),能够用来容易地从jsp页面访问数据,这种表达式语言简化了基于jsp的不含脚本的应用程序的编写,不需要使用java scriptlet或者java表达式;
2.用于直接使用jsp技术来定义可重用的自定义行为的新语法,该语法使用.tag和.tagx文件,这类文件可由开发人员或者网页作者编写;
3.xml语法得到了实质性的改进,增加了新的标准文件扩展名(.tagx用于标签文件,.jspx用于jsp文件)。
本文主要讨论表达式语言、简化的标签api和标签文件。相信目前的jsp开发人员将会发现这些重要的特性不但有趣,而且非常有用。
为何要从1.2跨越到2.0?
在java规格请求(jsr 152)中版本号最初定为1.3。但是正如你将在后面看到的那样,由于这些新特性对jsp应用程序的开发模型产生了如此深刻的影响,专家组感到有必要把主版本号升级到2.0,这样才能充分反映这种影响。此外,新的版本号也有助于把开发人员的注意力吸引到这些有趣的新特性上来。令人欣慰的是,所有合法的jsp1.2页面同时也是合法的jsp2.0页面。
jsp 2.0起步
在着手学习jsp 2.0之前,你需要一个支持jsp2.0和java servlet 2.4规范的jsp环境。幸运的是,jakarta tomcat 5.0(alpha测试版)支持新的jsp2.0和servlet 2.4规范,可以从http://jakarta.apache.org/builds/jakarta-tomcat/release/v5.0.4-alpha/下载并安装tomcat 5.0。
jsp表达式语言
向jsp网页传递信息是通过jsp scoped属性和请求参数完成的。专门为网页作者量身定做的一种表达式语言(el)把jsp scoped属性提升为从业务逻辑向jsp页面传递信息的标准方式。但是要注意,尽管这种表达式语言是jsp的一个重要特性,它并不是一种通用的程序语言。它仅仅是一种数据访问语言,可以方便地访问和处理应用程序数据,而无需使用scriptlet或者请求时(request-time)表达式的值。
在jsp2.0之前,网页作者只能使用表达式<%= aname %>访问系统的值,比如下面的例子:
<sometags:atag attribute="<%= pagecontext.getattribute("aname") %>">
<%= acustomer.getaddress().getcountry() %>
<sometags:atag attribute="${aname}"> ${acustomer.address.country} 运算符
说明
+
加
-
减
*
乘
/ 或 div
除
% 或 mod
模(求余)
== 或 =
等于
!= 或 !=
不等于
< 或 lt
小于
> 或 gt
大于
<= 或 le
小于等于
>= 或 ge
大于等于
&& 或 and
逻辑与
or or
逻辑或
! 或 not
逻辑非
empty
检查是否为空值
a ? b : c
条件运算符
隐含对象
内容
applicationscope
应用程序范围内的scoped变量组成的集合
cookie
所有cookie组成的集合
header
http请求头部,字符串
headervalues
http请求头部,字符串集合
initparam
全部应用程序参数名组成的集合
pagecontext
当前页面的javax.servlet.jsp.pagecontext对象
pagescope
页面范围内所有对象的集合
param
所有请求参数字符串组成的集合
paramvalues
所有作为字符串集合的请求参数
requestscope
所有请求范围的对象的集合
sessionscope
所有会话范围的对象的集合
<html><head><title>form content</title></head>
<body>
<h3>fill-out-form</h3><p><form action="form.jsp" method="get"> name = <input type="text" name="name" value="${param['name']}"> <input type="submit" value="submit name">
</form>
<p>the name is: ${param.name}
</body></html>
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; }
}
<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>
<%@ 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>
package jsp2.examples.simpletag;
import javax.servlet.jsp.jspexception;import javax.servlet.jsp.tagext.simpletagsupport;
import java.io.ioexception;
/** * simpletag handler that prints "this is my first tag!" */ public class hellotag extends simpletagsupport {
public void dotag() throws jspexception, ioexception { getjspcontext().getout().write("this is my first tag!");
}
}
<tag>
<description>prints this is my first tag</description>
<name>hello</name>
<tag-class>jsp2.examples.simpletag.hellotag</tag-class>
<body-content>empty</body-content>
</tag>
<%@ taglib prefix="mytag" uri="/web-inf/jsp2/jsp2-example-taglib.tld" %><html><head><title>simple tag handler</title></head>
<body><h2>simple tag handler</h2><p><b>my first tag prints</b>: <mytag:hello/></body></html>
<%@ taglib prefix="tags" tagdir="/web-inf/tags" %><html><head><title>jsp 2.0 examples - hello world using a tag file</title></head><body><h2>tag file example</h2><p><b>the output of my first tag file is</b>: <tags:greetings/></body></html>
<%@ attribute name="color" %><%@ attribute name="bgcolor" %><%@ attribute name="title" %><table border="0" bgcolor="${color}">
<tr> <td><b>${title}</b></td> </tr> <tr> <td bgcolor="${bgcolor}"> <jsp:dobody/> </td> </tr></table>代码示例12: newsportal.jsp
<%@ taglib prefix="tags" tagdir="/web-inf/tags" %><html><head><title>another tag file example</title></head><body><h2>news portal: another tag file example</h2> <table border="0">
<tr valign="top"> <td> <tags:display color="#ff0000" bgcolor="#ffc0c0" title="travel"> last french concorde arrives in ny<br/> another travel headline<br/> yet another travel headline<br/> </tags:display> </td> <td> <tags:display color="#00fc00" bgcolor="#c0ffc0" title="technology"> java for in-flight entertainment<br> another technology headline<br> another technology headline<br> </tags:display> </td> <td> <tags:display color="#ffcc11" bgcolor="#ffffcc" title="sports"> american football<br/> nba<br/> soccer<br/> </tags:display> </td> </tr> </table> </body> </html>
本文章由will整理发布,您可以点击http://www.matrix.org.cn/user_view.asp?username=will查看他的详细信息
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器