JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»

<simp:formparam name=»username»> Username was not found </simp:formparam>


In this JSP fragment, we would expect the tag to send the client the «Username was not found» message when the form parameter username isn’t found.

NOTE You may be asking why you would use the tag’s body to specify an alternative content and not some other attribute (e.g., <simp:formparam name=»paramname» alt=»»paramname» was not provided»/>). Using an attribute to specify alternative values is possible, but not as flexible as using the body. The body lets the alternative content be as complex and dynamic as necessary; tag attributes are much more limited. It can also be looked at as a style issue as well, where you can easily wrap your alternative content between start and end tags rather than burying it in an attribute and worrying about quote delimiting and other tedious formatting issues.

To enable this feature in ShowFormParamTag’s handler class requires a minimal change (confined to a single method doStartTag()), as illustrated in listing 6.20.

Listing 6.20 Modifying ShowFormParamTag’s handler class to make it body aware

public class ShowFormParamBodyTag extends ExTagSupport {

// Some code was removed

public int doStartTag()

throws JspException


try {

HttpServletRequest req =

(HttpServletRequest)pageContext.getRequest(); String value = req.getParameter(name); if(null != value) {

writeHtml(pageContext.getOut(), value); return SKIP_BODY; Q


} catch(java.io.IOException ioe) {

// User probably disconnected …

// log an error and throw a JspTagException




Скачать в pdf «JSP Tag Libraries»