TAGLIB principle and implementation: Chapter 3 Nested and attribute reading between tags

xiaoxiao2021-03-06  77

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: Object supports EL expression, indicating that the MAN object is indicated. Output's Property represents the properties named Name from this object. 2. How to support Nested TAG to call the getParent method in sub-tag, you can get the parent TAG object. With the FindanceStorwithClass method, you can find the TAG you want to find by recursive. For example For the innermost OutputTag, call getParent, you can get WITHCOLLECTIONTAG, through the FindanceStorwithClass (this, Withtag.class), you can get the property after getting the TAG, you can get the property of TAG, logic Process, then output to JSP 3. How to support class properties lookup feature obvious, in the OutputTag above, we have to find this property in the lookup class based on the name of the attribute. The value of the property is then removed and displayed. Typically, this can be written in its own reflection function. A simpler way is to complete the function through the Beanutil's PropertyUtils method. Beanutil is an open source project on Apache. Examples are as follows: import org.apache.commons.BeanUtils.propertyUtils;. . . . . . Property = PropertyUtils.getProperty (CurrentClass, PropertyName); PropertyName is the name of the property to be found, such as "name" above, CurrentClass is the class to be found, such as the top of the top, remember to add commons-beanutils.jar to web-inf / Lib Directory 4. Now let's implement the issued problem, write the Withtag 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

with diegoyun.withtag JSP value false TRUE Nestedout < Tag-class> diegoyun.nestedoutputtag EMPTY Property false FALSE Write JSP page <% @ page language = "java"%> <% @ page import = "Diegoyun.vo. *"%> <% @ Taglib URI = "/ Web-INF /TLDS/diego.tld "prefix =" DIEGO "%>

<% man man = new man (); man.setname ("Diego");

Request.setttribute ("Man", Man);%> Test Nested Tag:

You can see: Test NESTED TAG: DIEGO

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

New Post(0)