Struts Source Code Research - HTML-LINK Tags [Reserved]

xiaoxiao2021-03-06  20

The HTML: LINK tag in Struts is a very common label, which is also obvious: generate this HTML code

When I started to contact this label, I know this tag can be used:

============================================================================================================================================================================================================= =======

String via paramid and paramname

============================================================================================================================================================================================================= =======

This NewValue is generally a string type variable, the above code will generate such an HTML code:

String Via paramid and paramname

Where $ NewValue is indicated by NewValue's value of this variable

It can also be used in this way:

============================================================================================================================================================================================================= =======

Paramid = "BooleanProperty"

Paramname = "TestBean" paramproperty = "BooleanProperty">

Boolean Via Paramid, Paramname, And Paramvalue

============================================================================================================================================================================================================= =======

Here is a paramproperty property, the name attributes here and the property attributes and bean: Write tags, in general, the value of Name is a binding

The key value of the object in the request, session, application, or page range, the value of the Property property is the name of a member variable of this object

After using Name and Property, Struts will look for objects in the above four ranges, and after reading the object, remove the Property definition.

Value of member variables.

So the above label is converted to an HTML code as follows:

Boolean Via paramid, paramname, and paramvalue

OK, after understanding, I feel a hint of regret, because if there is only three attributes of paramid, paramname, paramproperty, then for the HTML: Link tab, you can only customize a parameter, if you need to define multiple parameters ,Such as

http:// serverip: portno / projectContextName / html-link.do? a = 3 & b = 5 & y = 9 This link URL is invincible.

If you want to define multiple parameters, you need to manually fight URLs, define inside the page attribute, which loses the meaning of using the label.

In this case, with a disappointed mood, open the Struts source code, start to view the source code of this tag, and suddenly discover, this tag is completely customized, the key is on the Name property.

First come to see how to customize the code of multiple parameters, as follows:

============================================================================================================================================================================================================= =======

<%

Java.util.hashmap new severues = new java.util.hashmap ();

NewValues.Put ("FloatProperty", New Float (444.0));

NewValues.Put ("intProperty", New Integer (555));

NewValues.Put ("StringArray", New String []

{"Value1", "Value2", "Value3"});

PageContext.SetaTRibute ("NewValues", NewValues;

%>

Float, int, And StringArray Via Name (Map)

============================================================================================================================================================================================================= ======= Originally using the Name property can put a MAP type variable into the REQUEST, SESSION, Application, Page four-range containers, then take a key value, fill in the Name property.

Struts will automatically read all the objects defined by the Map type object, and then automatically generate HTML code.

It is like the above examples, which will generate the following HTML code:

Float, int, and stringArray via name (map)

The desire has finally reached, but also fully proves the strength of Struts, but in this implementation, finding the object of Name defined this is clear in the previous source profiling (used is the Lookup method in tagutils.java) )

But after this object is obtained, how is the Struts read the key value collection and a key value corresponding to the key value? Code here:

============================================================================================================================================================================================================= =======

// Add the required request parameters

Boolean Question = Temp.Indexof ('?')> = 0;

// The params variable here is the object we entered the MAP type.

// This is the code to get the key value collection.

Iterator Keys = params.keyset (). Iterator (); while (keys.hasnext ()) {

String Key = (string) keys.next ();

Object value = params.get (key);

IF (value == null) {

IF (! quothes) {

URL.APPEND ('?');

Question = True;

} else {

Url.append (separator);

}

Url.Append (this.encodeurl (key));

Url.append ('='); // Interpret Null AS "No Value"

} else if (Value InstanceOf String) {

IF (! quothes) {

URL.APPEND ('?');

Question = True;

} else {

Url.append (separator);

}

Url.Append (this.encodeurl (key));

URL.APpend ('=');

Url.Append (this.EncodeURL (String) Value);

} else if (Value InstanceOf String []) {

String Values ​​[] = (String []) Value;

For (int i = 0; i

IF (! quothes) {

URL.APPEND ('?');

Question = True;

} else {

Url.append (separator);

}

Url.Append (this.encodeurl (key));

URL.APpend ('=');

Url.Append (this.EncodeURL (Values ​​[i]));

}

} Else / * Convert Other Objects to a string * / {

IF (! quothes) {

URL.APPEND ('?');

Question = True;

} else {

Url.append (separator);

}

Url.Append (this.encodeurl (key));

URL.APpend ('=');

Url.Append (this.encodeurl (value.tostring ()));

}

}

============================================================================================================================================================================================================= =======

It is no longer necessary to do excessive explanation, the code has explained everything.

In fact, in PropertyMessageResource.java, there are also similar code, which is a piece of code that reads the Property file. When the ActionServlet is initialized, this code can be found in the bean: Message label research.

Finally, what I want to say is that the Struts' source code is quite excellent, non-stop learning is very beneficial to improve their level.

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

New Post(0)