jsp - how to load values in dropdown using jstl -
i searching way populate dropdown using jstl. need when page loading. tried code. not working.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <select> <c:foreach items="${requestscope.personlist}" var="person"> <option value="${person.getid()}">${person.getname()} </option> </c:foreach> </select>
and controller servlet code.
protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); try { casualleaveservice service1 = new casualleaveserviceimpl(); list<person> personlist = service1.searchname(); request.setattribute("personlist", personlist); requestdispatcher rd = getservletcontext().getrequestdispatcher("/casual"); rd.forward(request, response); } catch (exception e) { e.printstacktrace(); } }
server class code.
public list<person> searchname() { list<person> list = null; try (daosession session = daosessionfactory.createsession()) { employeedao emp = session.getemployeedao(); list = emp.findallpersons(); if (!list.isempty()) { system.out.println("my list :"+list); } else { system.out.println("error in list"); } } return list; }
person class
public class person { private int id; private string employeeno; private string name; @validno(message = "must number between 1 999") private integer epfno; public person() { } /** * * @param id * @param employeeno * @param name initials + surname * @param epfno */ public person(int id, string employeeno, string name, integer epfno) { this.id = id; this.employeeno = employeeno; this.name = name; this.epfno = epfno; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getemployeeno() { return employeeno; } public void setemployeeno(string employeeno) { this.employeeno = employeeno; } /** * * @return initials + surname */ public string getname() { return name; } /** * * @param name initials + surname */ public void setname(string name) { this.name = name; } public integer getepfno() { return epfno; } public void setepfno(integer epfno) { this.epfno = epfno; } @override public int hashcode() { int hash = 7; hash = 67 * hash + this.id; return hash; } @override public boolean equals(object obj) { if (obj == null) { return false; } if (getclass() != obj.getclass()) { return false; } final person other = (person) obj; return this.id == other.id; }
}
web.xml mapped this.
<servlet> <jsp-file>/web-inf/views/leave/casualleave.jsp</jsp-file> <servlet-name>casualleave</servlet-name> </servlet> <servlet-mapping> <servlet-name>casualleave</servlet-name> <url-pattern>/casual</url-pattern> </servlet-mapping>
i can assure service class working fine. because called service class method directly jsp , output coming perfectly. problem using jstl methods not working.
can tell me error. because don't use scriptlet tags within jsp html code. heard bad practice.
thank you.
this right instead of method ${person.getid()}
have write property name ${person.id}
because when write ${person.id}
, automatically call getid()
method of person
bean. try using below code :
<select> <c:foreach items="${personlist}" var="person"> <option value="${person.id}">${person.name}</option> </c:foreach> </select>
Comments
Post a Comment