Else... If...
<C: If test = ""> AAA </C: If> -- iftag. Java
<C: else> BBBB </C: else> -- elsetag. Java
Put in parent label
Let the IF and else object processors share a Boolean object to notify the respective label processors whether to execute the following code
<C: Choose> -- choosetag. Java
<C: whentest = "$ {user! = NULL} "> -- whentag. Java
This is my page
</C: When>
<C: Other> -- othertag. Java
If user is not ex
</C: Other>
</C: Choose>
Code:
Choosetag. Java
Private BooleanFlag =False;
// To read the flag value in the subclass, all get methods are required.
Public BooleanIsflag (){
ReturnFlag;
}
Public voidSetflag (BooleanFlag ){
This. Flag = flag;
}
@ Override
Public voidDotag ()ThrowsJspexception, ioexception {
//TodoAuto-generatedmethod stub
Jspfragment JF =This. Getjspbody ();
JF. Invoke (Null);
}
Otherwise. Java section:
Public voidDotag ()ThrowsJspexception, ioexception {
//TodoAuto-generatedmethod stub
// Obtain the parent tag object
Choosetag parent = (choosetag)This. Getparent ();
If(! Parent. isflag ()){
This. Getjspbody (). Invoke (Null);
Parent. setflag (True);
}
}
Whentag. Java
Private BooleanTest;
Public voidSettest (BooleanTest ){
This. Test = test;
}
@ Override
Public voidDotag ()ThrowsJspexception, ioexception {
//TodoAuto-generatedmethod stub
// Parent tag object
Choosetag parent = (choosetag)This. Getparent ();
If(Test &&! Parent. isflag () {// test is true, and no branch has been executed
// Process the TAG body
This. Getjspbody (). Invoke (Null);
Parent. setflag (True);
}
}
Testif. jsp
<% Session. setattribute ("user", "zhangsan"); %>
<Br/> __________________________________ <br/>
<! -- Test the IF and else labels -->
<C: Choose>
<C: When test ="$ {User! = NULL}">Aaaaa</C: When>
<C: otherwise>Bbbbbb</C: otherwise>
</C: Choose>
C. TLD Section
<? XML version ="1.0"Encoding =UTF-8"?>
<! Doctype taglib public "-// sunmicrosystems, Inc. // dtd jsp tag library 1.2 //" http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd ">
<Taglib>
<Tlib-version> 1.0 </tlib-version>
<JSP-version> 1.2 </JSP-version>
<Short-Name> C </short-Name>
<URI> http://www.c.com </uri>
<Tag>
<Name> If </Name>
<Tag-class> com. hbsi. Tag. iftag </Tag-class>
<Body-content>Scriptless</Body-content>
<Attribute>
<Name> test </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
</Tag>
<Tag>
<Name> choose </Name>
<Tag-class> com. hbsi. Tag. choosetag </Tag-class>
<Body-content>Scriptless</Body-content>
</Tag>
<Tag>
<Name> when </Name>
<Tag-class> com. hbsi. Tag. whentag </Tag-class>
<Body-content>Scriptless</Body-content>
<Attribute>
<Name> test </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
</Tag>
<Tag>
<Name> otherwise </Name>
<Tag-class> com. hbsi. Tag. otherwisetag </Tag-class>
<Body-content>Scriptless</Body-content>
</Tag>
</Taglib>