tags:

views:

27

answers:

1

hi everyone,

In my servlet I construct the query like the following:

    net.sf.hibernate.Session s = null;
    net.sf.hibernate.Transaction tx;
    try {
        s= HibernateUtil.currentSession();
        tx=s.beginTransaction();
        Query query = s.createQuery("select  opcemployees.Nom,opcemployees.Prenom,dailytimesheet.TrackingDate,dailytimesheet.Activity," +
                "dailytimesheet.ProjectCode,dailytimesheet.WAName,dailytimesheet.TaskCode," +
                "dailytimesheet.TimeSpent,dailytimesheet.PercentTaskComplete from  Opcemployees opcemployees,Dailytimesheet dailytimesheet  " +
                "where opcemployees.Matricule=dailytimesheet.Matricule  and dailytimesheet.Etat=3 " +
                "group by opcemployees.Nom,opcemployees.Prenom" );  

        List opdts= query.list();
        request.setAttribute("items", opdts);
        request.getRequestDispatcher("EspaceValidation.jsp").forward(request, response);
    } catch (HibernateException e){   e.printStackTrace();}

But in the jsp I can't display the result correctly. I do the following in JSP:

<table>
<c:forEach items="${items}" var="item">
    <tr>
        <td>${item}</td>
    </tr>
</c:forEach>
</table>

thanks for help.

A: 

You need to determine the object type of every list item. It should be an existing model object (a Javabean like class with private properties and public getters). The ${item} in JSP would only display its Object#toString() outcome, something like com.packagename.ClassName@hashcode. Or if the toString() method is overridden, then you would see a "human friendly" string. Other way to reveal the type would then be debugging the getClass() of the list item:

System.out.println(opdts.get(0).getClass().getName());

Once the type is known and the property names are known, then you can just access its properties in EL like ${item.someProperty}, ${item.otherProperty}, etcetera.

Update: since the list seems to contain Object[] as every item (thus the list signature is actually List<Object[]>), here's how you could display it in JSP/JSTL:

<table>
    <c:forEach items="${list}" var="objects">
        <tr>
            <c:forEach items="${objects}" var="object">
                <td>${object}</td>
            </c:forEach>
        </tr>
    </c:forEach>
</table>

This does basically the same as following in "raw" Java:

System.out.println("<table>");
for (Object[] objects : list) {
    System.out.println("<tr>");
    for (Object object : objects) {
        System.out.println("<td>" + object + "</td>");
    }
    System.out.println("</tr>");
}
System.out.println("</table>");
BalusC
you mean that i could create a bean that bind the 2 classes here such as opcemployees and dailytimesheet
kawtousse
since joining the two tables(objects) don't let to display different property from different objects
kawtousse
What exactly does the returned list contain?
BalusC
it contains column from first table called dailytimesheet and othersfrom the second table called opcemployees
kawtousse
so different columns(properties) from different tables(objects)
kawtousse
What object type exactly is it? What does `${item}` say? What result did you got in webpage? What does `getClass().getName()` say for every item? Do you understand my answer? Do you understand Java at any way? PS: you can edit comments within 5 minutes by clicking `edit` link at end of a comment.
BalusC
[Ljava.lang.Object;@1f09a31 [Ljava.lang.Object;@11b1d80 this is what returns: System.out.println(opdts.get(0).getClass().getName());
kawtousse
I know this is my two right records from the query
kawtousse
Ah OK, it's an `Object[]`.
BalusC
yes it is ok i knew this conversion to Object[] from the start but this is not really the problem since i have only one record displayed or the query result must be 2 records.I can't put the request dispatcherv in the loop and i can't have all the resultset.so this is what i am doing:
kawtousse
`` for(Iterator it =query.iterate();it.hasNext();){ if(it.hasNext()){Object[] obj = (Object[])it.next();System.out.println(obj.getClass().getName());request.setAttribute("items",obj );}}request.getRequestDispatcher("Validation.jsp").forward(request, response);`
kawtousse
No... Just put list in request scope. Do not overwrite the request attribute with a single item everytime. That said... I don't mean to be harsh, but do you in any way *understand* what you're doing? It look more and more that you're just shooting in the dark. I stongly, strongly recommend to stop with this and go get a decent tutorial/book and learn the stuff. Learn basic Java. Learn Hibernate. Learn JSP/Servlet. Then continue. Do not stab around in the dark on an existing project. It's only a waste of time. You've been busy with this since half March and you don't seem to make any progress.
BalusC