I have DAO bean rows retrieved in a List. In my JSP I am accessing the List to iterate thru to populate my page. My JSP can't access the List because it says it must be a String when I execute a request.getParameter. How I convert this to String eventually populate my page?
public List getAccessRequest()
{
List accessRequesttList = new ArrayList()); // parse List to string
//AccessRequest accessrequest = null;
AccessRequest accessRequest = new AccessRequest());
try
{
System.out.println("Try statement begins AccessRequestDAO");
PreparedStatement accrqststmt = super.getConnection().prepareStatement(AccRqstSqlStmt);
ResultSet resultSet = accrqststmt.executeQuery();
while (resultSet.next())
{
// Creating an instant of job follows
accessRequest = new Accessrequest();
accessRequest.setJOB_NAME(resultSet.getString("job_name"));
accessRequest.setRequest_ts(resultSet.getTime("request_ts"));
accessRequestList.add(accessRequest);
Iterator iterator = accessRequestList.iterator();
while (iterator.hasNext())
{
accessRequest = (Accessrequest) iterator.next();
}
}
return (accessRequestList);
My JSP look like below:
<%
List jobList = request.getParameter("acccessrequests"); // parse List to String
Iterator iterator = jobList.iterator();
while (iterator.hasNext())
{
accessRequest = (AccessRequest) iterator.next());
%>
<tr>
<td><input type="checkbox" name="<%accessRequest.getApproval_ind(); %>"></td>
开发者_StackOverflow中文版 <td><input type="text" id="jobname' name="accessRequests" value="job_name"></td>
HttpServletRequest#getParameter()
returns a String
, not a List
. So the compiler is right.
I am not sure how you have ever set the List
as a request parameter, there's no such method like HttpServletRequest#setParameter()
. So you're probably misinterpreting something. The normal approach is to set the list as request attribute by HttpServletRequest#setAttribute()
and access it in JSP by EL (expression language) like as ${attributeName}
. You also normally iterate over the list using JSTL <c:forEach>
tag.
Assuming that you've set the list in the request scope using a Servlet like follows...
request.setAttribute("list", list);
...here's a kickoff example how to iterate over the list:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
<c:forEach items="${list}" var="item">
<tr>
<td>${item.property1}</td>
<td>${item.property2}</td>
<td>${item.property3}</td>
</tr>
</c:forEach>
</table>
Alhamdulillah, thanks God!
This help me a lot. I try to build my own java Web framework. Before reading this QA, I don't know how to access an object (say, row of table) from a JSP. I just redirect it, and leave the database code in JSP, generated by DreamWeaver. Now I know how to do it. For example, this is a BiroController, which display data from Biro Table :
public void index() throws IOException, ServletException {
List list=new ArrayList();
list.add(BiroModel.create("1", "SDM"));
list.add(BiroModel.create("2", "Keuangan"));
request.setAttribute("list", list);
super.index();
}
firstly, I populate an array (subsequently, this will come from database table). and then set request attribute, then call superclass index method :
public void index() throws IOException, ServletException {
RequestDispatcher rd = request.getRequestDispatcher(viewPage);
if(rd!=null){
rd.forward(request, response);
}else{
PrintWriter out = response.getWriter();
out.print("can not dispatch to " + viewPage);
}
//OLD Code : response.sendRedirect(ServletUtil.getBaseUrl(request) + viewPage)
}
And, I did as you instructed in the JSP :
<c:forEach items="${list}" var="item">
<tr>
<td>${item.idbiro}</td>
<td>${item.biro}</td>
</tr>
</c:forEach>
I use Netbeans, so I can easily pick JSTL library from the list of available library
It works charmingly.. :) tq
精彩评论