一种输入select内容
第一步
action实现
package *;
import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.jboss.logging.Logger;
import cn.*.util.service.*;
public class SelectTag extends SimpleTagSupport {
private static final Logger log = Logger.getLogger(SelectTag.class);
//缓存map
private static Map<String, List<Map<String, String>>> cacheSelectMap = new HashMap<String, List<Map<String, String>>>();
// 标签的属性
private String id;
private String code;
private String name;
private String sql;
private boolean cache=false;
// 执行数据访问属性
private Connection conn = null;
private Statement st = null;
private ResultSet rs = null;
private static String url = null;
private static String username = null;
private static String password = null;
static
{
url = (*.getJDBC_URL());
username = (*.getJDBC_USERNAME());
password = (*.getJDBC_PASSWORD());
// 注册驱动类
try
{
Class.forName(*.getJDBC_CLASS_NAME());
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
public void doTag() throws JspException, IOException {
try {
//获取页面输出流
Writer out = getJspContext().getOut();
if(cache&&cacheSelectMap.get(id)!=null&&cacheSelectMap.get(id).size()>0){//从缓存中写出数据
List<Map<String, String>> cacheData = cacheSelectMap.get(id);
for (Map<String, String> map : cacheData) {
String codeVo = map.get(code);
String nameVo = map.get(name);
out.write("<option value="+codeVo+">");
out.write(nameVo);
out.write("</option>");
}
}else{
//获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// 创建Statement对象
st = conn.createStatement();
// 执行查询
rs = st.executeQuery(sql);
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
// 在页面输出select
while (rs.next()) {
String codeVo = rs.getString(code);
String nameVo = rs.getString(name);
if(cache){//记录缓存
Map<String, String> map = new HashMap<String, String>();
map.put(code, codeVo);
map.put(name, nameVo);
list.add(map);
}
out.write("<option value="+codeVo+">");
out.write(nameVo);
out.write("</option>");
}
if(cache){
cacheSelectMap.put(id, list);
}
}
} catch (Exception e) {
e.printStackTrace();
log.error(e);
// throw new JspException("自定义标签错误" + cnfe.getMessage());
} finally {
// 关闭结果集
try {
if (rs != null)
rs.close();
if (st != null)
st.close();
if (conn != null)
conn.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public boolean isCache() {
return cache;
}
public void setCache(boolean cache) {
this.cache = cache;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
第二步
WEB-INF/tlds 下文件 selectTag.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">
<tlib-version>2.0</tlib-version>
<short-name>下拉框</short-name>
<uri></uri>
<!-- 无标签体 -->
<tag>
<name>option</name>
<tag-class>cn.*.ncp.tag.SelectTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>id</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
<attribute>
<name>cache</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
<attribute>
<name>code</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
<attribute>
<name>sql</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
</tag>
</taglib>
第三步
web.xml 引入自定义标签
<!-- 自定义标签 -->
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/selectTag</taglib-uri>
<taglib-location>/WEB-INF/selectTag.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/authTag</taglib-uri>
<taglib-location>/WEB-INF/authTag.tld</taglib-location>
</taglib>
</jsp-config>
第四步 jsp页面引入
<%@taglib prefix="select" uri="/WEB-INF/tlds/selectTag.tld"%>
应用
<select:option id="org_add_city" cache="true" name="" code="" sql=""/>
第二种权限 自定义标签
action
package *;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 权限标签
* @author Administrator
*
*/
public class AuthorTag extends TagSupport{
//菜单编号
private String code;
@Override
public int doStartTag() throws JspException {
//默认无权限
boolean result = false;
if("0".equals(code)){//有权限
result = true;
}
return result ? EVAL_BODY_INCLUDE : SKIP_BODY;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
第二步
在 WEB-INF/tlds 下创建文件 authTag.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">
<tlib-version>1.0</tlib-version>
<short-name>author</short-name>
<uri></uri>
<tag>
<name>a</name>
<tag-class>cn.*.ncp.tag.AuthorTag</tag-class>
<body-content>tagdependent</body-content>
<attribute>
<name>code</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
</tag>
</taglib>
第三步
web.xml 引入自定义标签
第四步 jsp页面引入
<%@taglib prefix="auth" uri="/WEB-INF/tlds/authTag.tld"%>
应用
<auth:a code="0">
<button >xxx</button>
</auth:a>