Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HF&J - pg 543 - Re-writing Menu & MenuItems using SimpleTag

 
Phoebe Jones
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To see how much simpler it is to user Simple Tag -

package myTagHandler;
import javax.servlet.jsp.JspException;
import java.io.IOException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.ArrayList;

public class MenuSimple extends SimpleTagSupport {
private ArrayList items = new ArrayList();
public void addMenuItem ( String item) {
items.add ( item );
}

public void doTag() throws JspException, IOException {
getJspBody().invoke(null);
getJspContext().getOut().println (" Menu items are: " + items + "<br>" );
}
}

package myTagHandler;
import javax.servlet.jsp.JspException;
import java.io.IOException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class MenuItemSimple extends SimpleTagSupport {
private String itemValue;

public void setItemValue (String value) {
itemValue = value;
}

public void doTag() throws JspException, IOException {
MenuSimple parent = (MenuSimple) getParent();
if ( parent != null ) {
parent.addMenuItem (itemValue); // Call parent's method
}
}
}




--- to test it ---

<%@ taglib prefix="mine" uri="MyTagLibrary" %>
<html><body>

<h3>TestMenuSimple.jsp</h3>

<mine:MenuSimple>
<mine:MenuItemSimple itemValue="Dogs" />
<mine:MenuItemSimple itemValue="Cats" />
<mine:MenuItemSimple itemValue="Horses" />
</mine:MenuSimple>

</body></html>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic