您的位置:首页 > 其它

自定义tld 标签

2014-05-08 15:44 155 查看
.新建一个标签我们命名为name.tld,注意该文件请放在WEB-INF文件下(与web.xml同级),运行时会自动加载

<?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"<
1334e
span>)) {  
                    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"%>  
<span style="color:#ff0000;"><%@ taglib uri="http://www.yuqiaotech.com/name" prefix="name" %></span>  
  
<html>  
    <head>  
        <title>查询图书信息</title>  
    </head>  
  
    <body>  
    <span style="color:#ff0000;"><name:getName userName="沈维海" var="myName"></name:getName></span>  
    ${myName }  
         <s:form action="query">  
            <s:textfield label="书名" name="name" />              
            <s:submit value="查询"/>  
        </s:form>  
    </body>  
</html><span style="color:#ff0000;">  
</span>  

5.运行效果如下:



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

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

[java]
view plaincopy





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;  
    }  
  
}  

[java]
view plaincopy





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定义:

[html]
view plaincopy





<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> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: