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.2つのファイルがある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ページを書く
気をつけて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.運行効果は以下の通りである:
tld标签的使用(自定义标签)_第1张图片
/*------------------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>