Frage

A warm hello to the brilliant folk of SO,

I am a newbie to JSP pages. I am required to work on them at my new assignment at work and I have been developing a sample project which simply uses JSP pages to store/retrieve data from the dB. I wrote the following code for the "retrieve" view and was advised to go through the internet for finding more effective ways of writing code on JSP pages. I did some read up, but I am a little confused and overwhelmed with the capabilities of the JSP.

Could you nice folk here help me understand JSP pages better by first helping me understand how to make my code effective, so I have a place to start? I am not sure what to apply where! So I am quite certain that once I know how my own code is supposed to look like, it will make it easier for me to apply myself as I will have something I built, to relate my understanding with.

My apologies in case this question is too noob-ish, I am relatively new to development.

Thanks in advance!

Code:

Note: "name" and "id" are "String" and "Integer" ArrayLists that have been sent from the Action class. (I'm using Struts-Hibernate integrated environment for developing the sample app.)

<%

if ((request.getAttribute("id")==null)||(request.getAttribute("name")==null))
{
    out.println("Please Enter the DOB");
}
else
{
    int size = (Integer)request.getAttribute("size"),i=0;
    String[] names = new String[size];
    Integer[] ids = new Integer[size];
    ArrayList<String> name = (ArrayList<String>)request.getAttribute("name");
    ArrayList<String> id = (ArrayList<String>)request.getAttribute("id");

    for (Object o:name)
    {
        names[i++]=(String)o;
    }

    i=0;

    for (Object o:id)
    {
        ids[i++]=(Integer)o;
    }

    for(i=0;i<size;i++)
    { 
%>

ID : <%=ids[i]%><br/>
Name : <%=names[i]%><br/>

<%}}%>
War es hilfreich?

Lösung

Well, I found the answer after speaking to an experienced colleague of mine. Posting it here for any other beginners who might be getting aboard my ship:

<table border="5" cellpadding="5" cellspacing="5">
<tr><td>EmpId</td><logic:iterate name="id" id="id">
    <td width="8"><bean:write name="id"/></td>
</logic:iterate></tr>

<tr><td>Name</td><logic:iterate name="name" id="name">
    <td width="8"><bean:write name="name"/></td>
</logic:iterate></tr>
</table>

The table tag is used for presentation. The key tag here is the logic:iteration tag. It helps you iterate through the list(s) you pass to the page. Two separate logic:iterate tags were created for each list.

In order to use the logic:iterate tag, you simply need to include the following piece of code at the top of the JSP page:

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top