Use reflection to extract the beann property as a value
Page 2 (3 pages a total)
Developers typically use Java Reflection to improve custom label code. In this section, we will use Reflection to change your MaPENTRYTAG so that you can use all BEAN attributes within all ranges to define items in the MAP. For example, suppose there is such a bean: public class test {string test = "jenny"; public string gettest () {return test;} public void settest (string string) {test = string;}} After rewritten label Use the Bean property as an item in the map, like this:
Note that the firstname item is now defined with the TEST property of the bean.
In order to do this, we need to add RELECTION in the custom label, like this (see the comment in the code to understand the change process): public class mapenTrytag Extends tagsupport {... / * all of these Have Corresponding Getters and setter method * / String type = "java.lang.String"; // Holder for the type attribute String id; // Holder for the id attribute String value; // Holder for value attribute String name; // Holder for name attribute String property; // Holder for property attribute String scope; // Holder for scope attribute public int doEndTag () throws JspException {MapDefineTag mapDef = (MapDefineTag) this.getParent (); Object objectValue = null; / * Check to see if the value Property is set, if it is life is a simple entry * / if (value! = null) {i ("java.lang.string")) {ObjectValue = value;} else} (Type.equals ("java.lang.integer")) {Integer INTVALUE = INTEGER.VALUEOF (value); objectValue = intValue;} else} (type.equals ("java.lang.float") {float floatvalue = float.valueof Value); ObjectValue = floatvalue;} / * if it is not a simple entry y, then use reflection to get the property from the bean * /} else {Object bean = null; if (scope == null) {bean = pageContext.findAttribute (name);} else if ( "page" .equalsIgnoreCase (scope )) {bean = pageContext.getAttribute (name);} else if ( "request" .equalsIgnoreCase (scope)) {bean = pageContext.getRequest () getAttribute (name);.} else if ( "session" .equalsIgnoreCase (scope )) {bean = pageContext.getSession (). getAttribute ("Application" .EqualsignoreCase (Scope) {bean = pageContext.getServletContext (). GetAttribute (name);
} / * If The Property Attribute Is Null, Then Just Use The bean as the entry * / if (property == NULL) {ObjectValue = bean; mapdef.getmap (). PUT (ID, bean); / * if the property attribute is set, then use reflection to read the property * /} else {try {String propertyMethod = "get" property.substring (0,1) .toUpperCase () property.substring (1, property.length ()) Method prop = bean.getClass () .getMethod (PropertyMethod, New class [] {}); ObjectValue = prop.invoke (bean, new object [] {});} catch (exception e) {throw new runtimeException (e );}}} mapdef.getmap (). PUT (ID, ObjectValue); Return EVAL_PAGE;} It seems to be a lot of work just to achieve some functions of many tags. Fortunately, there is a library that makes this development easy. Analyze this library - Struts in the next section - how works. Simplify custom label development with Struts
Page 3 (3 pages a total)
The in-depth discussion of Struts exceeds the scope of this tutorial (see Resources for more information on this framework). However, if you are familiar with this framework, then this knowledge will help develop custom labels. You can also use the Reflection, and use Struts's requestILS to achieve the results seen in the previous section, as shown below: Public class mapenTrytag Extends tagsupport {... private string type = "java.lang.string"; ... public int doEndTag () throws JspException {MapDefineTag mapDef = (MapDefineTag) this.getParent (); Object objectValue = null; if (! value = null) {if (type.equals ( "java.lang.String")) {objectValue = Value;} else if ("java.lang.integer")) {Integer INTVALUE = INTEGER.VALUEOF (value); objectValue = intValue;} else} ("java.lang.float") ) {Float floatValue = Float.valueOf (value); objectValue = floatValue;}} else {/ ** THIS USED TO BE 30 LINES OF CODE * / objectValue = RequestUtils.lookup (pageContext, name, property, scope);} mapDef .getMap (). PUT (ID, ObjectValue); Return Eval_page;} You can see, ObjectValue = Requestutils.lookup (PageContext, Name, Property, Scope); this line replaces 30 lines in the code using the Reflection! Struts comes with many utilities, making the development of custom labels easier. Introduction
Page 1 (3 pages of)
You can write a label handler to operate its body content. Remember, the textual content of the label is data between the start and end tags that appear in the JSP page. Manipulate the label of its body is called a text tag. Writing a body tag handler is complicated than a simple label handler. Note: Remember that simple labels can also have a body. The only difference is that simple tags cannot read or manipulate their text. To write a text tag handler, you must implement a BodyTag interface. BodyTag implements all methods for TAG implementation (see Step 1: Create a label handler that implements the TAG interface, and also achieves two methods for processing the text content:
Method Destination Void SetBodyContent (BodyContent B) setter method for the Bodycontent property. Void DoinitBody () is ready to judge the body. When the label is called each time, the new BodyContent is obtained and it is called once after setting it via setBodyContent (). If you do not request the text content, it is not called because dostarttag () returns Eval_Body_Buffered. Just like the Tag interface has a Tagsupport class, the Bodytag interface has a Bodytagsupport class. Therefore, the body tag handler only needs to overwrite the methods they want. The Bodytagsupport class inherits Tagsupport and implements the BodyTag interface. This makes it easier to write a body label handler. BodyTagSupport defines get / setBodyContent () and a protected bodycontent instance variable. The Bodytagsupport class redefines the dostartTAG () survival cycle method to return Eval_Body_Buffered. By returning evAl_body_buffered, DostartTag () requests to create a new buffer - a bodycontent. BodyContent is a buffer containing the runtime body judgment result. BodyContent inherited JSPWriter and used the implicit OUT of the label. Therefore, the JSP container creates an instance of the BodyContent and is written to this instance instead of the root JSPWriter when the text of the tag is processed. Therefore, when using implicit objects in the tag, actually uses a BodyContent object rather than JSPWriter (jspwriter is implicit OUT). It can be judged from BodyContent, which is a string. BodyContent is created by the PushBody () and PopBody () method of the PSHBODY () and PopBody () methods that are running by the container call page (only in dostarttag () returns Eval_Body_buffered. Therefore, BodyContent is in a nested structure in JSPWriter and BodyContent. (Out Out can be another BodyContent object, because BodyContent is a jspwriter.) Provide BodyContent to the body tag handler via the setBodyContent () method. Pass a Bodycontent instance to the body tag handler (by setBodyContent ()) and you can decide how to handle it. You can do one step to do it, give it to give it to your browser. It has already introduced enough background knowledge, the analysis code! In the following sections, we will analyze an example of a simple body label. Example: Map label
Page 2 (3 pages a total)
Custom label developers can determine how to handle the body of the tag. For example, you can write a label for executing a SQL statement, the body of the tag is the SQL statement to be executed. Go back to the HashMap theme, we will write a tag that parsing strings like this: {firstname = jennifer, lastname = Wirth, agn = 25} In fact, this tag will read this string and convert it to a MAP (Java.util.map). We will call the new label Map. Here is an example of using a new label: <% @ Taglib URI = "MAP" prefix = "map"%> {firstname = jennifer, lastname = Jones, age = 25}
The Employee Map Is <% = Employee%> The above code creates a map called Employee, which has three: firstname, lastname, and agn.
Implement label handler
Page 3 (3 pages a total)
MapparsetAg is a MAP tag label handler. It defines a map based on the string passed to the body. In the DoafterBody () method, mapparsetag is grabbed by body.getstring () to grab the text content as a string. Then use body.clearbody () to clear the text content. public class MapParseTag extends BodyTagSupport {private String id; private Map map; public int doStartTag () throws JspException {map = new FastTreeMap (); return EVAL_BODY_BUFFERED;} public int doAfterBody () throws JspException {/ * Grab the body content * / BodyContent Body = this.getBodyContent (); / * get the body content as a string * / string content = body.getstring (); / * clear the body * / body.clearbody (); / * Parse the map * / int Start = Content.indexof ("{"); int end = content.lastindexof ("}"); content = content.substring (start 1, end); / * Parse the entries in the map * / stringtokenizer token = new StringTokenizer (Content, "=; / T / R / N"); while (token.hasmoretoKens ()) {string key = token.nextToken (); string value = token.nextToken (); map.put (key, value );} this.pageContext.setttribute (ID, map); return skip_body;} More simple! We have discussed the label for its body, and now we must discuss the label of implementation of the implementation process.