Avatar billede fredand Forsker
23. februar 2006 - 22:22 Der er 1 løsning

Problems with BodyTag

Hello!

I got a problem with a tag that extends BodyTagSupport. The tag has a body that should be displayed if the session contains a certain value. The problem is this that the body contains an other tag that always need a value form the page context. The second value is stored in the page context by the first tag when the body should be displayed.

The problem there for is if the body is not to be displayed, there will be no second value in the page context. There for a Exception (Nullpointer I guess) is thrown from the second tag.

In The JSP the code looks like:


<mytags:ProcedureFormMenuTag>

<html:link page="/tiles/setupproduct.do" name="paramsname0" scope="page">Procedure</html:link>

</mytags:ProcedureFormMenuTag>

The object paramsname0 is set into page context by mytags:ProcedureFormMenuTag if the body should be displayed. But if it is not set the Exception is thrown by the Struts tag html:link.

My code for mytags:ProcedureFormMenuTag looks like:

public class ProcedureFormMenuTag extends BodyTagSupport
{
    public int doAfterBody()
    {
        try
        {
            HttpSession session = pageContext.getSession();

            if(session.getAttribute("selectedproduct") != null)
            {
                   
                HashMap procedureFormMenuLinkParams = new HashMap();
                procedureFormMenuLinkParams.put("formindex", "1");
                procedureFormMenuLinkParams.put("procedure_id","2");
                pageContext.setAttribute("paramsname0", procedureFormMenuLinkParams);
            }

            JspWriter jspWriter = bodyContent.getEnclosingWriter();
            jspWriter.print(body);
        }
        catch(Exception js)
        {
            js.printStackTrace();
        }

        return SKIP_BODY;
    }
}

The code is slightly minimized, all html is removed but I guess everything is here.

The question now is how to solve this?

Best regards
Fredrik
Avatar billede fredand Forsker
24. februar 2006 - 21:03 #1
The solution was somewhere in the back of my head:

public class TestBodyTag extends TagSupport
{

    private String value;

    public void setValue(String v)
    {
        value = v;
    }

    public int doStartTag()
    {
        if(value.equals("x"))
        {
            HashMap procedureFormMenuLinkParams = new HashMap();
            procedureFormMenuLinkParams.put("formindex", "1");
            pageContext.setAttribute("param", procedureFormMenuLinkParams);
            return EVAL_BODY_INCLUDE;
        }
        else
        {
            return SKIP_BODY;
        }
    }
}

Best regards
Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester