欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java--自定义标签--(价格策略中应用)

程序员文章站 2022-03-04 12:05:57
...

又用到java自定义标签,我比较不喜欢用它,没上心看,但是还得用啊!

1.先上我的代码,使用java自定义标签

1.1PSCheckboxTag.tag

<%@ tag pageEncoding="UTF-8" import="java.util.*,com.hanpeng.base.util.SelectManager,com.hanpeng.base.util.SelectManagerRequest" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="id"  required="true" rtexprvalue="true"%>
<%@ attribute name="name"  required="true" rtexprvalue="true"%>
<%@ attribute name="mainid"  required="true" rtexprvalue="true"%>
<%@ attribute name="required" required="false" rtexprvalue="true" type="java.lang.Boolean"%>

<%@ attribute name="isSelect"   required="false" rtexprvalue="true" type="java.lang.String[]"%> <!--  -->
<%@ attribute name="isShowHead"   required="false" rtexprvalue="true" type="java.lang.Boolean"%> <!-- 是否显示请选择 -->
<%@ attribute name="headinfo"   required="false" rtexprvalue="true" %> <!-- 自定义下拉框默认显示 -->
<%@ attribute name="sendMethod"   required="false" rtexprvalue="true" %> <!-- 调用参数 -->
<%@ attribute name="disabledstate"   required="false" rtexprvalue="true" %> <!-- 可传任意值  如:disabled=true -->
<%@ attribute name="widlength"   required="false" rtexprvalue="true" %> <!-- 设置宽度 -->
<%@ attribute name="diyClass"   required="false" rtexprvalue="true" %> <!-- 自定义样式 -->

<%@ attribute name="sendShowTitle"   required="false" rtexprvalue="true" %> <!-- 传入标题名称 -->
<%@ attribute name="sendOnChangeJs"   required="false" rtexprvalue="true" %> <!-- 指定方法JS -->

<%
	java.util.List list = null;
	if(sendMethod != null){
		if("zaiXinZhiFuXinXi".equals(sendMethod)) list = SelectManager.zaiXinZhiFuXinXi(); /* 在线支付渠道 */
		if("cunKuanYinHang".equals(sendMethod)) list = SelectManagerRequest.getCunKuanYinHang(request); /* 在线支付渠道 */
		if("getThreeMonthBefore".equals(sendMethod)) list = SelectManager.getThreeMonthBefore(); /* 本月前三月 */
		if("getMoneyType".equals(sendMethod)) list = SelectManager.getMoneyType(); /* 面值类型 */
	}
%>
<input type="checkbox" id="<%=mainid %>" name="<%=mainid %>" class="<%=diyClass %>" onclick="<%=sendOnChangeJs %>"/>&nbsp;&nbsp;<%=sendShowTitle%>&nbsp;&nbsp;
<%	if(list != null){
		for (int i=0;i<list.size();i++ ){
			java.util.HashMap ot = (java.util.HashMap)list.get(i);
			String oti = ot.get("val").toString();
			if(isSelect != null){
				%> <input type="checkbox" id="<%=id %>" name="<%=name %>" value="<%=ot.get("val")%>" <%
				for(int j=0;j<isSelect.length;j++){
					String tid = isSelect[j];
					if(tid.equals(oti)){
						%> checked="checked" 
						<%
					}
				}
				%> onClick="doRemovethis()"/>&nbsp;<%=ot.get("key") %>&nbsp;&nbsp; <%
			}else{
				%> <input type="checkbox" id="<%=id %>" name="<%=name %>" value="<%=ot.get("val")%>" onClick="doRemovethis()"/>&nbsp;<%=ot.get("key") %>&nbsp;&nbsp; <%
			}
		}
	}
%>

