Jsp tag instance

Source: Internet
Author: User

 

1. problem: the People object in the request has a property named men, and men is a Collection with many man. Now, all man names in the collection are displayed.

Obviously, this is a nested Tag problem. Three tags interact with each other: the outermost Tag finds the People object, the Tag in the middle gets the Collection, and the sub-Tag is responsible for printing.

For example:

<Diego: withObject value =$>

<Diego: withCollection property = men>

<Diego: elementout property = name/>

</Diego: withCollection>

</Diego: withObject>

The idea is as follows:

1) Write WithObjectTag to obtain the object from the El expression.

2) Compile WithCollectionTag to retrieve Collection from the object and traverse Collection. Each time a Collection is traversed, the body is executed.

3) Write ElementoutTag to print the name of each men object in the Collection.

2. the complete procedure is as follows:

In the preceding diegoyun. vo package, 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 of the People object obtained from the 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 obtains Collection and traverses and executes sub-tags.

Package 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 iterator = null;

Public Object getElement (){

Return element;

}

Public void setProperty (String property) throws JspException {

// Get the parent Tag object and get the 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 and then execute the sub-Tag

Iterator = list. iterator ();

If (iterator. hasNext ())

Element = iterator. next ();

Return EVAL_BODY_INCLUDE;

}

Public int doAfterBody (){

If (iterator. hasNext ()){

// If a child element exists, set the child element and execute the child Tag again.

// Loop

// Otherwise, the sub-Tag will not be executed

Element = iterator. next ();

Return EVAL_BODY_AGAIN;

}

Else

Return EVAL_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 );

Try

{

// Determine whether the attribute name exists in the upper-layer tag. If yes, obtain the attribute value. Otherwise, an error is returned.

PropertyValue = PropertyUtils. getProperty (parent. getElement (), property );

}

Catch (Exception e)

{

Throw new JspException (e );

}

}

Public int doEndTag () throws JspException

 

Catch (IOException e)

{

Throw new JspException (e );

}

Return EVAL_PAGE;

}

}

Write tld

<! -- WithObjectTag -->

<Tag>

<Name> withObject </name>

<Tag-class> diegoyun. WithObjectTag </tag-class>

<Body-content> JSP </body-content>

<Attribute>

<Name> value </name>

<Required> false </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

</Tag>

<! -- WithCollectionTag -->

<Tag>

<Name> withCollection </name>

<Tag-class> diegoyun. WithCollectionTag </tag-class>

<Body-content> JSP </body-content>

<Attribute>

<Name> property </name>

<Required> false </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

</Tag>

<! -- ElementOutputTag -->

<Tag>

<Name> elementout </name>

<Tag-class> diegoyun. ElementOutputTag </tag-class>

<Body-content> empty </body-content>

<Attribute>

<Name> property </name>

<Required> false </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

</Tag>

Compile jsp

<% @ Page language = java %>

<% @ Page import = diegoyun. vo. * %>

<% @ Page import = java. util. * %>

<% @ Taglib uri =/WEB-INF/tlds/diego. tld prefix = diego %>

<Html>

<Body bgcolor = # FFFFFF>

<%

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. setAttribute (people, p );

%>

Test loop tag:

<Br>

<Diego: withObject value =$>

<Diego: withCollection property = men>

<Diego: elementout property = name/>

<Br>

</Diego: withCollection>

</Diego: withObject>

</Body>

</Html>

Run, you can see:

Test loop tag:

Diego

Zidane

Rui

Related Article

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.