【问题标题】:Unable to get data in dropdown in jsp无法在jsp的下拉列表中获取数据
【发布时间】:2014-07-20 02:26:59
【问题描述】:

我有一个要求,我需要将数据库中的数据显示到下拉列表。直到动作类显示数据,甚至在动作类中显示。但是当涉及到jsp。下拉菜单为空。

这里有jsp标签

html:select property="strName" tabindex="1">
<html:option value="">-- Select --</html:option>
<logic:notEmpty name="NameList">
<html:optionsCollection  name="PinRequestActionForm" property="NameList" label="pinrequestVO.strNameCD" value="pinrequestVO.strNameId" />
</logic:notEmpty>
</html:select>

请建议我如何解决此问题。

【问题讨论】:

  • 你有命名列表的属性吗?

标签: java jsp struts


【解决方案1】:

如果将数据添加到 NameList 中,您可以使用 &lt;logic:iterate&gt; 标签显示:

<html:select property="strName" tabindex="1"> <logic:iterate id="x" name="NameList"> <html:option value='${x.strNameId}'>${x.strNameCD} </html:option> </logic:iterate> </html:select>

【讨论】:

    【解决方案2】:

    我已经参考了这个链接。 http://www.roseindia.net/answers/viewqa/JSP-Servlet/29460-how-to-get-data-from-database-into-dropdownlist-in-jsp.html

    在你的程序中使用这个 servlet 代码。

    Customer Name:
    
    <select name="name">
         <% 
         try{
    
            Class.forName("com.mysql.jdbc.Driver");
    
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/scs","root","root");
    
            Statement st=con.createStatement();
            String sql="select * from staff";
            ResultSet rs=st.executeQuery(sql);
            while(rs.next()){
             //String name = rs.getString("name");
         }
       %>
       <option value="<%=rs.getString("fname")%>"></option>
    
         <%
            }catch(Exception e){
            }     
         %>
    
     </select>
    

    【讨论】:

    • 这是一个不合适的解决方案,原因有很多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2021-08-15
    • 2016-02-04
    相关资源
    最近更新 更多