(Transfer) Master the JSP custom label: (3)

zhaozj2021-02-16  93

Step 3: Create attributes in the label handler Java class

Page 5 (9 pages a total)

We want to specify three properties for the MaPDefine tag, as shown below:

Property Description ID The name of the new Scriptlet variable. Scope new ScriptleT variables are located. Type new Scriptlet variable (HashMap, FasthashMap, TreeMap, or FastTreeMap). If Type is set to Hash, you will create a HashMap. If Type is set to Fasthash, FasthashMap will be created. When using this tag in the JSP page, it looks like this: ... This label will be Create a HashMap called EditParams within the session. In order to create properties in the label handler, you need to define the corresponding JavaBeaN property. Therefore, each attribute has a corresponding setter method in the label handler, as shown below: public class mapdefinetag extends tagsupport {... private string type = fasttree; private string id; private string scope; public void settype (String String ) {type = string;} public void setid (String String) {id = string;} PUBLIC VOID STRING (String String) {Scope = String;} The conversion engine will set this label with hard-coded configuration data or runtime expression. Attributes. We will discuss this in step 4: Detailed discussion in the property in the TLD file. In step 5: Implementing the dostartTag () method, we will use these properties in the dostartTag () method of the label handler.

Step 4: Define properties in a TLD file

Page 6 (9 pages a total)

Just as in the previous section, the custom property is defined by declaring the JavaBean property, and then declare these properties in the TLD file. Each JavaBean property must match the corresponding custom tag properties. The attribute defined in the TLD must match the JavaBeaN property, but it can have a javabean property that does not match the tag properties. Here is the property declaration of mapdefinetag: mapdefine Trivera.tags.map.mapdefinetag jsp .. . id true false The id attribute scope < / name> false false The scope attribute type false FALSE Specifier The Type of Map Valid Values ​​Are FastTree, Fasthash, Hash, Tree Name element specifies the name of the property. The Required element specifies if the attribute is required (the default is false). The RTEXPRVALUE element indicates that the attribute is hard-coded to convert the conversion or allowing the runtime Scriptlet expression. Remember, the MapDefineTag class must define a JavaBeaN property for each attribute described in the previously described, in step 3: Create this task in the label handler Java class. Step 5: Implement DostartTAG () Method

Page 7 of 9

Call the dostarttag () method at the start of the label - From the developer's point of view, this is when the engine encounters . If DOSTATTTAG () returns Skip_Body, the tag body will not proceed. If it returns an evAl_body_include, the body will be processed. The DostartTag () method of the MapDefine class completes the following:

Determine the properties of the MAP to be created according to the Type property. Determine the new MAP object in what range according to the Scope property. Determine the name of the range of the new MAP object to be placed based on the id attribute. Let us analyze this process in more detail. The MAPDefine class checks the Type property is set to fasttree, haveh, Tree or Fasthash. And then create the appropriate map, as shown below: / * String constants for the different types of maps we support * / public static final String FASTHASH = "FASTHASH"; public static final String FASTTREE = "FASTTREE"; public static final String HASH = "HASH"; public static final String TREE = "TREE"; / ** The map we are going to create * / private Map map = null; / ** The member variable that holds the type attribute * / private String type = FASTTREE ; ... public int doStartTag () throws JspException {/ ** Based on the type attribute, determines which type of Map to create * / if (type.equalsIgnoreCase (FASTTREE)) {map = new FastTreeMap ();} else if (Type.equalsignorecase (HASH)) {map = new hashmap ();} else if (Type.Equalsignorecase (Tree)) {map = new treemap ();} else f (type.equalsignorecase (fasthash) {map = new FastHashMap ();} then uses the ID and Scope properties to set the HashMap to a given range in a given name: private string id; private intendTAG () throws jspexception {... if (Scope == null) {pageContext.setttribute (ID, m ap);} else if ( "page" .equalsIgnoreCase (scope)) {pageContext.setAttribute. (id, map);} else if ( "request" .equalsIgnoreCase (scope)) {pageContext.getRequest () setAttribute (id, map);} else if ( "session" .equalsIgnoreCase (scope)) {pageContext.getSession () setAttribute (id, map);.} else if ( "application" .equalsIgnoreCase (scope)) {pageContext.getServletContext (). SetAttribute (ID, MAP);} returnval_body_includ;} If the range attribute is null, the MAP will put into the page range.

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

New Post(0)