ArrayList <Employee> Dropdownlist
<jsp:useBean id="loadData" class="com.myclass" scope="session"/>
<select>
<c:forEach var="?" items="?">
<option ?>
</c:forEach>
</select>
Hi, in the class "myclass" i get an ArrayList of objects like ...let's say Employee, so this is an array of Employees. Well I want to pupulate a combo in my jsp with only the names of those employees ...like Employee.getName(). How can i do that using jstl?
So let me put it in other words, The "myclass" i'm using is getting a List of employees from the database with all their attributes. I'm retrieving those employees and putting them into an ArrayList. In my jsp i want to get that ArrayList and populate a dropdownlist with only the names of the employees that i previously retrieved. How can i do that?
# 1
use c.tld and try this...........
<c:forEach items="${Employee}" var="sal" varStatus="idx">
<html:select property="salaries" >
<html:option value="<c:out value="${sal}" />" > <c:out value="${sal}" /> </html:option>
</html:select>
</c:forEach>
Employee is the list of Employee beans
sal is the property
Employee{
double sal;
dobule getSal(){
return this.sal;
}
public void setSal(double sal){
this.sal = sal
}
}
sal in var is property in Employee bean....
# 2
How did you guess that he was using struts?
Anyway, the JSTL + HTML solution look like:
<jsp:useBean class="mypackage.EmployeeBean" id="employeeBean" scope="request" />
<select>
<c:forEach items="${employeeBean.employees}" var="employee">
<option value="<c:out value="${employee.id}" />"><c:out value="${employee.name}" /></option>
</c:forEach>
</select>
mypackage.EmployeeBean:private List<Employee> employees; // + getter + setter
Employeeprivate Long id;
private String name;
// + getters + setters
# 3
That's perfect....thanks but i'm still having problems. I still can't figure out how to set the List of employees from a database. I can do it but not keeping the MVC pattern. I just want a little help by telling me how to get the data from the database. When i display the jsp i want the data loaded already in my form at that point i'm not using the form per se, just loading data in the combo box....what should i use....which is the procedure i should take.
# 4
You can use JDBC+SQL to load data from a DB.
# 5
> That's perfect....thanks but i'm still having
> problems. I still can't figure out how to set the
> List of employees from a database. I can do it but
> not keeping the MVC pattern. I just want a little
> help by telling me how to get the data from the
> database. When i display the jsp i want the data
> loaded already in my form at that point i'm not using
> the form per se, just loading data in the combo
> box....what should i use....which is the procedure i
> should take.
You could use the DAO design pattern for that
http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html
Here's a catalog of all patterns:
http://java.sun.com/blueprints/corej2eepatterns/Patterns/index.html
MVC is the Front Controller pattern.
# 6
now i don't have errors but in my jsp any data is shown...it seems that the tag usebean can't find the object instantiated....what could be happening?
<form name="form1" method="post">
Formulario de Pre-visita
<table>
<tr>
<td>Dependencia</td>
<td>
<input type="text" name="inputdependencia" readonly="true" >
</td>
<td>
<jsp:useBean id="dependenciaBean" scope="request" class="com.cinfo.bean.EDependencia" />
<select>
<c:forEach items="${dependenciaBean.dependencias}" var="dependencia">
<option value="<c:out value="${dependencia.nom_dependencia}" />"><c:out value="${dependencia.nom_dependencia}" /></option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>Fecha:</td>
<td>
<input name="inputfecha" type="text" readonly="true">
</td>
<td>
<a href="javascript:cal1.popup();">
<img src="img/tigracal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date">
</a>
</td>
</tr>
</table>
</form>
public class EDependencia {
private String cod_dependencia;
private String nom_dependencia;
private char est_dependencia;
private Date fec_registro;
private String cod_usuario;
private List <EDependencia> dependencias;
# 7
i mean any data is NOT shown.........the drop down list is not being populated