Home > front end >  How do you get a custom JSP tag to evaluate tags in its body
How do you get a custom JSP tag to evaluate tags in its body

Time:02-06

I am writing a custom JSP tag. What I want my tag to do is check some conditions, and if the condition is TRUE, then evaluate the JSP/HTML contained in the body. Otherwise, redirect to another page.

The redirect part works just fine - the problem is that I can't (seem to) get the custom tag to evaluate any JSP in the custom tag's body.

My tag looks like this:

public class MyTag extends BodyTagSupport implements BodyTag
{
    private function checkImportantCondition()
    {
         ...
    }

    public int doStartTag() throws JspException
    {
        int retVal = SKIP_BODY;
        if(checkImportantCondition()==false)
        {
            try
            {
            pageContext.forward("myredirectPage.jsp");
            }
            catch (ServletException e) 
            {
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
        else
        {
            retVal = EVAL_BODY_BUFFERED;  // Why doesn't this cause the JSP body to be evaluated?
        }
        return retVal;
    }
}

My JSP that uses the custom tag then looks like this:

...
<%@ taglib uri="http://my.org.name/MyTag" prefix="myt" %>

<myt:MyTag>
    <h:form id="form1">
         ... some other JSP tags here
    </h:form>
</myt:MyTag>

Now, when the checkImportantCondition() method returns false, it successfully redirects to myredirectpage.jsp.

However, when the checkImportantCondition() method returns true, the <h:form> tag and all the other jsp tag's that are inside the custom myt:MyTag tag - are not evaluted - instead, I just get a blank screen.

Can anyone advise what I need to do? I was under the impression that returning EVAL_BODY_BUFFERED from doStartTag() method should cause the subsequent JSP tags in the body to be evaluated, but this doesn't seem to be happening.

Thanks heaps :-)

CodePudding user response:

I figured out what was wrong. It wasn't in the Java code at all. It was in my associated mytag.tld. I needed to change the tag to JSP, whereas previously it was tagdependent.

The myTag.tld was:

<taglib version="2.1" 
        xmlns="http://java.sun.com/xml/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
  <tlib-version>2.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>MyTag</short-name>
  <uri>http://my.org.name/MyTag</uri>
  <tlib-version>1.0</tlib-version>
  <description>
     
  </description>
  <tag>
    <name>MyTag</name>
     <tag-class>my.org.name.MyTag</tag-class>
    <body-content>tagdependent</body-content>        
  </tag>
</taglib>

But I changed the myTag.tld to this (note the change in the tag:

<taglib version="2.1" 
        xmlns="http://java.sun.com/xml/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
  <tlib-version>2.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>MyTag</short-name>
  <uri>http://my.org.name/MyTag</uri>
  <tlib-version>1.0</tlib-version>
  <description>
     
  </description>
  <tag>
    <name>MyTag</name>
     <tag-class>my.org.name.MyTag</tag-class>
    <body-content>JSP</body-content>        
  </tag>
</taglib>

Having made this change, the custom tag now evaluates the JSP in the body.

  • Related