Nested and attribute reading between the third TAG
1. Question: There is a Man object in the request, which has two properties: Name and AGE. Now, we want to use a nested TAG, the parent TAG gets the object, and the sub-tag gets the Name property and appears on the page. For example, its form is as follows:
Package diegoyun;
Import java.io.ioException;
Import javax.servlet.jsp.jspexception; import javax.servlet.jsp.tagext.bodytagsupport;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; / ** * @author chenys * / public class WithTag extends BodyTagSupport {private Object value = null; private Object output = null;
public void setOutput (Object output) {this.output = output;} 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 {try {pageContext.getOut () print (output);.} catch (IOException e) {Throw new jspexception (e);} Return Eval_page;}}
Write NestedOutputTAG as follows:
Package diegoyun;
Import javax.servlet.jsp.jspexception; import javax.servlet.jsp.tagext.bodytagsupport;
Import org.apache.commons.beanutils.propertyUtils;
/ ** * @author chenys * / public class NestedOutputTag extends BodyTagSupport {private String property = null; public void setProperty (String property) {this.property = property;} public int doEndTag () throws JspException {WithTag parent = (WithTag) getParent (); if (parent == null) throw new JspException ( "Can not find parent Tag"); try {Object propertyValue = PropertyUtils.getProperty (parent.getValue (), property); parent.setOutput (propertyValue);} Catch (Exception E) {throw new jspexception (e);} Return Eval_page;}}
Add a package Vo under the bag diegoyun, write a Man class under Vo:
Package diegoyun.vo;
/ ** * @Author chenys * / public class man {private string name = null; private int Age = 0; public int getage () {Return Age;} public void setage (int agent) {this.age = agn; Public string getname () {return name;} public void setname (string name) {this.name = name;}} write TLD
<% man man = new man (); man.setname ("Diego");
Request.setttribute ("Man", Man);%> Test Nested Tag:
You can see: Test NESTED TAG: DIEGO