JSP 自定义标签 的 开发

阅读更多
为了简单说明,拿系统代码处理为例,根据标识获取名称

定义xxx.tld文件,放在WEB-INF下的tld中



	1.2
	1.1
	cpctld
	/fulg
	
		xiTongDaiMaMingCheng
		com.xxx.xxx.tag.XiTongDaiMaNameTag
		empty
		
			
		
		
			biaoshi
			true
			true
			
				
			
		
		
			mingCheng
			false
			true
			
				
			
		
		
			zhengXingDaiMa
			false
			true
			
				
			
		
		
			ziFuDaiMa
			false
			true
			
				
			
		
		
	




web.xml中配置所写的xxx.tld


   
	/fulg
	/WEB-INF/tld/xxx.tld
   





写标签类

package xxx.xxx.tag;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.WeakHashMap;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;



/**
 * @ClassName: XiTongDaiMaNameTag
 * @Description: TODO(系统代码标签,根据zhengXingDaiMa或ziFuDaiMa获取整形代码名称,两种情况标签中至少写一个,都写按ziFuDaiMa查询)
 * @Company neusoft
 * @author fulg
 * @date 2013-10-30
 */
public class XiTongDaiMaNameTag extends TagSupport{
	
	//父节点标识
	private int biaoshi;

	//父节点名称,非必填项,仅作显示说明使用,代码里显示当前系统代码的名称
	private String mingCheng;
	

	//整形代码
    private int zhengXingDaiMa;
	
    //字符代码
	private String ziFuDaiMa;
	
	//名称缓存
	//private WeakHashMap nameMap;//使用map缓存后,系统代码树的修改无法实时反映到map中
	

	
	@Override	
	public int doStartTag() throws JspException {
		
		String xiTongDaiMaMingCheng = "";
		
		//if(nameMap==null||nameMap.isEmpty()){
			
				
				ContextHolder.setCustomerType(SysConstants.CURRENT_SERVER);
		        
				//系统代码实例对象
			    IXiTongDaiMaService xiTongDaiMaService = (IXiTongDaiMaService)AppContextUtils.getBean("xiTongDaiMaService");
				
					
				//根据父类查询系统代码列表	
				List   list = null;
				
				
				//if(mingCheng!=null){//不以名称查询,因为名称可以改变
								
				//	list = xiTongDaiMaService.getXiTongDaiMaByFenLeiMingCheng(mingCheng);
								
				//}else{
				
					list = xiTongDaiMaService.getXiTongDaiMaByFuJieDianBiaoShi(biaoshi);
					
				//}//else end
				
			
				//nameMap = new WeakHashMap();
					
				for (XiTongDaiMa vo:list) {
					
						if(ziFuDaiMa!=null){
							
							//nameMap.put(vo.getZiFuDaiMa(), vo.getMingCheng());
							
							if(ziFuDaiMa.equals(vo.getZiFuDaiMa())){
								xiTongDaiMaMingCheng = vo.getMingCheng();
								break;
							}
							
						}else{
							
							//nameMap.put(vo.getZhengXingDaiMa(), vo.getMingCheng());
							
							if(zhengXingDaiMa==vo.getZhengXingDaiMa()){
								xiTongDaiMaMingCheng = vo.getMingCheng();
								break;
							}
							
						}
		
					}
			
		//}//if nameMap==null
		
		/*if(ziFuDaiMa!=null&&!ziFuDaiMa.equals("")){
			xiTongDaiMaMingCheng = (String)nameMap.get(ziFuDaiMa);
			
		}else{
			xiTongDaiMaMingCheng = (String)nameMap.get(zhengXingDaiMa);
			
		}*/
			
		
                //此方法里也可以封装jsp页面元素等操作
                //例如String str = ""
                //注:option需要for循环等操作
                //然后把会写到页面就可以了out.write(str);

		try {
			JspWriter out = pageContext.getOut();
			out.write(xiTongDaiMaMingCheng);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return SKIP_BODY;
	}
	
	
	
	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}
	
	
	
	
	public int getBiaoshi() {
		return biaoshi;
	}

	public void setBiaoshi(int biaoshi) {
		this.biaoshi = biaoshi;
	}

	public String getMingCheng() {
		return mingCheng;
	}

	public void setMingCheng(String mingCheng) {
		this.mingCheng = mingCheng;
	}

	public int getZhengXingDaiMa() {
		return zhengXingDaiMa;
	}

	public void setZhengXingDaiMa(int zhengXingDaiMa) {
		this.zhengXingDaiMa = zhengXingDaiMa;
	}

	public String getZiFuDaiMa() {
		return ziFuDaiMa;
	}

	public void setZiFuDaiMa(String ziFuDaiMa) {
		this.ziFuDaiMa = ziFuDaiMa;
	}

	/*public WeakHashMap getNameMap() {
		return nameMap;
	}

	public void setNameMap(WeakHashMap nameMap) {
		this.nameMap = nameMap;
	}*/
	
}




最后页面使用

     

你可能感兴趣的:(自定义标签,jsp)