Taglib principle and implementation: Chapter 4 Cycling TAG

xiaoxiao2021-03-06  77

Fourth article supports looped TAG

1. Question: In the prople object in the request, there is a property called Men, MEN is a collection, there are many Man. Now, put the name of the Man in the Collection

Obviously, this is a nesting TAG problem. There are three TAG mutual effects: the outermost TAG finds the people object, the middle tag gets Collection, and the sub-tag is responsible for printing. For example: The idea is as follows : 1. Write WITHOBJECTTAG, responsible for obtaining objects from the EL expression. Write the WITHCOLLECTIONTAG, responsible for getting Collection, traversing Collection, traversing the Collection, executing a Body 3. Write ElementTAG, putting each MEN object in Collection Name Prints 2. The full program is as follows: In the Diegoyun.vo package in the previous example, 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, this is the outermost TAG that gets the people object from Request

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, which is responsible for getting Collection and traverses the executive tagpackage 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 ity = null;

Public Object getElement () {return Element;

public void setProperty (String property) throws JspException {// Parent Tag acquisition target, and the resulting 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 {// Set the first element, then execute sub-tag itrator = list.iterator (); if (Iterator.hasNext ()) ELEMENT = Iterator.next ();

Return Eval_Body_includ;

Public int doafterBody () {if (Iterator.hasNext ()) {// If there is a child element, set a sub-element, and the sub-tag // loop again is // otherwise no longer perform sub-tag element = Iterator .next (); RETURN EVAL_BODY_AGAIN;} else returnval_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") {// determine whether the attribute name exists in the upper tag, if present, obtain the attribute value, otherwise error PropertyValue = PropertyUtils.getProperty (parent.getLew (), Property;} catch (exception e) {throw new jspexception e);}} public int doendtag () throws jspexception {try {// Simple value prints to JSP page PageContext.getut (). PropertyValue;} catch (ioException e) {throw new JSPEXCEPTION (E); } Return Eval_page;}}

Write a TLD withobject diegoyun.withobjectTag JSP value false TRUE withcollection diegoyun.withcollectionTag jsp proty false TRUE elementout diegoyun.ementputputtag Empty value 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 ();

MAN Man1 = new man (); man1.setname ("DIEGO"); C.Add (man1);

MAN man2 = new man (); man2.setname ("zidane"); C.ADD (man2);

Man Man3 = new man (); man3.setname ("rui"); C.Add (man3);

People p = new people (); p.setmen (c); Request.settribute ("people", p);%> test loop tag:

You can see: test LOOP TAG: Diego Zidane Rui

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

New Post(0)