JSP自定义标签
WEB页面
导入
<%@taglib uri="/gacl" prefix="xdp"%>
选项:
<xdp:search id="xdp" url="/web/user/userhours" callback="fill_data">
<xdp:field id="searchSelect" name="sfz" type="xdp"></xdp:field>
<xdp:field id="sfz" name="sfz" type="text"></xdp:field>
<xdp:field id="sfz" name="sfz" type="date"></xdp:field>
<xdp:field id="cph" name="cph" type="number"></xdp:field>
<xdp:field id="cph" name="cph" type="list" ></xdp:field>
<xdp:field id="xm" name="xm" type="date"></xdp:field>
<xdp:field id="xm" name="xm" type="number"></xdp:field>
<xdp:field type="search" name=""></xdp:field>
</xdp:search>
web.xml
<jsp-config>
<taglib>
<taglib-uri>/gacl</taglib-uri>
<taglib-location>/WEB-INF/gacl.tld</taglib-location>
</taglib>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
gacl.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>自定义标签库</description>
<tlib-version>1.0</tlib-version>
<short-name>GaclTagLibrary</short-name>
<uri>/gacl</uri>
<tag>
<name>search</name>
<tag-class>com.safeluck.common.TagDemo1</tag-class>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>url</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>callback</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>field</name>
<tag-class>com.safeluck.common.TagDemo1</tag-class>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>type</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>datasource</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
TagDemo1.java
package com.safeluck.common;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
public class TagDemo1 extends TagSupport {
//标签属性
private String datasource =null;
private String name = null;
private String type = null;
private String id = null;
private String url = null;
private String callback = null;
private String headChoise = "身份证,车牌号,姓名";
private String selectVal = "sfz,cph,xm";
private String displayStirng =null;
public int doStartTag() throws JspTagException {
return super.EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspTagException{
pageContext.getSession();
JspWriter out = pageContext.getOut();
if (type != null) {
try {
String[] alltypes =headChoise.split(",");
String[] selectVals =selectVal.split(",");
if (alltypes.length >0 && null != name){
displayStirng = (name.equals(selectVals[0])?"style='display:block;'":"style='display:none;'");
switch (type) {
case "xdp":
out.print("<script type=\"text/javascript\">");
out.print("function change_dis(){");
for(int i =0;i <selectVals.length;i++) {
out.print("$(\"input[name='"+selectVals[i]+"']\").hide();");
out.print("$(\"div[name='"+selectVals[i]+"']\").hide();");
out.print("$(\"select[name='"+selectVals[i]+"']\").hide();");
}
out.print("var val = $(\"select[name='"+id+"']\").val();");
out.print("$(\"input[name='\"+val+\"']\").show();");
out.print("$(\"div[name='\"+val+\"']\").show();");
out.print("$(\"select[name='\"+val+\"']\").show();");
out.print("}");
out.print("function clickToURL(){");
out.print("var val = $(\"select[name='"+id+"']\").val();");
out.print("var subString =\"\";");
out.print("$(\"input[name='\"+val+\"']\").each(function(){");
out.print("var id =$(this).attr(\"id\");");
out.print("var worth =$(this).val();");
out.print("if(worth != \"\"){");
out.print("subString =subString+ id+\"=\"+worth+\"&\";");
out.print("}");
out.print("});");
out.print("$(\"select[name='\"+val+\"']\").each(function(){");
out.print("var id =$(this).attr(\"id\");");
out.print("var worth =$(this).val();");
out.print("subString =subString+ id+\"=\"+worth+\"&\";");
out.print("});");
out.print("subString=subString.substring(0,subString.length-1);");
out.print("alert(subString);");
out.print("alert(\"${ctx}\"+\""+url+"?\"+subString);");
out.print("}");
out.print("</script>");
out.print("<select name='" + id + "' onchange='change_dis()'>");
for (int i =0;i <alltypes.length;i++){
out.print("<option value ='"+selectVals[i]+"'>"+alltypes[i]+"</option>");
}
out.print("</select>");
break;
case "list":
out.print("<select id='listCon"+name+"' name='"+name+"'"+displayStirng+"/>");
out.print(StringCondition());
out.print("<select id='list"+name+"' name='"+name+"'"+displayStirng+">");
for (int i = 0; i < 5; i++) {
out.print("<option value ='" + i + "'>" + i + "</option>");
}
out.print("</select>");
break;
case "text":
out.print("<select id='textCon"+name+"' name='"+name+"'"+displayStirng+">");
out.print(StringCondition());
out.print("<input id='text"+name+"' type='" + type + "' name='" + name + "' id='" + id + "' "+displayStirng+">");
break;
case "date":
out.print("<div name='" + name + "' "+displayStirng+">开始时间:" +
"<input id='begin"+id+"' type='"+type+"' name='"+name+"' onclick='WdatePicker({dateFmt:\'yyyy-mm-dd\'})'>");
out.print("结束时间:<input id='end"+id+"end' type='"+type+"' name='"+name+"' onclick='WdatePicker({dateFmt:\"'yyyy-mm-dd'\"})'></div>");
break;
case "number":
out.print("<select id='numCon"+name+"' name='"+name+"'"+displayStirng+">");
out.print(StringCondition());
out.print("<input id='num"+name+"' type='" + type + "' name='" + name + "' id='" + id + "' "+displayStirng+">");
break;
case "search":
out.print("<button type='button' id='ay-search-btn' onclick='clickToURL();'>查询</button>");
break;
default:
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return super.SKIP_BODY;
}
public String StringCondition(){
StringBuffer strBu = new StringBuffer();
strBu.append("<option value ='='>等于</option>");
strBu.append("<option value ='!='>不等于</option>");
strBu.append("<option value ='>='>大于等于</option>");
strBu.append("<option value ='<='>小于等于</option>");
strBu.append("<option value ='>'>大于</option>");
strBu.append("<option value ='<'>小于</option>");
strBu.append("</select>");
return strBu.toString();
}
/* 释放资源 */
public void release(){
super.release();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getCallback() {
return callback;
}
public void setCallback(String callback) {
this.callback = callback;
}
}