i have this as JSP page and i want a combobox to fill up the values from a table in the database. I have a class Assembly and a Class Assemblys, in Assemblys i make every record an instance of Assembly and put it in a list. But i can't retrieve it in JSTL? Why is this?
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
.....
<select name = "assembly">
<c:forEach var ="item" items="${Assemblys}">
<option value="${item.id}">${item.Name}</option>
</c:forEach>
</select>
This as the class Assembly Class
public class Assembly {
public String Name;
public int cost;
public int id;
public Assembly(String Name, int cost, int id) {
this.Name = Name;
this.cost = cost;
this.id = id;
}
}
And this is the class Assemblys
public class Assemblys {
List<Assembly> list = new ArrayList<Assembly>();
public Assemblys(){
list = getAssemblys();
}
private List<Assembly> getAssemblys() {
try {
Connection conn = MysqlConnect.conn();
PreparedStatement pstmt = null;
ResultSet rs = null;
String Query = "Select * from tbl_assembly";
pstmt = conn.prepareStatement(Query);
rs = pstmt.executeQuery();
while (rs.next()) {
Assembly ass = new Assembly(rs.getString(2),rs.getInt(3),rs.getInt(1));
list.add(ass);
}
conn.close();
} catch (SQLException ex) {
Logger.getLogger(Assemblys.class.getName()).log(Level.SEVERE, null, ex);
}
return list;
}
}
Why is my drop down list blank? I don't get it. I thought this was the right code?
Many thanks in advance!
You need a servlet or using scriptlet(marked as bad practice) in jsp to invoke class Assemblys. The tag you specified, i am guessing that you are using servlet/jsp application.
The name Assemblys used in jsp should be in request object as attributes. The key you specify for Assemblys can be used for fetch the list.