用JSP自定义标签实现下拉列表框

chinautopian 2006-03-13 07:36:39
我想做一个这样功能:从数据库取出一组数据,然后把它加到下拉列表框中去,打算用JSP里的自定义标签来实现,以下是我的代码:
app.tld:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.2</jspversion>
<shortname>Application Tag Library</shortname>
<uri>http://jakarta.apache.org/taglibs/struts-example-1.0</uri>
<info>
</info>
<tag>
<name>personnelTag</name>
<tagclass>com.utopian.tag.personnelTag</tagclass>
<bodycontent>empty</bodycontent>
<info>
</info>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>page</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>

personnelTag.java
public final class personnelTag extends TagSupport
{
////
public int doStartTag() throws JspException
{
////////
return SKIP_BODY;
}

//////
public int doEndTag() throws JspException
{
ServletRequest request = pageContext.getRequest();

java.sql.Connection conn = null;
ResultSet rs = null;
List personnel = new ArrayList();

try {
Context ictx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ictx.lookup ("java:comp/env/jdbc/user");

conn = ds.getConnection();

Statement stmt = conn.createStatement();
String strSql = " select username from user";
rs = stmt.executeQuery(strSql);
while (rs.next()) {
personnel.add(rs.getString(1));
System.out.println(rs.getString(1));
}
if (null != rs) {
rs.close();
}
if (null != stmt) {
stmt.close();
}
if (null != conn) {
conn.close();
}

} catch (Exception e) {

}

request.setAttribute("personnelList",personnel);

//pageContext.setAttribute("personnelList",personnel);

return (EVAL_PAGE);
// return (SKIP_PAGE);
} //end doEndTag
}
Test.jsp
<%@ page contentType="text/html;charset=GB2312" language="java" %>
<%@ taglib uri="/WEB-INF/app.tld" prefix="app" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<head>
<title>Test</title>
<html:base/>
</head>
<body bgcolor="white">
<app:personnelTag />
<bean:write name="personnelList" />
<html:select property="vcname">
<html:options collection="personnelList" />
</html:select>
</body>
</html:html>
但是会出现错误:
javax.servlet.ServletException: Cannot find bean under name org.apache.struts.taglib.html.BEAN
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.Test_jsp._jspService(Test_jsp.java:83)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
我确信值已经传过去了,因为如果只是用<bean:wtite>的话是可以正确打出数据来的,请各位大侠能帮我解决这个问题!谢谢!!!!
...全文
278 点赞 收藏 回复
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2006-03-13 07:36
社区公告
暂无公告