自定义tld 标签

.新建一个标签我们命名为name.tld, 注意该文件请放在WEB-INF文件下(与web.xml同级),运行时会自动加载
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
  3.                         "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">  
  4. <taglib>  
  5.  <tlib-version>1.0</tlib-version>  
  6.  <jsp-version>1.2</jsp-version>  
  7.  <short-name>name</short-name>  
  8.  <uri>http://www.yuqiaotech.com/name</uri>  
  9.  <description>name标签库</description>  
  10.     <tag>  
  11.         <name>getName</name>  
  12.         <tag-class>com.yuqiaotech.pms.webapp.tags.getName</tag-class>  
  13.         <attribute>  
  14.             <name>userName</name>  
  15.             <required>true</required>  
  16.             <rtexprvalue>true</rtexprvalue>  
  17.         </attribute>  
  18.         <attribute>  
  19.             <name>var</name>  
  20.             <rtexprvalue>false</rtexprvalue>  
  21.         </attribute>  
  22.     </tag>  
  23. </taglib>  

2.我们现在开始实现标签的getName方法。新建一个包com.yuqiaotech.pms.webapp.tags,该包下有两个文件getName.java和PmsTag.java

getName.java

  1. package com.yuqiaotech.pms.webapp.tags;  
  2.   
  3. import javax.servlet.jsp.JspException;  
  4.   
  5. public class getName extends PmsTag{  
  6.   
  7.     private String userName;  
  8.     public String getUserName() {  
  9.         return userName;  
  10.     }  
  11.   
  12.     public void setUserName(String userName) {  
  13.         this.userName = userName;  
  14.     }  
  15.   
  16.     public String getScope() {  
  17.         return scope;  
  18.     }  
  19.   
  20.     public void setScope(String scope) {  
  21.         this.scope = scope;  
  22.     }  
  23.   
  24.     private String scope;  
  25.       
  26.     public int doStartTag() throws JspException {  
  27.           
  28.         //将值直接进行赋值  
  29.         String myName=userName+"------通过标签获取的数据";  
  30.         setAttribute(var,this,pageContext,scope,myName);  
  31.         return super.doStartTag();  
  32.     }  
  33.   
  34. }  

PmsTag.java 注意PmsTag继承了struts自定义标签库TagSupport

  1. package com.yuqiaotech.pms.webapp.tags;  
  2.   
  3. import javax.servlet.jsp.PageContext;  
  4. import javax.servlet.jsp.tagext.TagSupport;  
  5. public class PmsTag extends TagSupport{  
  6.   
  7.         protected String var;  
  8.           
  9.         public static void setAttribute(String attrName,TagSupport tag,PageContext pageContext,String scope,Object obj){  
  10.             if (scope != null) {  
  11.                 if (scope.equals("page")) {  
  12.                     pageContext.setAttribute(attrName, obj);  
  13.                 } else if (scope.equals("request")) {  
  14.                     pageContext.getRequest().setAttribute(attrName, obj);  
  15.                 } else if (scope.equals("session")) {  
  16.                     pageContext.getSession().setAttribute(attrName, obj);  
  17.                 } else if (scope.equals("application")) {  
  18.                     pageContext.getServletContext().setAttribute(attrName, obj);  
  19.                 } else {  
  20.                     throw new RuntimeException("Attribute 'scope' must be: page, request, session or application :"+scope);  
  21.                 }  
  22.             }else{  
  23.                 pageContext.getRequest().setAttribute(attrName, obj);  
  24.             }  
  25.         }  
  26.         public String getVar() {  
  27.             return var;  
  28.         }  
  29.         public void setVar(String var) {  
  30.             this.var = var;  
  31.         }  
  32. }  

4.书写一个jsp页面 注意在querybooks.jsp添加的,该工程 可以参看我的文章:eclipse中新建struts工程http://blog.csdn.net/b10090120/article/details/8045271
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags"%>  
  3. <span style="color:#ff0000;"><%@ taglib uri="http://www.yuqiaotech.com/name" prefix="name" %></span>  
  4.   
  5. <html>  
  6.     <head>  
  7.         <title>查询图书信息</title>  
  8.     </head>  
  9.   
  10.     <body>  
  11.     <span style="color:#ff0000;"><name:getName userName="沈维海" var="myName"></name:getName></span>  
  12.     ${myName }  
  13.          <s:form action="query">  
  14.             <s:textfield label="书名" name="name" />              
  15.             <s:submit value="查询"/>  
  16.         </s:form>  
  17.     </body>  
  18. </html><span style="color:#ff0000;">  
  19. </span>  

5.运行效果如下:

  1. <c:if test=""> dosometing.... </c:if>  

