[size=large][color=red][b]1.xxx-tags.tld(关联具体的tag类)[/b][/color][/size]
[size=medium][color=blue][b]放在WEB-INF目录下[/b][/color][/size]
[size=large][color=red][b]2.xxx.tag.ButtonTag[/b][/color][/size]
[size=large][color=red][b]BaseTag [/b][/color][/size]
[color=blue][b]BaseTag比较重要的方法是doEndTag进行用户是否有权限显示对应的tag[/b][/color]
[size=large][color=red][b]3.页面引用[/b][/color][/size]
1.[size=medium][color=red][b]BodyTagSupport主要是标签处理类需要与标签体交互时使用[/b][/color][/size]
[size=medium][color=red][b]2.BodyTagSupport三个重要方法[/b][/color][/size]
doStartTag(),doEndTag(),doAfterBody(),
[color=blue]doStartTag()[/color]方法是[b]遇到标签开始时会呼叫的方法[/b],其合法的返回值是EVAL_BODY_INCLUDE与SKIP_BODY,前者表示将显示标签间的文字,后者表示不显示标签间的文字;
[color=blue]doEndTag()[/color][b]方法是在遇到标签结束时呼叫的方法[/b],其合法的返回值是EVAL_PAGE与 SKIP_PAGE,前者表示处理完标签后继续执行以下的JSP网页,后者是表示不处理接下来的JSP网页
[color=blue]doAfterBody()[/color]这个方法是在[b]显示完标签间文字之后呼叫的[/b],其返回值有EVAL_BODY_AGAIN与SKIP_BODY,前者会再显示一次标签间的文字,后者则继续执行标签处理的下一步。
[size=medium][color=blue][b]放在WEB-INF目录下[/b][/color][/size]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>2.2.3</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>tms</short-name>
<uri>/xxxx-tags</uri>
<display-name>"Struts Tags"</display-name>
<description><![CDATA["To make it easier to access dynamic data;
the Apache Struts framework includes a library of custom tags.
The tags interact with the framework's validation and internationalization features;
to ensure that input is correct and output is localized.
The Struts Tags can be used with JSP FreeMarker or Velocity."]]></description>
<tag>
<name>button</name>
<tag-class>xxx.tag.ButtonTag</tag-class>
<body-content>JSP</body-content>
<description><![CDATA[Render a HTML href element that when clicked can optionally call a URL via remote XMLHttpRequest and updates its targets]]></description>
<attribute>
<name>code</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
</attribute>
<attribute>
<name>xxId</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
</attribute>
<attribute>
<name>xxOnclick</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
</attribute>
<attribute>
<name>xxType</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
</attribute>
<attribute>
<name>xxClass</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
</attribute>
<attribute>
<name>xxValue</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
</attribute>
</tag>
</taglib>
[size=large][color=red][b]2.xxx.tag.ButtonTag[/b][/color][/size]
public class ButtonTag extends BaseTag {
private static final long serialVersionUID = 1L;
private String xxValue;
public String getXxValue() {
return xxValue;
}
public void setXxValue(String xxValue) {
this.xxValue = xxValue;
}
public String getHtml(){
StringBuffer sb=new StringBuffer();
String body=this.bodyContent.getString();
sb.append("<button ");
if(SysUtils.isNotEmpty(xxValue)){
sb.append(" value=").append(xxValue);
}
sb.append(">");
sb.append(body);
sb.append("</button>");
return sb.toString();
}
}
[size=large][color=red][b]BaseTag [/b][/color][/size]
[color=blue][b]BaseTag比较重要的方法是doEndTag进行用户是否有权限显示对应的tag[/b][/color]
public abstract class BaseTag extends BodyTagSupport {
private String workCode;
//其他标签类实现该方法
protected abstract String getHtml();
public String getWorkCode() {
return workCode;
}
public void setWorkCode(String workCode) {
this.workCode = workCode;
}
private static final long serialVersionUID = 1L;
//控制的关键
@Override
public int doEndTag() throws JspException {
String autcode=getAutcodeByWorkCode();
JSONObject user = (JSONObject)ServletActionContext.getRequest().getSession().getAttribute("USER_INFO");
String[] auth = (String[])ServletActionContext.getRequest().getSession().getAttribute("USER_AUTH");
if(user == null){
return EVAL_PAGE;
}
if(autcode == null){
try {
pageContext.getOut().write(getHtml());
} catch (IOException e) {
e.printStackTrace();
}
}else{
if(auth != null){
if(SysUtils.hasAuth(auth,workCode)){
try {
// 这句话是显示的关键
pageContext.getOut().write(getHtml());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return EVAL_PAGE;
}
}
[size=large][color=red][b]3.页面引用[/b][/color][/size]
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/xxxx-tags" prefix="cbm"%> //对应tag文件中的uri内容
<cbm:button xxId="xx" code="权限码" xxType="button" xxClass="btn ">xx</cbm:button>
//权限的控制主要是与code进行交互
1.[size=medium][color=red][b]BodyTagSupport主要是标签处理类需要与标签体交互时使用[/b][/color][/size]
[size=medium][color=red][b]2.BodyTagSupport三个重要方法[/b][/color][/size]
doStartTag(),doEndTag(),doAfterBody(),
[color=blue]doStartTag()[/color]方法是[b]遇到标签开始时会呼叫的方法[/b],其合法的返回值是EVAL_BODY_INCLUDE与SKIP_BODY,前者表示将显示标签间的文字,后者表示不显示标签间的文字;
[color=blue]doEndTag()[/color][b]方法是在遇到标签结束时呼叫的方法[/b],其合法的返回值是EVAL_PAGE与 SKIP_PAGE,前者表示处理完标签后继续执行以下的JSP网页,后者是表示不处理接下来的JSP网页
[color=blue]doAfterBody()[/color]这个方法是在[b]显示完标签间文字之后呼叫的[/b],其返回值有EVAL_BODY_AGAIN与SKIP_BODY,前者会再显示一次标签间的文字,后者则继续执行标签处理的下一步。