Populate JSP dropdown with database info

后端 未结 2 469
遥遥无期
遥遥无期 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:53

    I would suggest trying to stay away from mixing the display and model code. Keep all of your html in the jsp page and create model backing objects that provide the information you need. For example, let's say you have a simple Java class that has a list of objects:

    package com.example;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ListBean {
    
        public List getItems() {
            List list = new ArrayList();
            list.add("Thing1");
            list.add("Thing2");
            list.add("Thing3");
            return list;
        }
    }
    

    It doesn't matter how the getItems method constructs the list that it is returning. To display these items in the JSP Page using JSTL you would do the following:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
    
    
    
    
    
    Insert title here
    
    
    
    
    
    
    
    
    

    Instead of using useBean the items collection used in the forEach loop could also come from the session or a request object.

    This link also has good advice: http://java.sun.com/developer/technicalArticles/javaserverpages/servlets_jsp/

提交回复
热议问题