2010-07-16 107 views
2

我知道这已被问过,但它不适合我。如何使用jstl从数据库填充下拉列表?

我想使用JSTL来填充下拉从数据库列表,这是我作为一个useBean的使用类:

public class feedData { 

    Connection con; 

    public feedData() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { 

     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     String url = "******"; 

     con = DriverManager.getConnection(url, "**", "**"); 

    } 

    public ArrayList<String> getUnis() throws SQLException { 
     ArrayList<String> uniList = new ArrayList<String>(); 
     String tryquery = "select aff from libra.smalluniqdbtmp"; 
     Statement stmt2 = con.createStatement(); 
     ResultSet rs1 = stmt2.executeQuery(tryquery); 

     while (rs1.next()) { 

      uniList.add(rs1.getString("aff")); 

     } 

     return uniList; 
    } 

    public ArrayList<String> getRI() throws SQLException { 
     ArrayList<String> RIList = new ArrayList<String>(); 
     String tryquery = "select interest from libra.riuniqdb"; 
     Statement stmt2 = con.createStatement(); 
     ResultSet rs1 = stmt2.executeQuery(tryquery); 

     while (rs1.next()) { 

      RIList.add(rs1.getString("aff")); 

     } 

     return RIList; 
    } 
} 

,这里是我的jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 

     <jsp:useBean id="obj" scope="page" class="uiLibraWeb2Pkg.feedData" /> 
     <h1>Hello World!</h1> 
     <table border="1"> 
      <thead> 
       <tr> 
        <th></th> 
        <th></th> 
        <th></th> 
        <th></th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td> <form action="response.jsp"> 
          <strong>Select a university:</strong> 

          <select name="affiliation"> 
           <c:forEach var="aff" items="${obj.unis}"> 
            <option value="${aff}"></option> 
           </c:forEach> 

          </select> 

         </form> 

        </td> 
        <td></td> 
        <td></td> 
        <td></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td></td> 
        <td></td> 
        <td></td> 
       </tr> 
      </tbody> 
     </table> 

    </body> 
</html> 

有没有错误消息在服务器日志和项目的生成输出中,但下拉列表为空。一直在努力,我不知道什么是错的。我也尝试设置一个数据源,并使用jstl遍历查询的结果集来执行此操作,但是其行为方式相同。

现在我不依赖任何数据源,但仍然是相同的结果。

帮助表示赞赏,感谢

回答

5

我还没有测试过你的代码,但是请在服务器端添加一个断点来检查列表中有多少元素。也许它在服务器端也是空的。

的第二点是,这个代码

<option value="${aff}"></option> 

不显示任何东西给用户,因为它是在HTML渲染为不带文字的选项。也许它应该是

<option value="${aff}">${aff}</option> 
+0

感谢您的建议,工作。我愚蠢。 – 2010-07-16 20:27:31

1

右击在网页浏览器的HTML页面和查看源。很有可能你看到JSTL标签没有在那里解析。这是正确的,你没有在JSP的顶部导入JSTL标签库为每JSTL core taglib documentation

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

(以及由@Guido进一步提及也不要忘记设置选项标签)

如果这样不能解决问题,并且你得到了无法识别的taglib的编译错误,那么这意味着servletcontainer问题并没有附带JSTL,你必须自己安装JSTL。这基本上很简单:只需将jstl-1.2.jar放入您的web应用的/WEB-INF/lib并重新部署即可。

如果您想知道为什么JSP标签不需要它,那是因为它们默认已被JspServlet识别。任何其他taglibs应该在JSP的顶部明确声明为@taglib


与实际问题无关:您的JDBC代码看起来不正确。你没有关闭连接,从而泄漏资源。如果您长时间重复运行此操作,那么数据库将耗尽连接,并且您的应用程序将中断。您可能会发现this article有助于了解如何正确使用JDBC代码。

+0

谢谢你所指的taglib在代码中。当我从代码中删除一些评论时,它被删除了。我的坏,不能指望人们在我犯这样的错误时提供帮助。 感谢您的JDBC文章,我会查找它。 – 2010-07-16 20:29:33