简体   繁体   中英

Why pagecontext in customtag cause Null Exception

I try to code demo about custom tag , but this problem .. I hate it, I can solve it by my self.

here is my .TLD file:

<tag>
    <name>custom</name>
    <tagclass>MyTag.CustomerTag</tagclass>
    <bodycontent>empty</bodycontent>
</tag>

And here is my class CustomerTag:

public class CustomerTag extends TagSupport {

    private PageContext _pageContext;

    @Override
    public int doStartTag() throws JspException {
        try {
            _pageContext.getOut().println("kakaka"); <-- **here cause exception**
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        return SKIP_BODY;
    }

    @Override
    public int doEndTag() throws JspException {
        return SKIP_PAGE;
    }
}

Here is my JSP file:

<%@taglib uri="/WEB-INF/Marko.tld"  prefix="myTag" %>
<myTag:custom />

Please help me, thanks everyone

In your code you are defining a separate _pageContext field that you never initialize; however, by extending TagSupport you should have access to a field pageContext that JSP initializes for you automatically.

Try removing the variable declaration and calling pageContext.getOut().println("kakaka"); instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM