中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > Struts/Hibernate
剖析Struts中的FormTag
作者:未知 时间:2005-07-24 21:22 出处:JR 责编:chinaitpower
              摘要:剖析Struts中的FormTag
[pre]java.lang.Object
  |
  +--javax.servlet.jsp.tagext.TagSupport
        |
        +--org.apache.struts.taglib.FormTag[/pre]

一、技术准备



Tag接口

1.    TagSupport实现了javax.servlet.jsp.tagext.Tag接口,Tag接口定义了Tag Handler和JSP实现类之间的基本协议。定义了生命周期及在Tag开始与结束时应调用的方法。Tag接口定义了如下几个方法:
doStartTag ()
doEndTag ()
release ()
getParent ()、setParent( Tag )
setPageContext ( PageContext )
JSP page实现对象首先调用setPageContext、seParent方法,并初始化其他属性;然后,就可以调用tag handler的doStartTag()、doEndTag()方法。
2.    生命周期
[img]/members/srx81/formtag-1.jpg[img] 
    [1] 调用release()释放所有属性
    [2]  tag正常结束
    [3] Check the TryCatchFinally interface for additional details related to exception handling and resource management
TagSupport类

javax.servlet.jsp.tagext.TagSupport是tag handler的基础类,实现了Tag、IterationTag接口。IterationTag继承于Tag接口,添加了一个doAfterBody()的方法。你只需继承TagSupport ,重新定义几个方法即可生成一个新的tag handler。TagSupport 有两个protected的属性:
    id ?D?D protected String
    pageContext ?D?Dprotected PageContext
    values ?D?D private Hashtable
    parent - private Tag
还有其他一些继承自Tag、IterationTag的常量,比如EVAL_BODY_INCLUDE等。
含有一个无参的构造方法,TagSupport的子类也需要定义一个public的无参构造方法,在其中调用父类的构造方法即可。
    TagSupport有如下方法:
    Tag、IterationTag接口定义的方法;
    属性id、pageContext、parent的setter/getter方法;
    关键值对方法,有setValue(String, Object)、getValues()、getValue(String)、removeValue(String)方法。

二、工作机理


首先,看看一个Servlet容器首次接收到一个.jsp请求时,是如何解析类似<html:form action="/logon" focus="username">这样的tag。
以 Tomcat 4.0.3 解析struts-example中的logon.jsp为例,如果你已经访问过如下链接:
    http://localhost:8080/struts-example/logon.jsp 
在 $TOMCAT\work\localhost\struts-example\ 下可以找到一个名为logon$jsp.java的文件,这个文件就是Tomcat解析logon.jsp得到的对应的java源文件。可以找到logon.jsp中
<html:form action="/logon" focus="username">
标签对应的java源码片断,
  1. org.apache.struts.taglib.html.FormTag _jspx_th_html_form_0 = 
  2. new org.apache.struts.taglib.html.FormTag();
  3. _jspx_th_html_form_0.setPageContext(pageContext);
  4. _jspx_th_html_form_0.setParent(_jspx_th_html_html_0);
  5. _jspx_th_html_form_0.setAction("/logon");
  6. _jspx_th_html_form_0.setFocus("username");
  7. try {
  8.     int _jspx_eval_html_form_0 = _jspx_th_html_form_0.doStartTag();
  9.     if (_jspx_eval_html_form_0 == 
  10.         javax.servlet.jsp.tagext.BodyTag.EVAL_BODY_BUFFERED)
  11.        throw new JspTagException("Since tag handler " 
  12.          + "class org.apache.struts.taglib.html.FormTag does not implement" 
  13.          + "BodyTag, it can't return BodyTag.EVAL_BODY_TAG");
  14.        ……
  15.     if (_jspx_th_html_form_0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE)
  16.           return;
  17. finally {
  18.       _jspx_th_html_form_0.release();
  19. }

注意,在

主要属性

  1. protected String action = null;  // 这个form应提交的action URL
  2. protected String name = null;  // 
  3. protected String type = null;   // form bean的类名
  4. protected String scope = null;  // form bean的可见范围
  5. protected static MessageResources messages = 
  6.     MessageResources.getMessageResources(Constants.Package, + “.LocaleStrings”);


主要方法

1、doStartTag
// 查找form bean的name、scope、type属性
lookup();

2、lookup
  1. protected void lookup() throws JspException {
  2.     // 检查需要的值是否已设置
  3.     if (name != null) {
  4.         if (scope == null)
  5.             scope = “session”;
  6.         if (type == null) {          // 错误处理范本
  7.             JspException e = 
  8. new JspException ( messages.getMessage(“formTag.nameType”));
  9.             pageContext.setAttribute (Action.EXCEPTION_KEY, e, 
  10.                 PageContext.REQUEST_SCOPE);
  11.             throw e;
  12.         }
  13.         return;
  14.     }
  15. // 查找需要的application范围的集合实例,mappings、formBeans,在action Servlet中实例化,并放入ServletContext中。
  16. ActionMappings mappings = (PageContext)
  17. pageContext.getAttribute(Action.MAPPINGS_KEY, 
  18. PageContext.APPLICATION_SCOPE);
  19. ActionFormBeans formBeans = …
  20. if ((mappings == null) || (formBeans == null)) … 
  21. // 查找这个form关联的ActionMapping
  22. String mappingName = getActionMappingName();  // 从action中解析出mapping的名字
  23. ActionMapping mapping = mappings.findMapping(mappingName);



关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有