tld标签的使用(自定义标签)
2012-10-08 09:45
453 查看
1.新建一个标签我们命名为name.tld,注意该文件请放在WEB-INF文件下(与web.xml同级),运行时会自动加载
2.我们现在开始实现标签的getName方法。新建一个包com.yuqiaotech.pms.webapp.tags,该包下有两个文件getName.java和PmsTag.java
getName.java
PmsTag.java注意PmsTag继承了struts自定义标签库TagSupport
4.书写一个jsp页面注意在querybooks.jsp添加的,该工程可以参看我的文章:eclipse中新建struts工程http://blog.csdn.net/b10090120/article/details/8045271
5.运行效果如下:
![](http://img.my.csdn.net/uploads/201210/08/1349660995_7856.JPG)
/*-------------- 2014-01-27 补 ------------ */
但是如果我想实现像:
标签实现:判断当前用户是否是超级用户,如果是超级用户,就执行标签体,如果不是则不执行
package com.njupt.webapp.tags;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 是否执行标签体,同时设置变量可使用范围 (默认:pageContext) 参考: jstl:core:if实现原理
* */
public abstract class AuthorityBaseTag extends TagSupport {
private boolean result; /* 是否执行 */
private String var;
private int scope; /* 结果保存范围 */
protected abstract boolean condition() throws JspTagException, JspException;
public AuthorityBaseTag() {
init();
}
public int doStartTag() throws JspException {
this.result = condition();
if (this.result) {
return 1;
}
return 0;
}
public void release() {
super.release();
init();
}
public void setVar(String var) {
this.var = var;
}
public String getVar() {
return this.var;
}
public void setScope(String scope) {
if (scope.equalsIgnoreCase("page"))
this.scope = 1;
else if (scope.equalsIgnoreCase("request"))
this.scope = 2;
else if (scope.equalsIgnoreCase("session"))
this.scope = 3;
else if (scope.equalsIgnoreCase("application"))
this.scope = 4;
}
public int getScope() {
return this.scope;
}
private void init() {
this.result = false;
this.scope = 1;
}
}
tld定义:
<tag>
<name>isSuperUser</name>
<tag-class>com.njupt.webapp.tags.IsSuperUserTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>userId</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>userType</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>is</name>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<?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>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>name</short-name> <uri>http://www.yuqiaotech.com/name</uri> <description>name标签库</description> <tag> <name>getName</name> <tag-class>com.yuqiaotech.pms.webapp.tags.getName</tag-class> <attribute> <name>userName</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>var</name> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>
2.我们现在开始实现标签的getName方法。新建一个包com.yuqiaotech.pms.webapp.tags,该包下有两个文件getName.java和PmsTag.java
getName.java
package com.yuqiaotech.pms.webapp.tags; import javax.servlet.jsp.JspException; public class getName extends PmsTag{ private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } private String scope; public int doStartTag() throws JspException { //将值直接进行赋值 String myName=userName+"------通过标签获取的数据"; setAttribute(var,this,pageContext,scope,myName); return super.doStartTag(); } }
PmsTag.java注意PmsTag继承了struts自定义标签库TagSupport
package com.yuqiaotech.pms.webapp.tags; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; public class PmsTag extends TagSupport{ protected String var; public static void setAttribute(String attrName,TagSupport tag,PageContext pageContext,String scope,Object obj){ if (scope != null) { if (scope.equals("page")) { pageContext.setAttribute(attrName, obj); } else if (scope.equals("request")) { pageContext.getRequest().setAttribute(attrName, obj); } else if (scope.equals("session")) { pageContext.getSession().setAttribute(attrName, obj); } else if (scope.equals("application")) { pageContext.getServletContext().setAttribute(attrName, obj); } else { throw new RuntimeException("Attribute 'scope' must be: page, request, session or application :"+scope); } }else{ pageContext.getRequest().setAttribute(attrName, obj); } } public String getVar() { return var; } public void setVar(String var) { this.var = var; } }
4.书写一个jsp页面注意在querybooks.jsp添加的,该工程可以参看我的文章:eclipse中新建struts工程http://blog.csdn.net/b10090120/article/details/8045271
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <%@ taglib uri="http://www.yuqiaotech.com/name" prefix="name" %> <html> <head> <title>查询图书信息</title> </head> <body> <name:getName userName="沈维海" var="myName"></name:getName> ${myName } <s:form action="query"> <s:textfield label="书名" name="name" /> <s:submit value="查询"/> </s:form> </body> </html>
5.运行效果如下:
/*-------------- 2014-01-27 补 ------------ */
但是如果我想实现像:
<c:if test=""> dosometing.... </c:if>
标签实现:判断当前用户是否是超级用户,如果是超级用户,就执行标签体,如果不是则不执行
package com.njupt.webapp.tags;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 是否执行标签体,同时设置变量可使用范围 (默认:pageContext) 参考: jstl:core:if实现原理
* */
public abstract class AuthorityBaseTag extends TagSupport {
private boolean result; /* 是否执行 */
private String var;
private int scope; /* 结果保存范围 */
protected abstract boolean condition() throws JspTagException, JspException;
public AuthorityBaseTag() {
init();
}
public int doStartTag() throws JspException {
this.result = condition();
if (this.result) {
return 1;
}
return 0;
}
public void release() {
super.release();
init();
}
public void setVar(String var) {
this.var = var;
}
public String getVar() {
return this.var;
}
public void setScope(String scope) {
if (scope.equalsIgnoreCase("page"))
this.scope = 1;
else if (scope.equalsIgnoreCase("request"))
this.scope = 2;
else if (scope.equalsIgnoreCase("session"))
this.scope = 3;
else if (scope.equalsIgnoreCase("application"))
this.scope = 4;
}
public int getScope() {
return this.scope;
}
private void init() {
this.result = false;
this.scope = 1;
}
}
package com.njupt.webapp.tags; import javax.servlet.jsp.JspException; import org.springframework.web.context.support.WebApplicationContextUtils; import com.njupt.dao.Constants; import com.njupt.dao.UniversalManager; import com.njupt.model.ClientUser; import com.njupt.model.CompanyUser; import com.njupt.utils.TagUtils; public class IsSuperUserTag extends AuthorityBaseTag{ protected String managerId = "manager"; protected Long userId; protected String userType; protected String is; protected boolean condition() throws JspException { Long _userId = (Long)TagUtils.evaluate("userId", userId+"", Long.class, this, pageContext); String _userType = (String)TagUtils.evaluate("userType", userType, String.class, this, pageContext); UniversalManager manager = (UniversalManager)getBean(managerId); boolean isSuper = false; if(Constants.COMPANY_USER_TYPE.equals(_userType)){ CompanyUser cu = (CompanyUser)manager.queryUniqueResult("from CompanyUser where id=?", new Object[]{_userId}); isSuper = cu.getIsSuperUser(); } else if(Constants.CLIENT_USER_TYPE.equals(_userType)){ ClientUser cu = (ClientUser)manager.queryUniqueResult("from ClientUser where id=?", new Object[]{_userId}); isSuper = cu.getIsSuperUser(); } if (getVar() != null) this.pageContext.setAttribute(getVar(), new String(isSuper?"超级用户身份一验证":"没有权限访问"), getScope()); if (is != null) this.pageContext.setAttribute(is, new Boolean(isSuper), getScope()); return isSuper; } public void release(){ super.release(); } private Object getBean(String beanName){ return WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext()).getBean(beanName); } public String getManagerId() { return managerId; } public void setManagerId(String managerId) { this.managerId = managerId; } public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public String getUserType() { return userType; } public void setUserType(String userType) { this.userType = userType; } public String getIs() { return is; } public void setIs(String is) { this.is = is; } }
tld定义:
<tag>
<name>isSuperUser</name>
<tag-class>com.njupt.webapp.tags.IsSuperUserTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>userId</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>userType</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>is</name>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!-- 如果该用户是公司超级用户则将此菜单显示出来 --> <gauge:isSuperUser var="info" is="isSuper" userId="${me.id }" userType="Company" > <div iconCls="icon-password" title="超级用户修改本公司用户密码...." id="updateCompanyUserPassword">修改员工密码</div> </gauge:isSuperUser>
相关文章推荐
- 如何自定义标签(.tld文件的使用) --Freemark
- 自定义tld标签,页面使用
- 使用tld文件注册自定义标签注意事项
- 使用自定义tld标签简化jsp的繁琐操作
- 使用自定义tld标签简化jsp的繁琐操作
- 帝国 标签模板 使用程序代码 自定义 时间显示方式
- 学会怎样使用Jsp 内置标签、jstl标签库及自定义标签
- 灵活使用XMultipleSeriesRenderer设置自定义的轴标签
- 自定义标签的使用
- 自定义标签的开发及使用自定义标签实现迭代foreach循环
- jsp 使用自定义标签时候错误
- dedecms在搜索结果页面中不能调用arclist标签和自定义宏标记的使用
- Play framework使用java代码自定义标签--FastTags
- Play framework使用java代码自定义标签--FastTags
- jsp:自定义标签实例(TLD)
- 自定义JSP中的Taglib标签之五自定义标签之SimpleTagSupport使用
- 自定义分页标签结合spring mvc、bootstrap、mybatis、mysql的使用
- Java - 使用标签库有什么好处?如何自定义JSP标签?
- php整理html 自定义允许使用的标签和属性属性
- 小程序之wx:for通过data自定义标签传值和setDa 3ff8 ta的使用