1. Implement the myswitchtag class
Package class3g. web. tag; import Java. io. ioexception; import javax. servlet. JSP. jspexception; import javax. servlet. JSP. tagext. simpletagsupport; public class myswitchtag extends simpletagsupport {private int exp; private Boolean lastcase_break = false; // Private Boolean lastcase_done = false; // Private Boolean terminate = false; Public Boolean islastcase_break () {return lastcase_break;} public void setlastcase_break (Boolean lastcase_break) {This. lastcase_break = lastcase_break;} public Boolean islastcase_done () {return lastcase_done;} public void setlastcase_done (Boolean lastcase_done) {This. lastcase_done = lastcase_done;} public Boolean isterminate () {return terminate;} public void setterminate (Boolean terminate) {This. terminate = terminate;} public int getexp () {return exp;} public void setexp (INT exp) {This. exp = exp ;}@ overridepublic void dotag () throws jspexception, ioexception {This. getjspbody (). invoke (null );}}
Implement the mycasetag class
package class3g.web.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class MyCaseTag extends SimpleTagSupport {private int value;private boolean breakSwitch;public void setValue(int value) {this.value = value;}public void setBreakSwitch(boolean breakSwitch) {this.breakSwitch = breakSwitch;}@Overridepublic void doTag() throws JspException, IOException {MySwitchTag parent = (MySwitchTag) this.getParent();if (parent.isTerminate() == false) {if (parent.isLastCase_done() == false) {if (parent.getExp() == value) {this.getJspBody().invoke(null);parent.setLastCase_done(true);parent.setTerminate(breakSwitch);}} else {if (parent.isLastCase_break() == false) {this.getJspBody().invoke(null);parent.setLastCase_done(true);parent.setLastCase_break(breakSwitch);parent.setTerminate(breakSwitch);}}}}}
Implement the mydefatag tag class
package class3g.web.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class MyDefaultTag extends SimpleTagSupport {@Overridepublic void doTag() throws JspException, IOException {MySwitchTag parent = (MySwitchTag) this.getParent();if(parent.isTerminate() == false){this.getJspBody().invoke(null);parent.setTerminate(true);}}}
2. Modify the TLD File
<tag><description>switch tag</description> <name>switch</name><tag-class>class3g.web.tag.MySwitchTag</tag-class><body-content>scriptless</body-content><attribute><name>exp</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute> </tag> <tag><description>case tag </description> <name>case</name><tag-class>class3g.web.tag.MyCaseTag</tag-class><body-content>scriptless</body-content><attribute><name>value</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>breakSwitch</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute> </tag> <tag><description>default </description> <name>default</name><tag-class>class3g.web.tag.MyDefaultTag</tag-class><body-content>scriptless</body-content> </tag>
3. Use custom tags in JSP
<G: Switch exp = "2"> <G: Case value = "1" breakswitch = "true"> 11111111111 <br/> </G: Case> <G: case value = "2" breakswitch = "false"> 22222222222222 <br/> </G: Example> <G: case value = "3" breakswitch = "true"> 333333333333333 <br/> </G: Case> <G: Default> dddddddddddddddddddd <br/> </G: default> </G: Switch> <! -- Switch: has an exp attribute. The three member variables terminate are used as the marker for interrupted break execution. lastcase_done: The execution status of the previous case tag. lastcase_break: The Break value status of the previous case tag. case: two attributes: Value and Boolean breakswitch. No member default: No attribute, no member -->