[pre]java.lang.Object
|
+--javax.servlet.jsp.tagext.TagSupport
|
+--org.apache.struts.taglib.FormTag[/pre]
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
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源码片断,
org.apache.struts.taglib.html.FormTag _jspx_th_html_form_0 =
new org.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)
throw new JspTagException( "Since tag handler "
+ "class org.apache.struts.taglib.html.FormTag does not implement"
+ "BodyTag, it can't return BodyTag.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();
}
注意,在
protected String action = null; // 这个form应提交的action URL
protected String name = null; //
protected String type = null; // form bean的类名
protected String scope = null; // form bean的可见范围
protected static MessageResources messages =
MessageResources.getMessageResources(Constants.Package, + “.LocaleStrings”);
1、doStartTag
// 查找form bean的name、scope、type属性
lookup();
2、lookup
protected void lookup() throws JspException {
// 检查需要的值是否已设置
if (name != null) {
if (scope == null)
scope = “session”;
if (type == null) { // 错误处理范本
JspException e =
new JspException ( messages.getMessage(“formTag.nameType”));
pageContext.setAttribute (Action.EXCEPTION_KEY, e,
PageContext.REQUEST_SCOPE);
throw e;
}
return;
}
// 查找需要的application范围的集合实例,mappings、formBeans,在action Servlet中实例化,并放入ServletContext中。
ActionMappings mappings = (PageContext)
pageContext.getAttribute(Action.MAPPINGS_KEY,
PageContext.APPLICATION_SCOPE);
ActionFormBeans formBeans = …
if ((mappings == null) || (formBeans == null)) …
// 查找这个form关联的ActionMapping
String mappingName = getActionMappingName(); // 从action中解析出mapping的名字
ActionMapping mapping = mappings.findMapping(mappingName);
|
+--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源码片断,
org.apache.struts.taglib.html.FormTag _jspx_th_html_form_0 =
new org.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)
throw new JspTagException( "Since tag handler "
+ "class org.apache.struts.taglib.html.FormTag does not implement"
+ "BodyTag, it can't return BodyTag.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();
}
注意,在
主要属性
protected String action = null; // 这个form应提交的action URL
protected String name = null; //
protected String type = null; // form bean的类名
protected String scope = null; // form bean的可见范围
protected static MessageResources messages =
MessageResources.getMessageResources(Constants.Package, + “.LocaleStrings”);
主要方法
1、doStartTag
// 查找form bean的name、scope、type属性
lookup();
2、lookup
protected void lookup() throws JspException {
// 检查需要的值是否已设置
if (name != null) {
if (scope == null)
scope = “session”;
if (type == null) { // 错误处理范本
JspException e =
new JspException ( messages.getMessage(“formTag.nameType”));
pageContext.setAttribute (Action.EXCEPTION_KEY, e,
PageContext.REQUEST_SCOPE);
throw e;
}
return;
}
// 查找需要的application范围的集合实例,mappings、formBeans,在action Servlet中实例化,并放入ServletContext中。
ActionMappings mappings = (PageContext)
pageContext.getAttribute(Action.MAPPINGS_KEY,
PageContext.APPLICATION_SCOPE);
ActionFormBeans formBeans = …
if ((mappings == null) || (formBeans == null)) …
// 查找这个form关联的ActionMapping
String mappingName = getActionMappingName(); // 从action中解析出mapping的名字
ActionMapping mapping = mappings.findMapping(mappingName);