自定义标签拦截及实现

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/lv836735240/article/details/46894785

第一步:首先创建一个tld;

第二步:a.jsp引用tld文件并使用某个标签

第三步:taglib.DTag继承TagSupport 拦截该标签,并把获得的数据库数据放到b.jsp
//b.jsp获得request中set的数据
public String Path()
{
return “/WEB-INF/taglib/b.jsp”;
}
public int doStartTag() throws JspException
{
//1,从数据库加载数据
Service Service = (Service)SpringContextHolder.getBean(“Service”);
List dicItemBeanList =T.query(…);

    //2、将标签信息设置到页面
    getRequest().setAttribute("", );
    return SKIP_BODY;
}

第四步:把b.jsp加载到a.jsp

public int doEndTag() throws JspException
{
try
{
ServletContext servletContext = pageContext.getServletContext();
//加载b.jsp
RequestDispatcher dispatcher = servletContext.getRequestDispatcher(Path());
TagPageResponseWrapper wrapper = new TagPageResponseWrapper(getResponse());
dispatcher.include(getRequest(), wrapper);
JspWriter jspWriter = pageContext.getOut();
jspWriter.write(wrapper.getContent());
}
catch(Exception e)
{
e.printStackTrace();
}
return EVAL_PAGE;
}
OVER…
版权所有,请勿转载,谢谢

猜你喜欢

转载自blog.csdn.net/lv836735240/article/details/46894785
今日推荐