标签实现:判断当前用户是否是超级用户,如果是超级用户,就执行标签体,如果不是则不执行

  1. package com.njupt.webapp.tags;  
  2.   
  3. import javax.servlet.jsp.JspException;  
  4. import javax.servlet.jsp.JspTagException;  
  5. import javax.servlet.jsp.tagext.TagSupport;  
  6.   
  7. /** 
  8.  * 是否执行标签体,同时设置变量可使用范围 (默认:pageContext) 参考: jstl:core:if实现原理 
  9.  * */  
  10. public abstract class AuthorityBaseTag extends TagSupport {  
  11.       
  12.     private boolean result; /* 是否执行 */  
  13.   
  14.     private String var;  
  15.     private int scope; /* 结果保存范围 */  
  16.   
  17.     protected abstract boolean condition() throws JspTagException, JspException;  
  18.   
  19.     public AuthorityBaseTag() {  
  20.         init();  
  21.     }  
  22.   
  23.     public int doStartTag() throws JspException {  
  24.         this.result = condition();  
  25.         if (this.result) {  
  26.             return 1;  
  27.         }  
  28.         return 0;  
  29.     }  
  30.   
  31.     public void release() {  
  32.         super.release();  
  33.         init();  
  34.     }  
  35.   
  36.     public void setVar(String var) {  
  37.         this.var = var;  
  38.     }  
  39.   
  40.     public String getVar() {  
  41.         return this.var;  
  42.     }  
  43.   
  44.     public void setScope(String scope) {  
  45.         if (scope.equalsIgnoreCase("page"))  
  46.             this.scope = 1;  
  47.         else if (scope.equalsIgnoreCase("request"))  
  48.             this.scope = 2;  
  49.         else if (scope.equalsIgnoreCase("session"))  
  50.             this.scope = 3;  
  51.         else if (scope.equalsIgnoreCase("application"))  
  52.             this.scope = 4;  
  53.     }  
  54.   
  55.     public int getScope() {  
  56.         return this.scope;  
  57.     }  
  58.   
  59.     private void init() {  
  60.         this.result = false;  
  61.         this.scope = 1;  
  62.     }  
  63.   
  64. }  


  1. package com.njupt.webapp.tags;  
  2.   
  3. import javax.servlet.jsp.JspException;  
  4.   
  5. import org.springframework.web.context.support.WebApplicationContextUtils;  
  6.   
  7. import com.njupt.dao.Constants;  
  8. import com.njupt.dao.UniversalManager;  
  9. import com.njupt.model.ClientUser;  
  10. import com.njupt.model.CompanyUser;  
  11. import com.njupt.utils.TagUtils;  
  12.   
  13. public class IsSuperUserTag  extends AuthorityBaseTag{  
  14.   
  15.       protected String managerId = "manager";    
  16.           protected Long userId;  
  17.       protected String userType;  
  18.         
  19.       protected String is;  
  20.         
  21.       protected boolean condition() throws JspException    
  22.       {  
  23.           Long _userId = (Long)TagUtils.evaluate("userId", userId+"", Long.classthis, pageContext);  
  24.           String  _userType = (String)TagUtils.evaluate("userType", userType, String.classthis, pageContext);  
  25.           UniversalManager  manager =  (UniversalManager)getBean(managerId);  
  26.           boolean isSuper = false;  
  27.             
  28.           if(Constants.COMPANY_USER_TYPE.equals(_userType)){  
  29.                 CompanyUser cu = (CompanyUser)manager.queryUniqueResult("from CompanyUser where id=?"new Object[]{_userId});  
  30.                 isSuper = cu.getIsSuperUser();  
  31.             }  
  32.             else if(Constants.CLIENT_USER_TYPE.equals(_userType)){  
  33.                 ClientUser cu = (ClientUser)manager.queryUniqueResult("from ClientUser where id=?"new Object[]{_userId});  
  34.                 isSuper = cu.getIsSuperUser();  
  35.             }  
  36.           if (getVar() != null)  
  37.                 this.pageContext.setAttribute(getVar(), new String(isSuper?"超级用户身份一验证":"没有权限访问"), getScope());  
  38.           if (is != null)  
  39.                 this.pageContext.setAttribute(is, new Boolean(isSuper), getScope());  
  40.           return isSuper;  
  41.       }  
  42.   
  43.   
  44.     public void release(){ super.release(); }  
  45.       
  46.     private Object getBean(String beanName){  
  47.         return WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext()).getBean(beanName);  
  48.     }  
  49.         
  50.     public String getManagerId() {  
  51.         return managerId;  
  52.     }  
  53.   
  54.     public void setManagerId(String managerId) {  
  55.         this.managerId = managerId;  
  56.     }  
  57.   
  58.     public Long getUserId() {  
  59.         return userId;  
  60.     }  
  61.   
  62.     public void setUserId(Long userId) {  
  63.         this.userId = userId;  
  64.     }  
  65.   
  66.     public String getUserType() {  
  67.         return userType;  
  68.     }  
  69.   
  70.     public void setUserType(String userType) {  
  71.         this.userType = userType;  
  72.     }  
  73.   
  74.   
  75.     public String getIs() {  
  76.         return is;  
  77.     }  
  78.   
  79.   
  80.     public void setIs(String is) {  
  81.         this.is = is;  
  82.     }  
  83.   
  84.     }  

tld定义:
  1. <tag>  
  2.         <name>isSuperUser</name>  
  3.         <tag-class>com.njupt.webapp.tags.IsSuperUserTag</tag-class>  
  4.         <body-content>JSP</body-content>  
  5.         <attribute>  
  6.             <name>userId</name>  
  7.             <required>true</required>  
  8.             <rtexprvalue>true</rtexprvalue>  
  9.         </attribute>  
  10.         <attribute>  
  11.             <name>userType</name>  
  12.             <required>true</required>  
  13.             <rtexprvalue>true</rtexprvalue>  
  14.         </attribute>  
  15.         <attribute>  
  16.             <name>var</name>  
  17.             <rtexprvalue>false</rtexprvalue>  
  18.         </attribute>  
  19.         <attribute>  
  20.             <name>is</name>  
  21.             <rtexprvalue>false</rtexprvalue>  
  22.         </attribute>  
  23.         <attribute>  
  24.             <name>scope</name>  
  25.             <rtexprvalue>true</rtexprvalue>  
  26.         </attribute>  
  27.     </tag> 
  1. <!-- 如果该用户是公司超级用户则将此菜单显示出来    -->  
  2. <gauge:isSuperUser  var="info" is="isSuper" userId="${me.id }" userType="Company" >  
  3.      <div iconCls="icon-password" title="超级用户修改本公司用户密码...." id="updateCompanyUserPassword">修改员工密码</div>  
  4. </gauge:isSuperUser> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值