1

I want to pass List object to html select.

The problem is, nothing appends to html select.

Here is a Obiekt class:

    package test;

public class Obiekt {

    private String nazwa;
    private String adres;
    private String dyscyplina;
    private int idObiekt;
    private int idTermin;
    public String getNazwa() {
        return nazwa;
    }
    public void setNazwa(String nazwa) {
        this.nazwa = nazwa;
    }
    public String getAdres() {
        return adres;
    }
    public void setAdres(String adres) {
        this.adres = adres;
    }
    public int getIdObiekt() {
        return idObiekt;
    }
    public void setIdObiekt(int idObiekt) {
        this.idObiekt = idObiekt;
    }
    public String getDyscyplina() {
        return dyscyplina;
    }
    public void setDyscyplina(String dyscyplina) {
        this.dyscyplina = dyscyplina;
    }
    public int getIdTermin() {
        return idTermin;
    }
    public void setIdTermin(int idTermin) {
        this.idTermin = idTermin;
    }
}

And here is a class which creates List of Obiekts:

package test;

import java.sql.*;
import java.util.ArrayList;

public class Wypelnij {

    Connection conn = null;
    public Wypelnij() throws ClassNotFoundException
    {

        PreparedStatement pst = null;  
        ResultSet rs = null;  

        String url = "jdbc:mysql://localhost:3306/";  
        String dbName = "zespolowy";  
        String driver = "com.mysql.jdbc.Driver";  
        String userName = "adminek";  
        String password = "123";  
        try {  
            Class.forName(driver).newInstance();  
            conn = DriverManager  
                    .getConnection(url + dbName, userName, password);
        } catch (Exception e) {  
            System.out.println(e);  
        } finally {  
            if (conn != null) {  
                try {  
                    conn.close();  
                } catch (SQLException e) {  
                    e.printStackTrace();  
                }  
            }  

        } 
    }

    public ArrayList<Obiekt> getObiekty() throws SQLException
    {
        ArrayList<Obiekt> obiekty = new ArrayList<Obiekt>();
        Statement pst = null;  
        ResultSet rs = null;
        String query = "SELECT nazwa,adres,dyscyplina from obiekty;";
        pst = conn.createStatement();
        rs = pst.executeQuery(query);

        while(rs.next()){
            Obiekt obiekt = new Obiekt();
            obiekt.setNazwa(rs.getString("nazwa"));
            obiekt.setAdres(rs.getString("adres"));
            obiekt.setDyscyplina(rs.getString("dyscyplina"));
            obiekty.add(obiekt);
        }
        return obiekty;

    }

}

And finally:

<select>
            <c:forEach items="${obiekty}" var="obiekt">
            <option value="1"><c:out value="${obiekt.nazwa}"/></option>
            </c:forEach>
</select>

Like I said, select from shows nothing. Where's the problem ? I included taglib prefix clause on the top of jsp page.

Afsun Khammadli
  • 1,973
  • 3
  • 15
  • 26
abecadlo
  • 111
  • 1
  • 9

2 Answers2

2

You can do this by two way:

1.Calling Java method inside jsp

<%
List<Obiekt> list = new Wypelnij().getObiekty();
%>
<select>
<%
for(Obiekt obiekt : list){
%>
<option value="1" ><%=obiekt.getNazwa()%><option/>
<% } %>
</select>

2.Also you can pass list to JSP from Servlet. And with JSTL you can display

List<Obiekt> list = new Wypelnij().getObiekty();
request.setAttribute("obiekty",list);
RequestDispatcher requestDispatcher =
    request.getRequestDispatcher("YourJspUrlGoesHere");
requestDispatcher.forward(request, response);

The following code must be in JSP page.

<select>
        <c:forEach items="${obiekty}" var="obiekt">
            <option value="1">${obiekt.nazwa}</option>
        </c:forEach>
</select>

I hope this will help you.

Afsun Khammadli
  • 1,973
  • 3
  • 15
  • 26
  • It does not work in both ways. As you mentioned second way, do i have to put this in doGet or doPost function ? – abecadlo Jun 07 '15 at 16:21
  • I second way you can pass which you call from jsp.But first way also must work.What is problem? Did you check database?May be there is no data in database – Afsun Khammadli Jun 07 '15 at 16:22
  • The both ways must work. But I do not know what is problem :) How can I help you? – Afsun Khammadli Jun 07 '15 at 16:28
  • Is there possibility to remember obiekt.id of selected item from this select menu ? I'm asking, because on next step i want to make reservation for this Obiekt i selected. – abecadlo Jun 07 '15 at 16:47
  • Yes you can store id for example in session. And select option selected for setting selected.This will help you http://stackoverflow.com/questions/15657367/selected-value-for-jsp-drop-down-using-jstl – Afsun Khammadli Jun 07 '15 at 16:49
0

Also check if your list object size (records fetched from DB) is greater than 0.

Nitin
  • 176
  • 1
  • 9