tld标签的使用(自定义标签)

1.新建一个标签我们命名为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")) {
	                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>


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值