JSP and JSTL Tutorials - Tutorial Addendum - Tags Alive Calm

android appcolt wargamesios appcolt
 31 December 18:00   

    



    



    



    

Nested JSTL Tags

    



    

When tags are nested, JSP tag addendum ability offers the adolescent tag an

    

access to the ancestor tag by the setParent() adjustment call. You can yield advantage

    

of this admission to architecture nested tags to plan calm to accomplish added complex

    

functions.

    



    

As an example, I wrote two simple tags to accomplish a bend action with the

    

ability to breach out the bend conditionally.

    



    

Here is my bend tag, LoopTag.java:

    



    

/**

    

* LoopTag.java

    

* Absorb (c) 2003 by Dr. Yang. All rights reserved.

    

*/

    

package ;

    

import java.io.*;

    

import javax.servlet.jsp.tagext.*;

    

public chic LoopTag extends TagSupport {

    

accessible boolean stop = false;

    

accessible int doStartTag() {

    

acknowledgment EVAL_BODY_INCLUDE;

    

}

    

accessible int doAfterBody() {

    

if (!stop) acknowledgment EVAL_BODY_AGAIN;

    

abroad acknowledgment SKIP_BODY;

    

}

    

}

    



    



    

The argumentation of the "loop" tag is actual simple. It evaluates its physique anon already

    

the tag is encountered. At the end of the evaluation, it checks its "stop" condition,

    

if "stop" is false, it evaluates its physique again. This bend will be chock-full alone

    

if "stop" is set to true, during the physique evaluation.

    



    

Here is the breach tag, BreakTag.java:

    



    

/**

    

* BreakTag.java

    

* Absorb (c) 2003 by Dr. Yang. All rights reserved.

    

*/

    

package ;

    

import .*;

    

import javax.servlet.jsp.tagext.*;

    

public chic BreakTag extends TagSupport {

    

clandestine Tag ancestor = null;

    

accessible abandoned setParent(Tag t) {

    

ancestor = t;

    

}

    

accessible int doStartTag() {

    

if (parent!=null) {

    

LoopTag grandParent = (LoopTag) parent.getParent();

    

if (grandParent!=null) grandParent.stop = true;

    

}

    

acknowledgment SKIP_BODY;

    

}

    

}

    



    



    

The argumentation of the "break" tag is a little bit added complex. To breach the "loop" tag

    

conditionally, we are assured an "if" tag will be nested central the "loop" tag

    

to ascendancy the condition,

    

and the "break" tag will be nested central the "if" tag to breach the loop.

    

So the "break" tag is infact nested 2 levels down central the "loop" tag.

    

This is why I accept to use "parent.getParent()" to get the admirable ancestor

    

to admission the "loop" tag.

    



    

Here is the TLD file:

    

 

    

<?xml version="1.0"?>

    

<!DOCTYPE taglib Accessible

    

"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"

    

"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_2.dtd">

    

<!-- HyTaglib.tld

    

Absorb (c) 2003 by Dr. Yang

    

-->

    

<taglib>

    

<tlib-version>1</tlib-version>

    

<jsp-version>1.2</jsp-version>

    

<short-name> s Tag Library</short-name>

    

<tag>

    

<name>loop</name>

    

<tag-class>.LoopTag</tag-class>

    

<body-content>jsp</body-content>

    

</tag>

    

<tag>

    

<name>break</name>

    

<tag-class>.BreakTag</tag-class>

    

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

    

</tag>

    

<!-- additional tags -->

    

</taglib>

    



    



    

Here is a analysis page, LoopTagTest.jsp:

    

 

    

<?xml version="1.0"?>

    

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"

    

xmlns:c="http://java.sun.com/jstl/core"

    

xmlns:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="1.2">

    

<!-- LoopTagTest.jsp

    

Absorb (c) 2003 by Dr. Yang

    

-->

    

<jsp:directive.page contentType="text/html"/>

    

<html><body>

    

<c:set var="i" value="${1}"/>

    

<c:set var="s" value="${0}"/>

    

<hy:loop>

    

<c:set var="s" value="${s+i}"/>

    

<c:set var="i" value="${i+1}"/>

    

<c:if test="${i gt 10}">

    

<hy:break/>

    

</c:if>

    

</hy:loop>

    

<c:out value="Sum = ${s}"/><br/>

    

</body></html>

    

</jsp:root>

    



    

(Continued on next part...)

    



    

 


Tags: class, content, together, value, parent, notes, public, tutorial, tutorials, working, inside

 public, version, parent, class, nested, looptag, value, content, return, import, copyright, taglib, xmlns, together, grandparent, inside, breaktag, , < tag, tag <, tag class, < body, loop tag, < jsp, public int, html <, inside the, http java, name <, java sun, sun com, return eval body, return skip body, skip body }}the, < body content, import javax servlet, tags working together, tutorials tutorial notes, tutorial notes tags, notes tags working, jstl tutorials tutorial,

Share JSP and JSTL Tutorials - Tutorial Addendum - Tags Alive Calm:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  jsp