Populate JSP dropdown with database info

后端 未结 2 512
遥遥无期
遥遥无期 2021-02-06 03:30

I\'m trying to populate a JSP dropdown from a database table.

Here\'s the code that will create the array and fill it with the database info:

// this wil         


        
2条回答
  •  一个人的身影
    2021-02-06 03:50

    First, in your JSP import the class you are trying to use:

    <%@ page import="com.mypackage.MyClass" %>
    

    Then you can use that class as you would normally do:

    <%
        MyClass c = new MyClass();
        c.getSomeProperty();
    %>
    

    To fill the control, you iterate your array and set the value argument of the option tag:

    
    

    As you can see, there's mixed Java code and HTML. First it outputs the select tag, then on Java code there's a while loop iterating a list of objects. This could be your ResultSet, an array or some other collection. For each iteration it creates an option tag with some value, this would be the value you want the user to see.

    This is the basic approach, using only JSP. But there are many tag libraries, for example JSTL, that provide things like iteration so you can write things like:

    
    

提交回复
热议问题