Loop 1, question: In the request of the people object, there is a property called men, Men is a collection, there are many mans. Now, show the name of the man in the collection.
Obviously, this is a problem with nesting tag. There are three tag interaction: the outermost tag to find people objects, the middle of the tag to obtain collection, the child tag is responsible for printing.
For example:
Ideas are as follows:
1 Write Withobjecttag, responsible for obtaining objects from El expressions
2 Write Withcollectiontag, responsible for obtaining Collection from the object, traversing the Collection, every traverse Collection, perform a body
3 Write Elementouttag to print out the name of each men object in Collection
2. The complete procedure is as follows:
In the DIEGOYUN.VO package of the example above, write the People class
Package diegoyun.vo; Import java.util.Collection; public class people { Private Collection men = null; Public Collection Getmen () { return men; } public void Setmen (Collection men) { This.men = men; } } |
Write Withobject, which is the outermost tag from the request to get people objects
Package Diegoyun; Import javax.servlet.jsp.JspException; Import Javax.servlet.jsp.tagext.BodyTagSupport; Import Org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; public class Withobjecttag extends Bodytagsupport { Private Object value = null;
Public Object GetValue () { return value; } public void SetValue (Object value) throws Jspexception { This.value = Expressionevaluatormanager.evaluate ("Value", value.tostring (), Object.class, this, PageContext); } public int doStartTag () { return eval_body_include; } public int Doendtag () throws Jspexception { return eval_page; } } |
Write Withcollectiontag, the tag is responsible for getting collection, and traversing the execution sub tag
Package Diegoyun;
Import java.util.Collection; Import Java.util.Iterator;
Import javax.servlet.jsp.JspException; Import Javax.servlet.jsp.tagext.BodyTagSupport;
Import Org.apache.commons.beanutils.PropertyUtils;
public class Withcollectiontag extends Bodytagsupport { Private Object element = null;
Private Collection list = null;
Private iterator iterator = null;
Public Object getelement () { return element; }
public void SetProperty (String) throws Jspexception { Gets the parent tag object and obtains the collection Withobjecttag parent = (Withobjecttag) getparent (); if (parent = null) throw new Jspexception ("Parent tag is null"); try { Object propertyvalue = Propertyutils.getproperty (Parent.getvalue (), property); This.list = (Collection) propertyvalue; if (list = = null) throw new Jspexception ("Collection is null"); catch (Exception e) { throw new Jspexception (e); } }
public int doStartTag () throws Jspexception { Sets the first element and then executes the child tag iterator = List.iterator (); if (Iterator.hasnext ()) element = Iterator.next (); return eval_body_include; }
public int doafterbody () { if (Iterator.hasnext ()) { If a child element is still present, set the child element, and execute the child tag again Loops that come from this Otherwise the child tag is no longer executed element = Iterator.next (); return eval_body_again; } Else return eval_page; } } |
Write Elementoutputtag
Package Diegoyun; Import java.io.IOException;
Import javax.servlet.jsp.JspException; Import Javax.servlet.jsp.tagext.TagSupport;
Import Org.apache.commons.beanutils.PropertyUtils;
public class Elementoutputtag extends TagSupport { Private Object propertyvalue = null; public void SetProperty (String property) throws Jspexception { Withcollectiontag parent = (Withcollectiontag) getparent (); if (parent = null) throw new Jspexception ("Parent tag is null"); Try { To determine if there is a property name in the upper tag, if it exists, get the property value, otherwise the error PropertyValue = Propertyutils.getproperty (Parent.getelement (), property); } catch (Exception e) { throw new Jspexception (e); } } public int Doendtag () throws Jspexception { Try { Simply print the value to the JSP page Pagecontext.getout (). print (PropertyValue); } catch (IOException E) { throw new Jspexception (e); } return eval_page; } } |
Writing TLD
!--withobjecttag--> Withobject Diegoyun. Withobjecttag JSP value false true !--withcollectiontag--> withcollection Diegoyun. Withcollectiontag JSP Property false true !--elementoutputtag--> Elementout Diegoyun. Elementoutputtag Empty Property false true |
Writing JSP
<%@ page language= "java"%> <%@ page import= "diegoyun.vo.*"%> <%@ page import= "java.util.*"%> <%@ taglib uri= "/web-inf/tlds/diego.tld" prefix= "Diego"%>
<% Collection C = new ArrayList ();
Mans man1 = new Man (); Man1.setname ("Diego"); C.add (MAN1);
Mans man2 = new Man (); Man2.setname ("Zidane"); C.add (MAN2);
Mans MAN3 = new Man (); Man3.setname ("Rui"); C.add (MAN3);
People p =new people (); P.setmen (c); Request.setattribute ("People", p); %> Test Loop Tag:
|
Run, you can see:
Test Loop Tag: Diego Zidane Rui |