Reputation: 2076
I'm trying to populate my jsp's dropdown using jstl. My jsp is mapped like this.
<servlet>
<jsp-file>/WEB-INF/views/dilini/newjsp.jsp</jsp-file>
<servlet-name>newjsp</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>newjsp</servlet-name>
<url-pattern>/newjsp</url-pattern>
</servlet-mapping>
Also this is my jstl loaded dropdown.
<div class="dropdown">
<button class="btn btn2 btn-default dropdown-toggle" type="button" id="menu2" data-toggle="dropdown">--Select--
<span class="caret"></span></button>
<ul class="dropdown-menu" role="menu" aria-labelledby="menu2">
<c:forEach items="${personList}" var="person">
<li role="presentation"><a data-myAttribute="${person.getId()}" class="list2" href="#">${person.getName()}</a></li>
</c:forEach>
</ul>
</div>
And this is my controller servlet.
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();
if (personList.isEmpty()) {
response.getWriter().write("EMPTY ");
} else {
response.getWriter().write("NOT EMPTY ");
request.setAttribute("personList", personList);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/newjsp");
rd.forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
When I run my controller by own it shows something like this.
I really don't understand why dropdown is not visible the servlet. Please help me. Thank you.
Added more to answer. Hope this will help some one.
Also I should add another one when jsp is calling "by clicking button or clicking link" you should first got to controller and then controller will redirect the jsp. This was the missing part.
Once again thank you all for your support.
Upvotes: 0
Views: 678
Reputation: 2924
Summarizing the comments into an answer:
Your current approach is good, continue using the servlet for loading the data, and JSP only to display them (or an error message). I.e. avoid using JSP URLs in your links/form actions, use always an URL of a servlet, the servlet should load the data and then forward the request to an appropriate JSP. It is exactly the Model/View/Controller pattern/architecture.
Just few suggested changes to your code:
JSP: handle the case of empty list
<div class="dropdown">
<button class="btn btn2 btn-default dropdown-toggle" type="button" id="menu2" data-toggle="dropdown">--Select--<span class="caret"></span></button>
<ul class="dropdown-menu" role="menu" aria-labelledby="menu2">
<c:if test="${empty personList}">
<li>No person available</li>
</c:if>
<c:forEach items="${personList}" var="person">
<li role="presentation"><a data-myAttribute="${person.getId()}" class="list2" href="#">${person.getName()}</a></li>
</c:forEach>
</ul>
</div>
Servlet: do not tackle with the response, that's the task of a view; just load data and forward to the view (JSP)
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
CasualLeaveService service1 = new CasualLeaveServiceImpl();
List<Person> personList = service1.searchName();
request.setAttribute("personList", personList);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/WEB-INF/views/dilini/newjsp.jsp");
rd.forward(request, response);
} catch (Exception e) {
e.printStackTrace(); // bad practice, use some logger
request.setAttribute("error", "Error loading person list: " + e.getMessage());
RequestDispatcher rd = getServletContext().getRequestDispatcher("/WEB-INF/views/dilini/error.jsp"); // you have to create one
rd.forward(request, response);
}
}
Upvotes: 1
Reputation: 1256
You should be accessing the properties using the property name rather than the getXXX() way you are doing it now.
<li role="presentation"><a data-myAttribute="${person.id}" class="list2" href="#">${person.name}</a></li>
Upvotes: 0