Java jsp MasterPages实现母版页,实现统一风格的页面布局

2019-04-14 19:54发布

http://outofmemory.cn/code-snippet/964/Java-jsp-MasterPages-achieve-muban-page-achieve-tongyi-fengge-page-layout

Java jsp MasterPages实现母版页,实现统一风格的页面布局

更多0 jsp 模板 java利用Tag实现类似.Net的母版页功能。 MasterPageTag.java /** * */ package org.lhp.core.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; /** * TODO * * @Title : MasterPageTag.java * @author liheping 2012-5-14 下午03:59:03 * @version : V1.0 2012-5-14 * @Copyright : Copyright ( c ) dev.liheping.org ,2012 */ public class MasterPageTag extends BodyTagSupport { private static final long serialVersionUID = 1L; @Override public int doAfterBody() throws JspException { return SKIP_BODY; } @Override public int doStartTag() throws JspException { // 执行子标签 return EVAL_BODY_BUFFERED; } @Override public int doEndTag() throws JspException { JspWriter out = pageContext.getOut(); if (bodyContent != null) { if (out instanceof BodyContent) { out = ((BodyContent) out).getEnclosingWriter(); } } String content = this.bodyContent.getString(); try { this.bodyContent.clear(); out.write(content); } catch (IOException e) { e.printStackTrace(); } return SKIP_PAGE; // 不执行标签之后的内容 } } ContentPlaceHolderTag.java /** * */ package org.lhp.core.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; /** * TODO * @Title : ContentPlaceHolderTag.java * @author liheping 2012-5-14 下午03:33:01 * @version : V1.0 2012-5-14 * @Copyright : Copyright ( c ) dev.liheping.org ,2012 */ public class ContentPlaceHolderTag extends TagSupport { private static final long serialVersionUID = 1L; @Override public int doEndTag() throws JspException { JspWriter out = pageContext.getOut(); Object obj = this.pageContext.getRequest().getAttribute(this.getId()); try { if(obj!=null) out.write(obj.toString()); } catch (IOException e) { e.printStackTrace(); } return EVAL_PAGE; } } ContentPageTag.java /** * */ package org.lhp.core.tag; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyTagSupport; import org.lhp.core.response.CustomResponse; /** * TODO * * @Title : ContentPageTag.java * @author liheping 2012-5-14 下午04:00:47 * @version : V1.0 2012-5-14 * @Copyright : Copyright ( c ) dev.liheping.org ,2012 */ public class ContentPageTag extends BodyTagSupport { private final String masterFolderPath = "/WEB-INF/master/"; private final String masterPageSuffix = ".jsp"; private static final long serialVersionUID = 1L; @Override public void doInitBody() throws JspException { try { this.pageContext.getRequest().setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } super.doInitBody(); } @Override public int doAfterBody() throws JspException { return SKIP_BODY; } @Override public int doStartTag() throws JspException { //执行子标签 return EVAL_BODY_BUFFERED; } @Override public int doEndTag() throws JspException { JspWriter out = pageContext.getOut(); CustomResponse bufferedResponse = new CustomResponse((HttpServletResponse) this.pageContext.getResponse()); try { System.out.println("master url :"+this.getMasterPageUrl()); //渲染页面 this.