java - Filling dropdown list with related tables name in JSP -
i have following structure of database created in hibernate4:
department (has fields: department_id , department_name) connected through department_institute (has department_id , institute_id) institute (has institute_id , institute_name) later connected through institute_teacher (by id's again) teacher (teacher_id, name, surname, title).
it has 1 many relationship 1 department can have many institutes , 1 institute can have many teachers.
i put object , through jstl put 1 dropdown menu (dropdown list doesn't need have few levels, can on same level). problem is, don't know:
- how query data , put them 1 list,
use jstl in jsp put them in dropdown menu.
could tell me how that? love understand that. here skeleton of method querying "everything":
[edit] here made method querying database, if have if it's solution or not, grateful
public list<string> enlisteverything(){ session session = sessionfactory.opensession(); transaction tx = null; list<string> toview = new arraylist<string>(); try{ tx = session.begintransaction(); hashset<department> departments = (hashset<department>)session.createquery("from department").list(); hashset<institute> institutes = new hashset<institute>(); hashset<teacher> teachers = new hashset<teacher>(); for(department d : departments){ toview.add(d.getdepartmentname()); institutes.addall(d.getinstitutes()); } for(institute : institutes){ toview.add(i.getinstitutename()); teachers.addall(i.getteachers()); } for(teacher t : teachers){ toview.add(t.getteachertitle() + t.getteachername() + t.getteachersurname()); } }catch(hibernateexception e){ if (tx!=null) tx.rollback(); e.printstacktrace(); } { session.close(); } return toview; }
i've done datamodel using particular tutorial: http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-one-to-many-1.html inside department class there hashset institutes , in institutes there hashset teachers. thank in advance answers.
i put object , through jstl put 1 dropdown menu (dropdown list doesn't need have few levels, can on same level).
use toview
map instead of list keep key-value pairs put dropdown menu.
map<string,string> toview = new linkedhashmap<string,string>(); .. .. toview.put(d.getdepartmentid()+"dept",d.getdepartmentname()); toview.put(i.getinstituteid()+"inst",i.getinstitutename()); toview.put(t.getteacherid()+"teach",t.getteachertitle() + t.getteachername() + t.getteachersurname());
add map
request
:
map<string, string> toview = yourdataprovider.enlisteverything(); request.setattribute("toview", toview);
now in jsp, use put values in dropdown menu:
<select name="mapdata"> <c:foreach items="${toview}" var="data"> <option value="${data.key}">${data.value}</option> </c:foreach> </select>
Comments
Post a Comment