Taglib principle and implementation of nesting and attribute reading

Source: Internet
Author: User
1. Question: There is a man object in the request, which has two attributes: Name and age. Now, we want to use a nested tag, the parent tag gets the object, the child tag gets the Name property and is displayed on the page. For example, its form is as follows:




Object supports El expressions, representing the acquisition of a man object. The property of output indicates that an attribute named name is obtained from the object.

2, how to support the nesting between the tag

Call the GetParent method in the child tag, you can get the parent tag object. Using the Findancestorwithclass method, you can find the tag that you want to find by recursion. For example



the!--Gets the Name property and displays the-->


For the outputtag of the inner layer, the call to GetParent can get withcollectiontag, through such as Findancestorwithclass (this, Withtag.class) in a way that can get withtag. After the tag, you can get tag properties, business logic processing, and then output to the JSP

3, how to support the Class property lookup function

Obviously, in the above outputtag, we need to find out if there is a property in the class based on the name of the attribute. The value of the property is then fetched and displayed. Typically, this can be done by writing your own reflection functions. A simpler approach is to complete the function by Beanutil the 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 look for, such as "name" above, Currentclass is the class to look for, such as the people above

Remember to add Commons-beanutils.jar to the Web-inf\\\\lib directory

4. Let us now implement the questions raised at the outset and prepare 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)
{
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 under the package Diegoyun Vo, write a man class under Vo:

Package diegoyun.vo;

/**
* @author Chenys
*/
public class Mans
{
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;
}
}

Write TLD

!--withtag-->

with
Diegoyun. Withtag
JSP

value
false
true


!--outputtag3-->

Nestedout
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 mans = New Man ();
Man.setname ("Diego");

Request.setattribute ("Man", Mans);
%>
Test nested tag:







When you run the page, you can see:

Test nested tag:
Diego


5. Conclusion:

The above example simply depicts how the nested tag interacts with each other. Usually the child tag is responsible for obtaining the data, then set the parent tag properties, and finally in the parent tag to display to the JSP page. As in the example above, the output of the parent Tag represents the object to be printed, the value of name is obtained by Nestedouttag, the output is set, and then printed out.

By supporting El Expressions and dynamic property joins, tag can achieve powerful processing capabilities. The logic is concentrated in the tag, greatly simplifying the writing of the page.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.