我们日常开发中有时候经常遇到一个页面好多Select下拉框,下拉框的内容都被维护在一张字典表中,然后根据字典类型获取不同的数据作为下拉列表展示。这样很繁琐,每次都要需要写一堆东西来初始化下拉选项,我们可以利用jspTag自定义标签来完成。
- 自定义标签
/**
* <p>
* <code>DictSelectTag</code>
* </p>
* 基于JspTag的公共选择下拉框
* @author jianzhang11
* @version 1.0
* @desc
* @Date: 2016/5/18 18:43
* @since 1.0
*/
public class DictSelectTag extends TagSupport{
//获取系统定义的service
private BaseDicService baseDicService = SpringContextHolder.getBean("baseDicService");
private String id;
private String name;
private String value;
private String type;
/**
* 前端CSS样式
*/
private String cssStyle;
@Override public String getId() {
return id;
}
@Override public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setValue(String value) {
this.value = value;
}
public void setType(String type) {
this.type = type;
}
public void setCssStyle(String cssStyle) {
this.cssStyle = cssStyle;
}
/**
* 自定义标签实现
* @return
* @throws JspException
*/
@Override
public int doEndTag() throws JspException {
JspWriter out = pageContext.getOut();
List<BaseDic> dicList = baseDicService.findBaseDicsByType(type);
StringBuffer sb = new StringBuffer();
sb.append("<select name='"+name+"' id='"+this.getId()+"' class='"+cssStyle+"' >");
sb.append("<option value=''>--请选择--</option>");
for (BaseDic baseDic : dicList) {
if(baseDic.getValue().equals(value)){
sb.append("<option value='"+baseDic.getValue()+"' selected='selected'>");
}else{
sb.append("<option value='"+baseDic.getValue()+"'>");
}
sb.append(baseDic.getText()+"</option>");
}
sb.append("</select>");
try {
out.write(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
return TagSupport.EVAL_PAGE;
}
}
- 新建tld文件配置标签
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>dict</short-name>
<uri>http://iflytek.com/dict_tag</uri>
<tag>
<description>Renders an HTML 'select' element. Supports databinding to the selected option.</description>
<name>select</name>
<tag-class>com.iflytek.esociety.common.tag.DictSelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>type</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cssStyle</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
- 页面调用
<%@taglib prefix="dict" uri="/WEB-INF/tld/dict.tld" %>
<dict:select name="hyzk" type="0004" value="${graduates.hyzk}"></dict:select>
简单三步即可在页面完成下拉框的渲染,在后台Service获取字典表内容的时候建议加上缓存,这样就不需要多次查询数据库了!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。