How To Display The Html Table With All The Rows Returned From The Database In Jsp?
Solution 1:
Instead of sending one object from your server code send list of object which you want to display as list of rows.
I have not tested, you please take care of handling null check.
<table id="myTable" border="0" cellspacing="0" style="border-spacing:0; width:100%;border-collapse: collapse;">
<%
List<Object> object = (List<Object>)request.getAttribute("myContact");
for(int i=0;i<object.size();i++){
MyModel myModel = (MyModel)object.get(i);
String mail = myModel.getmail()!=null ? myModel.getmail().toString().trim() : "";
String title = myModel.gettitle()!=null ? myModel.gettitle().toString().trim() : "";
String name = myModel.getname()!=null ? myModel.getname().toString().trim() : "";
%>
<tr><tdclass="table-border-bottom"><labelfor="name">Name:</label></td><tdclass="table-border-bottom"><inputid="name"type="text"value='<%=name%>'name="name"class="required"style="height: 17px;"/></td><tdclass="table-border-bottom"><labelfor="contactTitle">Title:</label></td><tdclass="table-border-bottom"><inputid="title"type="text"value='<%=title%>'name="title"class="required"style="height: 17px;"/></td><tdclass="table-border-bottom"><labelfor="mail">Email:</label></td><tdclass="table-border-bottom"><inputid="mail"type="text"value='<%=mail%>'name="mail"class="required email"style="height: 17px; "/></td></tr>
<% } %>
<tralign="center"><tdvalign="bottom"colspan="6"style="height: 45px; "><inputtype="button"id="submit"name="submit"value="Save"style="width: 80px ; height:24px; text-align: center;border-radius: 10px 10px 10px 10px;"/><inputtype="button"id="revert"name="revert"value="Revert"style="width: 80px ; height:24px;text-align: center;border-radius: 10px 10px 10px 10px;"/></td></tr>
</table>
Also, it would be helpful to you by going with JSTL instead of doing with scriptlet.
For styling of rows, apply class like
CSS
.rowClass{
/* APPLY STYLE TO ROWS */
}
Solution 2:
If your MyModel
class has bean style getters like :
publicStringgetMail() {
returnthis.mail;
}
You should use EL like ${myContact.mail}
to retrieve the value of mail
attribute.
It is even better to use JSTL's <c:out value="${myContact.mail}">
tag to avoid cross-site scripting.
In case you want to display a List<MyModel>
, set it in the request
scope in the Servlet which forwards the request to your JSP .
request.setAttribute("myModelsList",myModelsListObject);
Then use , JSTL's <forEach>
loop to iterate over each element of the List
and display it .
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
<c:forEach items="${myModelsList}" var="myModel" varStatus="count">
<trid="${count.index}">
<td>${myModel.mail}</td>
<td>${myModel.title}</td>
<td>${myModel.name}</td>
</tr>
</c:forEach>
</table>
Read also :
Solution 3:
Instead, of one single Contact object you would now set a List<Contact>
instead as a request attribute.
List<Contact> myContacts = (List<Contact>) request.getAttribute("myContacts");
Then just use an Iterator
with a while
loop.
<table id="myTable" border="0" cellspacing="0" style="border-spacing:0; width:100%;border-collapse: collapse;">
<%
List<Contact> myContacts = (List<Contact>) request.getAttribute("myContacts");
Iterator<Contact> contacts = myContacts.iterator();
while (contacts.hasNext()) {
Contact myModel = contacts.next();
String mail = myModel.getmail()!=null ? myModel.getmail().toString().trim() : "";
String title = myModel.gettitle()!=null ? myModel.gettitle().toString().trim() : "";
String name = myModel.getname()!=null ? myModel.getname().toString().trim() : "";
%>
<tr><tdclass="table-border-bottom"><labelfor="name">Name:</label></td><tdclass="table-border-bottom"><inputid="name"type="text"value='<%=name%>'name="name"class="required"style="height: 17px;"/></td><tdclass="table-border-bottom"><labelfor="contactTitle">Title:</label></td><tdclass="table-border-bottom"><inputid="title"type="text"value='<%=title%>'name="title"class="required"style="height: 17px;"/></td><tdclass="table-border-bottom"><labelfor="mail">Email:</label></td><tdclass="table-border-bottom"><inputid="mail"type="text"value='<%=mail%>'name="mail"class="required email"style="height: 17px; "/></td></tr>
<% } // close loop
%>
<tralign="center"><tdvalign="bottom"colspan="6"style="height: 45px; "><inputtype="button"id="submit"name="submit"value="Save"style="width: 80px ; height:24px; text-align: center;border-radius: 10px 10px 10px 10px;"/><inputtype="button"id="revert"name="revert"value="Revert"style="width: 80px ; height:24px;text-align: center;border-radius: 10px 10px 10px 10px;"/></td></tr>
</table>
Post a Comment for "How To Display The Html Table With All The Rows Returned From The Database In Jsp?"