<script>
/* 全选/反选  */
function doSelectAll(val){
	var checkThis = document.getElementById('<%=mainid %>').checked;
	var checkList = document.getElementsByName('<%=id %>');

	if(checkThis == false){
		for(var i=0;i<checkList.length;i++){
			checkList[i].checked = false;
		}
	}else if(checkThis == true){
		for(var i=0;i<checkList.length;i++){
			checkList[i].checked = true;
		}
	}
}

/* 去掉全选 */
function doRemovethis(){
	document.getElementById('<%=mainid %>').checked = false;
}
</script>

 

1.2 showPriceStrategy.jsp中引用

<%@ taglib prefix="spes" tagdir="/WEB-INF/tags" %>

		<tr>
			<td align="right">
				面值:
			</td>
			<td>
			   <div id="divMoney">
					<spes:PSCheckboxTag 
						id="money" name="money"
						mainid="moneyAll"
						diyClass="sel" isShowHead="false" 
						sendMethod="getMoneyType"
						sendShowTitle="全部"
						sendOnChangeJs="doSelectAll(this.value)"
						isSelect="<%=request.getParameterValues("money") %>" 
					/>
			  </div>
			</td>
			<td><span id="textMoney">&nbsp;</span></td>
		</tr>

 

 1.3 SelectManager.java工具类中代码

//------------------------------------------------------------------------------
//文   件  名:                                            版           本:
//描          述:
//版权所有:杭州瀚鹏科技有限公司 
//------------------------------------------------------------------------------
//创  建   者:sgdjava          创建日期:2011-6-15
//修  改   者:                                             修改日期:
//修改说明:
//------------------------------------------------------------------------------
package com.hanpeng.base.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hanpeng.base.util.memoryCtrl.DictionaryCtrl;

 /**
 * @author sgdjava
 * @date 2011-6-15 上午10:06:44
 *
 */
@SuppressWarnings("unchecked")
public class SelectManager {
	
	/**月份中文名*/
	private final static String[] MONTHC = {
		"一月","二月","三月",
		"四月","五月","六月",
		"七月","八月","九月",
		"十月","十一月","十二月",
	};
	
