剖析Struts中的FormTag

来源:互联网 发布:mysql 5.6.35.tar.gz 编辑:程序博客网 时间:2024/06/07 17:33
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

  [pre]java.lang.Object

  |

  +--javax.servlet.jsp.tagext.TagSupport

  |

  +--org.apache.Struts.taglib.FormTag[/pre]

  一、技术准备

  Tag接口

  1.TagSupport实现了javax.servlet.jsp.tagext.Tag接口,Tag接口定义了TagHandler和JSP实现类之间的基本协议。定义了生命周期及在Tag开始与结束时应调用的方法。Tag接口定义了如下几个方法:

  doStartTag()

  doEndTag()

  release()

  getParent()、setParent(Tag)

  setPageContext(PageContext)

  JSPpage实现对象首先调用setPageContext、seParent方法,并初始化其他属性;然后,就可以调用taghandler的doStartTag()、doEndTag()方法。

  2.生命周期

  [img]/members/srx81/FormTag-1.jpg[img]

  [1]调用release()释放所有属性

  [2]tag正常结束

  [3]ChecktheTryCatchFinallyinterfaceforadditionaldetailsrelatedtoexceptionhandlingandresourcemanagement

  TagSupport类

  javax.servlet.jsp.tagext.TagSupport是taghandler的基础类,实现了Tag、IterationTag接口。IterationTag继承于Tag接口,添加了一个doAfterBody()的方法。你只需继承TagSupport,重新定义几个方法即可生成一个新的taghandler。TagSupport有两个protected的属性:

  id?D?DprotectedString

  pageContext?D?DprotectedPageContext

  values?D?DprivateHashtable

  parent-privateTag

  还有其他一些继承自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请求时,是如何解析类似这样的tag。

  以Tomcat4.0.3解析Struts-example中的logon.jsp为例,如果你已经访问过如下链接:

  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中

 

  标签对应的java源码片断,

  org.apache.Struts.taglib.html.FormTag_jspx_th_html_form_0=

  neworg.apache.Struts.taglib.html.FormTag();

  _jspx_th_html_form_0.setPageContext(pageContext);

  _jspx_th_html_form_0.setParent(_jspx_th_html_html_0);

  _jspx_th_html_form_0.setAction("/logon");

  _jspx_th_html_form_0.setFocus("username");

  try{

  int_jspx_eval_html_form_0=_jspx_th_html_form_0.doStartTag();

  if(_jspx_eval_html_form_0==

  javax.servlet.jsp.tagext.BodyTag.EVAL_BODY_BUFFERED)

  thrownewJspTagException("Sincetaghandler"

  +"classorg.apache.Struts.taglib.html.FormTagdoesnotimplement"

  +"BodyTag,itcan'treturnBodyTag.EVAL_BODY_TAG");

  ……

  if(_jspx_th_html_form_0.doEndTag()==javax.servlet.jsp.tagext.Tag.SKIP_PAGE)

  return;

  }finally{

  _jspx_th_html_form_0.release();

  }

  注意,在

  主要属性

  protectedStringaction=null;//这个form应提交的actionURL

  protectedStringname=null;//

  protectedStringtype=null;//formbean的类名

  protectedStringscope=null;//formbean的可见范围

  protectedstaticMessageResourcesmessages=

  MessageResources.getMessageResources(Constants.Package,+“.LocaleStrings”);

  主要方法

  1、doStartTag

  //查找formbean的name、scope、type属性

  lookup();

  2、lookup

  protectedvoidlookup()throwsJspException{

  //检查需要的值是否已设置

  if(name!=null){

  if(scope==null)

  scope=“session”;

  if(type==null){//错误处理范本

  JspExceptione=

  newJspException(messages.getMessage(“FormTag.nameType”));

  pageContext.setAttribute(Action.EXCEPTION_KEY,e,

  PageContext.REQUEST_SCOPE);

  throwe;

  }

  return;

  }

  //查找需要的application范围的集合实例,mappings、formBeans,在actionServlet中实例化,并放入ServletContext中。

  ActionMappingsmappings=(PageContext)

  pageContext.getAttribute(Action.MAPPINGS_KEY,

  PageContext.APPLICATION_SCOPE);

  ActionFormBeansformBeans=…

  if((mappings==null)||(formBeans==null))…

  //查找这个form关联的ActionMapping

  StringmappingName=getActionMappingName();//从action中解析出mapping的名字

  ActionMappingmapping=mappings.findMapping(mappingName);

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击