第一步:在web项目下的web-inf的tlds目录下,新建一个配置文件,名字为relation.tld,内容如下:
自定义标签的前缀为relation(由short-name标签决定),属性有saveField(要保存到数据库的字段名),value(要被选中的数据值),disField(作为显示文本的字段,相当select标签的option之间的文本),tableName(下拉框要显示的表的名称),name(相当html标签的name),id(相当html标签的id),cssClass(相当html标签的class),styleClass(相当html标签的style),onChange(相当html标签的onChange),condition(输入条件表达式)。
<?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>relation</short-name>
<uri>http://www.wang.com/relation</uri>
<tag>
<description>下拉框自定义标签</description>
<name>select</name>
<tag-class>com.tag.RelationSelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>saveField</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>disField</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>tableName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cssClass</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>styleClass</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>onChange</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>condition</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
第二步:新建标签处理类,用来接收自定义标签的属性,和返回结果,内容如下:
package com.tag;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.springframework.util.StringUtils;
import com.bxsurvey.sys.params.model.SysParams;
import com.bxsurvey.sys.params.service.SysParamsServiceI;
import net.framework.utils.SpringWiredBean;
public class RelationSelectTag extends TagSupport{
private String tableName;
private String value;
private String saveField;
private String disField;
private String name;
private String id;
private String cssClass;
private String styleClass;
private String onChange;
private String condition;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getCssClass() {
return cssClass;
}
public void setCssClass(String cssClass) {
this.cssClass = cssClass;
}
public String getStyleClass() {
return styleClass;
}
public void setStyleClass(String styleClass) {
this.styleClass = styleClass;
}
public String getOnChange() {
return onChange;
}
public void setOnChange(String onChange) {
this.onChange = onChange;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSaveField() {
return saveField;
}
public void setSaveField(String saveField) {
this.saveField = saveField;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getDisField() {
return disField;
}
public void setDisField(String disField) {
this.disField = disField;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
@Override
public int doEndTag() throws JspException{
String tableName = this.getTableName();
String name = this.getDisField();
String value = this.getSaveField();
String condition = this.getCondition();
String sql= "select "+value+","+name+" from "+ tableName;
if(condition!=null&&!"".equals(condition)){
hql+=" "+condition;
}
List list = sysParamsService.findBySql(sql);//自己定义方法查询数据库
JspWriter out = pageContext.getOut();
StringBuffer sb = new StringBuffer();
sb.append("<select name='"+this.getName()+"' id='"+this.getId()+"'");
if(!StringUtils.isEmpty(this.getCssClass())){
sb.append("class=\"" + this.getCssClass() + "\" data-live-search='true'");
}
if(!StringUtils.isEmpty(this.getStyleClass())){
sb.append("style=\"" + this.getStyleClass() + "\" ");
}
if(!StringUtils.isEmpty(this.getOnChange())){
sb.append("onchange=\"" + this.getOnChange() + "\" ");
}
sb.append(">");
sb.append("<option value=''>--请选择--</option>");
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Object[] tm = (Object[]) iterator.next();
String saveValue = tm[0].toString();
String display = tm[1].toString();
if(saveValue.equals(this.getValue())){
sb.append("<option value='"+saveValue+"' selected='selected'>");
}else{
sb.append("<option value='"+saveValue+"'>");
}
sb.append(display+"</option>");
}
sb.append("</select>");
try {
out.write(sb.toString());
} catch (IOException e) {
throw new JspException(e);
}
return TagSupport.EVAL_PAGE;
}
}
第三步:如何使用自定义标签
和引入其他标签库一样,在jsp页面头部引入自定义标签库,文件在在web项目下的web-inf的tlds目录下,就是上面新建的relation.tld。
<%@taglib prefix="relation" uri="http://www.wang.com/relation" %>,其中prefix="relation"由relation.tld文件中的<short-name>relation</short-name>决定,uri="http://www.wang.com/relation"由<uri>http://www.wang.com/relation</uri>决定。
接下来就可以使用自定义标签了
<relation:select value="" saveField="ID" disField="CO_NAME" tableName="TB_COMPANY" name="departId" id="departId" cssClass="form-control" onChange="alert();"/>