	/**
	 * 在线支付信息 	k=状态名称,v=0/2; t=t_dictionary
	 */
	public static List zaiXinZhiFuXinXi(){
		String[] sval = "65,66".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("65")
				+","+DictionaryCtrl.getServiceName("66")).split(",");// key
		return allList(sval,skey);
	}
	
	
	
	/**
	 * 银行对帐,匹配状态 	k=状态名称,v=0/2; t=t_dictionary
	 */
	public static List yinHangPiPeiZT(){
		String[] sval = "0,2".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("0")
				+","+DictionaryCtrl.getServiceName("2")).split(",");// key
		return allList(sval,skey);
	}
	
	
	
	/**
	 * 处理状态,未处理,处理成功
	 */
	public static List chulizhuangtai(){
		String[] sval = "0,2".split(",");// value
		String[] skey = ("处理成功,未处理").split(",");// key
		return allList(sval,skey);
	}
	
	
	
	/**
	 * 自动续期	k=状态名称,v=10/11;t=t_dictionary
	 */
	public static List ziDongXvQi(){
		String[] sval = "10,11".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("10")
				+","+DictionaryCtrl.getServiceName("11")).split(",");// key
		return allList(sval,skey);
	}
	
	
	/**
	 * 套餐记录类型	k=类型名称,v=155~159;t=t_dictionary
	 */
	public static List taoCanJiLuLeiXing(){
		String[] sval = "155,156,157,158,159".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("155")
				+","+DictionaryCtrl.getServiceName("156")
				+","+DictionaryCtrl.getServiceName("157")
				+","+DictionaryCtrl.getServiceName("158")
				+","+DictionaryCtrl.getServiceName("159")).split(",");// key
		return allList(sval,skey);
	}
	
	
	
	/**
	 * 读取状态	k=状态名称,v=16/17;t=t_dictionary
	 */
	public static List duQvZhuangTai(){
		String[] sval = "16,17".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("16")
				+","+DictionaryCtrl.getServiceName("17")).split(",");// key
		return allList(sval,skey);
	}
	
	/**
	 * 公告类型	k=类型名称,v=20~24;t=t_dictionary
	 */
	public static List gongGaoLeiXing(){
		String[] sval = "20,21,22,23,24".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("20")
				+","+DictionaryCtrl.getServiceName("21")
				+","+DictionaryCtrl.getServiceName("22")
				+","+DictionaryCtrl.getServiceName("23")
				+","+DictionaryCtrl.getServiceName("24")).split(",");// key
		return allList(sval,skey);
	}
	
	
	/**
	 * 公告展示	k=展示类型,v=26~28;t=t_dictionary
	 */
	public static List gongGaoZhanShi(){
		String[] sval = "26,27,28".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("26")
				+","+DictionaryCtrl.getServiceName("27")
				+","+DictionaryCtrl.getServiceName("28")).split(",");// key
		return allList(sval,skey);
	}
	
	
	
	
	/**
	 * 记录类型	k=类型名称,v=9/30/31/34/35/33/82/97/98/99
	 */
	public static List jiLuLeiXing(){
		String[] sval = "9,30,31,32,33,82,97,98,99,154".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("9")
				+";"+DictionaryCtrl.getServiceName("30")
				+";"+DictionaryCtrl.getServiceName("31")
				+";"+DictionaryCtrl.getServiceName("32")
				+";"+DictionaryCtrl.getServiceName("33")
				+";"+DictionaryCtrl.getServiceName("82")
				+";"+DictionaryCtrl.getServiceName("97")
				+";"+DictionaryCtrl.getServiceName("98")
				+";"+DictionaryCtrl.getServiceName("99")
				+";"+DictionaryCtrl.getServiceName("154")).split(";");// key
		return allList(sval,skey);
	}
	
	
	/**
	 * 运营商	k=运营商,v=40/41/42;t=t_dictionary
	 */
	public static List yunYingShang(){
		String[] sval = "40,41,42".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("40")
				+","+DictionaryCtrl.getServiceName("41")
				+","+DictionaryCtrl.getServiceName("42")).split(",");// key
		return allList(sval,skey);
	}
	
	
	/**
	 * 号码类型	k=类型名称,v=45/56/57;t=t_dictionary
	 */
	public static List haoMaLeiXing(){
		String[] sval = "45,46".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("45")
				+","+DictionaryCtrl.getServiceName("46")).split(",");// key
		return allList(sval,skey);
	}
	
	
	/**
	 * 冲正状态	k=状态名称,v=83/84/3/-1;t=t_dictionary
	 */
	public static List chongZhengZhangTai(){
		String[] sval = "83,84,3,-1".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("83")
				+","+DictionaryCtrl.getServiceName("84")
				+","+DictionaryCtrl.getServiceName("3")
				+","+DictionaryCtrl.getServiceName("-1")).split(",");// key
		return allList(sval,skey);
	}
	
	
	/**
	 * 单边状态	k=状态名称,v=95/96/111/-111;t=t_dictionary
	 */
	public static List danBianZhuangTai(){
		String[] sval = "95,111,-111".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("95")
				+","+DictionaryCtrl.getServiceName("111")
				+","+DictionaryCtrl.getServiceName("-111")).split(",");// key
		return allList(sval,skey);
	}
	
	
	
	/**
	 * 业务状态(后)	k=状态名称,v=0/1/2/-1/-2/-7;t=t_dictionary
	 */
	public static List yeWuZhuangTai(){
		String[] sval = "0,1,2,-1,-2,-7".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("0")
				+","+DictionaryCtrl.getServiceName("1")
				+","+DictionaryCtrl.getServiceName("2")
				+","+DictionaryCtrl.getServiceName("-1")
				+","+DictionaryCtrl.getServiceName("-2")
				+","+DictionaryCtrl.getServiceName("-7")).split(",");// key
		return allList(sval,skey);
	}
	
	
	
	/**
	 * 短信类型	k=类型名称,v=165~167,t=t_dictionary
	 */
	public static List duanXinLeiXing(){
		String[] sval = "165,166,167".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("165")
				+","+DictionaryCtrl.getServiceName("166")
				+","+DictionaryCtrl.getServiceName("167")).split(",");// key
		return allList(sval,skey);
	}
	
	
	/**
	 * 开关状态	k=字典表,v=10/11;t=t_dictionary
	 */
	public static List kaiGuanZhuangTai(){
		String[] sval = "10,11".split(",");// value
		String[] skey = (DictionaryCtrl.getServiceName("10")
				+","+DictionaryCtrl.getServiceName("11")).split(",");// key
		return allList(sval,skey);
	}
	
	/**
	 * 获取本月之前三月
	 * @return
	 * @author  Invalid
	 * @date 2011-6-24 下午05:13:40
	 */
	public static List getThreeMonthBefore(){
		Calendar date = Calendar.getInstance();
		int[] months = new int[3];
		int[] years  = new int[3];
		
		for(int i=0; i < months.length;i++){
			date.add(Calendar.MONTH, -1);
			months[i] = date.get(Calendar.MONTH)+1;
			years[i]  = date.get(Calendar.YEAR);
		}
		
		String[] sval = {
							years[0]+"-"+(months[0] < 10 ? "0"+months[0] : months[0]),
							years[1]+"-"+(months[1] < 10 ? "0"+months[1] : months[1]),
							years[2]+"-"+(months[2] < 10 ? "0"+months[2] : months[2])
						};// value
		String[] skey = {MONTHC[months[0]-1],MONTHC[months[1]-1],MONTHC[months[2]-1]};// key
		return allList(sval,skey);
	}
	
	/**
	 * 面值类型	k=面值数值1~1000,v=1~1000
	 */
	public static List getMoneyType(){
		String[] moneyVal = "1,3,5,10,20,30,50,100,200,300,500,1000".split(",");// value
		String[] moneyKey = "1,3,5,10,20,30,50,100,200,300,500,1000".split(",");//key
		
		return allList(moneyVal,moneyKey);
	}
	
	/**
	 * 公用方法,返回下拉列表框结果
	 * @param sval
	 * @param skey
	 * @return
	 * @author sgdjava
	 * @date 2011-6-15 上午10:26:12
	 */
	public static List allList(String[] sval,String[] skey){
		List result = new ArrayList();
		for (int i = 0; i < sval.length; i++) {
			Map map = new HashMap();
			map.put("val", sval[i]);
			map.put("key", skey[i]);
			result.add(map);
		}
		return result;
	}
}

 

--------------------------------------------------------------------------------------------------------------------------------------------------

 2.详细使用方法

 

--------------------------------------------------------------------------------------------------------------------------------------------------

3.自定义标签两个属性,需要注意

<required>yes</required><!-- 表示该属性,一定要使用 -->
<rtexprvalue>true</rtexprvalue><!-- 属性是否能用表达式 -->   即EL表达式是否可用

 

当在<attribute>标签里指定<rtexprvalue>true</rtexprvalue>时, 表示该自定义标签的某属性的值可以直接指定或者通过动态计算指定,  example as follow:

<sql:query var="result" >
    select * from mytable order by nameid
</sql:query>
<%request.setAttribute("nameid", "2"); %>
<myTag:cupSize cupSize="1" cupSizes="${result}"></myTag:cupSize>
<myTag:cupSize cupSize="${nameid}" cupSizes="${result}"></myTag:cupSize>


当在<attribute>标签里指定<rtexprvalue>false</rtexprvalue>时, 表示该自定义标签的某属性的值只能直接指定,  example as follow:
<myTag:cupSize cupSize="1" cupSizes="${result}"></myTag:cupSize>

-----------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

相关标签: java自定义标签