标签的名字是multi, util.tags.multilinktag是实现该标签功能的类,该类被称作标签处理器,按照jsp定制标签规范该处理器需要实现特定的接口。想了解更多有关资料,可以去sun官方网站查阅documentation。
为了方便学习,该标签还是模仿了html的标记,并有下列属性:
·id, 标签id.这将被用来创建html元素以支持动态菜单,对于本页面该标签id必须是唯一的。
·href. 说明url地址的属性,也类似于html。
·metalinks, xml文件的名称,包含嵌入在主连接里的连接信息。该文件分为几部分,用来设置每个连接,并且每一部分必须有名字属性,这类似于前面提高的标签id.
·onmouseover。 一个可选项,用来说明当鼠标悬在下拉图标上或者点击了它时,是否弹出一个菜单。如果没有设置该项为true则取消鼠标悬停在图标上时的作用。
jsp页面代码里使用multilink.tld定义的标签需要声明一个前缀,就像是html里的标记<a>,我们在此也声明一个前缀”a”,这样multi标签的完整名称为<a:multi>,下面我们用一个实例来展示一下该标签的使用:
<%@ taglib uri="multilink.tld" prefix="a" %>
<html>
<body>
<p>
nasa's high-speed research program is developing a new
<a:multi href="http://someurl/" id="hsct" metalinks="embedded_links.xml"
onmouseover="true">high-speed civil transport</a:multi>
passenger jet that is environment-friendly and economically viable.
the aircraft will have a cruise speed of
<a href="http://someurl/">mach</a> 2.4, and will feature an
<a:multi href="http://someurl/" id="xvs" metalinks="embedded_links.xml"
onmouseover="false">external vision system</a:multi>
for low-speed operations, eliminating the need for a conventional
windshield or drooping nose.
</p>
</body>
</html>
该标签是如何在浏览器里面发挥它的作用的呢?请继续往下看。
当通过url来访问该页面时,浏览器截取请求,通过处理jsp页面,用标签处理器(类util.tags.multilinktag)执行后产生的内容取代<a:multi>标签,标签处理器解析xml文件(embedded_links.xml或者该文件是jsp文件),收集一些嵌入连接的信息并以纯html发送到浏览器,下面是embedded_links.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<hrefs>
<section name="hsct">
<href url="http://oea.larc.nasa.gov/pais/hsr-general.html"
text="overview of nasa's hsct program"/>
<href url="http://www.beyond2000.com/news/aug_00/story_739.html"
text="hsct project objectives and motivation"/>
<href url="http://vesuvius.jsc.nasa.gov/er/seh/pg34s95.html"
text="nasa's high speed research page"/>
<href url="http://www.amitgoel.com/vizcraft/index.html"
text="hsct visualization"/>
<href url="http://www.aerospace.nasa.gov/goals/fsvtp.htm"
text="affordable supersonic travel"/>
<href url="http://www.aerospace.nasa.gov/goals/images/g6icon1.jpg"
text="what does it look like?"/>
</section>
<section name="xvs">
<href url="http://oea.larc.nasa.gov/pais/hsr-cockpit.html"
text="the external visibility system concept"/>
<href url="http://www.aerospace.nasa.gov/library/ar99/obj6.html#6-5"
text="cockpit with an artificial view"/>
<href url="http://lisar.larc.nasa.gov/images/small/el-1998-00169.jpeg"
text="what does it look like?"/>
</section>
</hrefs>
最后我们大体看一下jsp标签的执行。标签有一个体,标签处理器继承自javax.servlet.jsp.tagext.bodytagsupport.,并且它仅实现了dostarttag()和doaftertag()这两个方法,dostarttag()方法仅返回eval_body_tag,指明标签体需要评价,doaftertag()方法处理所有自定制标签的操作,他返回skip_body,知名标签体已经不存在并不再被请求。标签体就是jsp标签打开和关闭元素之间的那部分。
下面是标签处理器的简略的代码文件,可以去这里(source code)下载完整的代码:
public class multilinktag extends javax.servlet.jsp.tagext.bodytagsupport
{
// initialize variables and define setters
...
public int dostarttag() throws jspexception {
// indicate that we're interested in evaluating the tag's body
return eval_body_tag;
}
public int doafterbody() throws jspexception {
// process the tag's body and the embedded links
// found in the xml document.
...
// don't need the tag's body any more.
return skip_body;
}
}
一旦标签处理器有错误,可以通过点击主连接来恢复它,下图是jsp页所显示的:
点击"high-speed civil transport"超连接可以转到他所在的地址,把鼠标悬停在旁边的下拉图标上,会弹出个菜单,正如上图所示,你可以选一项然后会转到相应的地址,鼠标离开菜单后,弹出菜单自动消失。
该功能已在inte.net explorer 5.0+ 和.netscape navigator 6.0+.成功测试并通过,在其他浏览器上还没这项功能。
讨论:
多地址连接技术对于许多web使用者来说还是比较新颖的,假如用户们不愿用一些新思想,让他们接受一些完全的新导航功能也是一种挑战,因此现在采用基于现有的大家都熟悉的导航菜单来解决这个问题。在菜单系统里用户可以在一个主题里做一些选择,根据他们的选择转到相应的地方。
多地址连接使得用户有所选择。这样就减少了由于用户访问一些不相关的连接所消耗的时间和流量,并且能独立于html文本页面的内容存储连接信息~,以使得页面设计者可以独立于html文本去改变这些连接信息,这让人突发奇想到了动态超文本,即根据用户的需要来改变超文本。例如,页面设计师们改变连接以使其与用户联系更密切。
最后,下拉图标也是非常重要的。他从视觉上区分了一般的超连结和嵌入连接式的超连接,另外,onmouserover功能使得用户不需要点击图片,直接把鼠标悬在图标上就可以显示菜单。
总结:
本文描述了jsp定制标签扩充html超链接功能,也可以在.net平台上采用asp.net定制服务控制器来实现该功能
随着网络信息流量的日益增长,更方便的网页导航是必需的,可以增加些低成本而又能丰富web的改进。每天都有上百万的人浏览网页,所以很多事情需要做以使得网络更加强大更有使用价值,好让用户更好体验到上网的乐趣,真正的感触到网络生活的魅力所在!
matrix开源技术经onjava授权翻译并发布.
如果你对此文章有任何看法或建议,请到matrix论坛发表您的意见.
注明: 如果对matrix的翻译文章系列感兴趣,请点击oreilly和javaworld文章翻译计划查看详细情况
您也可以点击-javamen查看翻译作者的详细信息.
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 注册表 操作系统 服务器 应用服务器