Analysis of FormTag in Struts

xiaoxiao2021-03-06  90

[pre] java.lang.object

|

- javax.servlet.jsp.tagext.tagsupport

|

- org.apache.struts.taglib.formtag [/ pre]

First, technical preparation

TAG interface

1. Tagsupport implements the javax.servlet.jsp.tagext.tag interface, the TAG interface defines the basic protocol between the Tag Handler and JSP implementation classes. The life cycle is defined and the method to be called at the beginning of the Tag. The TAG interface defines the following methods:

DostartTag ()

doendtag ()

RELEASE ()

getParent (), SetParent (TAG)

SetPageContext (PageContext)

JSP Page Implements the object first calls the setPageContext, Separent method, and initializes other properties; then, you can call the Tag Handler's dostartTAG (), and DOENDTAG () method.

2. Lifecycle

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

? [1] Call release () Releases all properties

? [2] TAG ended normally

? [3] Check the trycatchfinally interface for additional details related to exception handling and resources management

Tagsupport class

Javax.Servlet.jsp.tagext.tagsupport is the basic class of Tag Handler that implements Tag, IterationTag interface. ITerationTag inherits to the TAG interface and adds a way to DOAFTERBODY (). You only need to inherit tagsupport, redefine several methods to generate a new Tag Handler. Tagsupport has two Protected properties:

? Id? D? D protected string

? PageContext? D? Dprotected pageContext

? VALUES? D? D Private Hashtable

? Parent - Private Tag

There are other constants that inherit from Tag, IterationTAG, such as Eval_Body_InClude, etc.

The subclass of Tagsupport also needs to define a non-arranging configuration method of a public in which a PUBLIC is unfolded.

Tagsupport has the following methods:

• Tag, IterationTAG interface definition method;

• Attribute ID, PageContext, Parent's setter / getter method;

• Key value pair method, SETVALUE (String, Object), getValues ​​(), getValue (string), RemoveValue (String) method.

Second, work mechanism

First, look at how a servlet container receives a .jsp request, how to resolve similar such a Tag.

Taking Tomcat 4.0.3 parsing the logon.jsp in Struts-Example as an example, if you have already accessed the link:

Http: // localhost: 8080 / struts-example / logon.jsp

In $ Tomcat / Work / LocalHost / Struts-Example, you can find a file called Logon $ Jsp.java, which is Tomcat to resolve the corresponding Java source file obtained by Logon.JSP. You can find

The Java source code corresponding to the label,

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 returnral");

......

IF (_jspx_th_html_form_0.doendtag () == javax.servlet.jsp.tagext.tag.skip_page)

Return;

}

Finally {

_jspx_th_html_form_0.release ();

}

Note that

Main attribute

protected string action = null;

// This form should be submitted by the action URL

protected string name = null;

//

protected string type = null;

// Form bean class name

protected string scope = null;

// Form Bean visible range

protected

Static MessageResources Messages =

MessageResources.getMessageResources (Constants.Package, ".localestrings");

Main method

1, dostarttag

/ / Find the Form Bean's Name, Scope, Type properties

LOOKUP ();

2, Lookup

protected

Void Lookup ()

Throws JSPEXCEPTION {

// Check if the value required is set

IF (name! = null) {

IF (scope == null)

SCOPE = "session";

IF (Type == Null) {

// Error handling model

JSPEXCEPTION E =

NEW JSPEXCEXCE ("FORMTAG.NAMETYPE"); pageContext.setttribute (action.exception_key, e,

PageContext.Request_scope);

Throw e;

}

Return;

}

/ / Find a collection instance of the required Application range, mappings, and instantiated in the Action Servlet and placed in servletContext.

ActionMAppings mappings = (pageContext)

PageContext.getattribute (action.mappings_key,

PageContext.Application_scope);

ActionformBeans FormBeans = ...

IF ((mappings == null) || (FORMBEANS == Null) ...

/ / Find this formally associated actionMApping

String mappingname = getActionMappingname ();

/ / Emphasize mapping name from Action

ActionMapping mapping = mappings.findmapping (mappingname);

转载请注明原文地址:https://www.9cbs.com/read-121750.html

New Post(0)