以下是一个java实现的jsp的标签事例,在写标签的过程中可以参考一下。
package com.nurbs.framework.tags;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.nurbs.framework.core.util.servlet.HttpRequestUtil;
import com.nurbs.framework.core.util.tools.StringUtil;
import com.nurbs.framework.core.vars.GlobalVars;
/**
* 所有标签的父表签-类似与html的html标签
*
* @author yjc
* @version 1.0 创建时间 Jul 17, 2014
*/
public class HtmlTag extends BodyTagSupport{
private static final long serialVersionUID = 1L;
// 构造函数
public HtmlTag() {
this.title = GlobalVars.APP_NAME;
this.nocache = "false";
this.keywords = GlobalVars.APP_KEYWORDS;
this.description = GlobalVars.APP_DESCRIPTION;
this.plugins = "all";
}
// 属性
private String title = null;// 网页的标题
private String nocache = null;// 是否每次浏览网页时,都清空浏览器缓存,默认false,不清
private String keywords = null;// 网页浏览关键字,以,间隔
private String description = null;// 网页描述
// 插件;默认为all加载所有系统已知插件;否则自己使用,对插件名称进行区分,
// 选择加载那些插件,使用时,注意插件依存关系;none标识任何插件均不加载
private String plugins = null;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNocache() {
return nocache;
}
public void setNocache(String nocache) {
this.nocache = nocache;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPlugins() {
return plugins;
}
public void setPlugins(String plugins) {
this.plugins = plugins;
}
/**
* 标签开始
*/
public int doStartTag() throws JspException {
try {
// 获取请求
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
// 获取输出字符串
StringBuffer strBF = new StringBuffer();
StringUtil.strBFAppendln(strBF,
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
StringUtil.strBFAppendln(strBF, "<html>");
StringUtil.strBFAppendln(strBF, " <head>");
// 页面连接的基准路径
StringUtil.strBFAppendln(strBF, " <base href=\""
+ HttpRequestUtil.getServerBasePath(request) + "\">");
// 标题
StringUtil.strBFAppendln(strBF, " <title>" + this.getTitle()
+ "</title>");
// 每次进入页面都清空浏览器缓存
if ("true".equalsIgnoreCase(this.getNocache())) {
StringUtil.strBFAppendln(strBF,
" <meta http-equiv=\"pragma\" content=\"no-cache\">");
StringUtil.strBFAppendln(strBF,
" <meta http-equiv=\"cache-control\" content=\"no-cache\">");
StringUtil.strBFAppendln(strBF,
" <meta http-equiv=\"expires\" content=\"0\">");
}
// 关键字
StringUtil.strBFAppendln(strBF, " <meta http-equiv=\"keywords\" content=\""
+ this.getKeywords() + "\">");
// 网页描述
StringUtil.strBFAppendln(strBF, " <meta http-equiv=\"description\" content=\""
+ this.getDescription() + "\">");
// 插件
StringUtil.strBFAppendln(strBF, this.getPluginsHtmlStr());
StringUtil.strBFAppendln(strBF, " </head>");
StringUtil.strBFAppendln(strBF, " <body>");
// 输出
JspWriter out = pageContext.getOut();// 输出对象
out.print(strBF.toString());
} catch (IOException e) {
throw new JspException("标签在将HTML输出到页面上时出现流的IO异常");
} catch (Exception e) {
throw new JspException("解析标签时出现异常:" + e.getMessage());
}
return EVAL_BODY_INCLUDE;
}
/**
* 获取插件html内容:插件的地址均以base-herf为基准延伸
*
* @author yjc
* @date 创建时间 Jul 17, 2014
* @since V1.0
*/
private String getPluginsHtmlStr() throws Exception {
String pluginList = "nbfbase,jquery,ueditor,easyui";// 插件列表:排列顺序为加载顺序
String loadPluginList = "";
// 如果全部加载
if ("all".equalsIgnoreCase(this.getPlugins())) {
loadPluginList = pluginList;
} else if ("none".equals(this.getPlugins())) {
loadPluginList = "";// 一个都不加载
} else {
// 按照列表加载
loadPluginList = StringUtil.get2StrIntersection(pluginList, this.getPlugins());
}
loadPluginList = loadPluginList.toLowerCase();// 转为小写
loadPluginList += ",";// 为了防止插件间存在重叠字符,加,区分
// 有些插件存在依存关系-此处要特殊出来依存关系
// 1.easyui要依存与jquery插件
if (loadPluginList.indexOf("easyui,") >= 0
&& loadPluginList.indexOf("jquery") < 0) {
loadPluginList += "jquery,";
}
// 字符串buffer
StringBuffer strBF = new StringBuffer();
// nbfbase插件:nbf框架基本插件
if (loadPluginList.indexOf("nbfbase,") >= 0) {
StringUtil.strBFAppendln(strBF,
" <link rel=\"stylesheet\" href=\"framework/nbf_default.css\"");
StringUtil.strBFAppendln(strBF,
" type=\"text/css\"></link>");
}
// jquery插件
if (loadPluginList.indexOf("jquery,") >= 0) {
StringUtil.strBFAppendln(strBF,
"<script type=\"text/javascript\" "
+"src=\"framework/jquery/jquery-1.7.2.min.js\"></script>");
}
// ueditor插件
if (loadPluginList.indexOf("ueditor,") >= 0) {
StringUtil.strBFAppendln(strBF,
" <script type=\"text/javascript\"");
StringUtil.strBFAppendln(strBF,
" src=\"framework/ueditor1.4.3/ueditor.config.js\"></script>");
StringUtil.strBFAppendln(strBF,
" <script type=\"text/javascript\"");
StringUtil.strBFAppendln(strBF,
" src=\"framework/ueditor1.4.3/ueditor.all.js\"></script>");
}
// easyui插件
if (loadPluginList.indexOf("easyui,") >= 0) {
StringUtil.strBFAppendln(strBF,
" <link rel=\"stylesheet\"");
StringUtil.strBFAppendln(strBF,
" href=\"framework/jquery-easyui-1.3.6/themes/default/easyui.css\"");
StringUtil.strBFAppendln(strBF,
" type=\"text/css\"></link>");
StringUtil.strBFAppendln(strBF,
" <link rel=\"stylesheet\"");
StringUtil.strBFAppendln(strBF,
" href=\"framework/jquery-easyui-1.3.6/themes/icon.css\" type=\"text/css\"></link>");
StringUtil.strBFAppendln(strBF,
" <script type=\"text/javascript\"");
StringUtil.strBFAppendln(strBF,
" src=\"framework/jquery-easyui-1.3.6/jquery.easyui.min.js\"></script>");
}
return strBF.toString();
}
/**
* 标签结束
*/
public int doEndTag() throws JspException {
try {
StringBuffer strBF = new StringBuffer();
StringUtil.strBFAppendln(strBF, " </body>");
StringUtil.strBFAppendln(strBF, "</html>");
JspWriter out = pageContext.getOut();
out.print(strBF.toString());
release();
} catch (IOException e) {
throw new JspException("标签在将HTML输出到页面上时出现流的IO异常");
} catch (Exception e) {
throw new JspException("解析标签时出现异常" + e.getMessage());
}
return EVAL_PAGE;
}
/**
* 资源释放,恢复默认值
*/
public void release() {
this.title = GlobalVars.APP_NAME;
this.nocache = "false";
this.keywords = GlobalVars.APP_KEYWORDS;
this.description = GlobalVars.APP_DESCRIPTION;
this.plugins = "all";
super.release();
}
}
<tag>
<description>
html标签,所有的其他标签均需要包含在此标签中,与html标签一致,
但是其包含了head,body中间内容只是在body中输出
</description>
<name>html</name>
<tagclass>com.nurbs.framework.tags.HtmlTag</tagclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>title</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>网页标题</description>
</attribute>
<attribute>
<name>nocache</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>刷新网页时是否清空浏览器缓存,默认为false,不清空</description>
</attribute>
<attribute>
<name>keywords</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>网页关键字</description>
</attribute>
<attribute>
<name>description</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>网页描述</description>
</attribute>
<attribute>
<name>plugins</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>插件集合,取值all,none,获取pluginlist</description>
</attribute>
</tag>
转载请注明: http://itsshq.com/article-235.html