| 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; } } |
| 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 age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
| <!--WithTag--> <tag> <name>with</name> <tag-class>diegoyun.WithTag</tag-class> <body-content>JSP</body-content> <attribute> <name>value</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <!--OutputTag3--> <tag> <name>nestedout</name> <tag-class>diegoyun.NestedOutputTag</tag-class> <body-content>empty</body-content> <attribute> <name>property</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> |
| <%@ page language="java" %> <%@ page import="diegoyun.vo.*"%> <%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%> <html> <body bgcolor="#FFFFFF"> <% Man man = new Man(); man.setName("diego"); request.setAttribute("man",man); %> Test nested tag: <br> <diego:with value="${man}"> <diego:nestedout property="name"/> </diego:with> </body> </html> |
评论加载中…
![]() |