views:

20

answers:

1

Hi every one, in order to separate java code and html code and be more faithful to MVC framework i am coding like that;

in the servlet i put 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" );  


             for(Iterator it=query.iterate();it.hasNext();)
             {                                                                           
                               if(it.hasNext()){

                                   Object[] row = (Object[]) it.next();
                                   request.setAttribute("items", row);
                               }}
                               } catch (HibernateException e){ 


                            e.printStackTrace();
                            }

request.getRequestDispatcher("EspaceValidation.jsp").forward(request, response);

and in jsp i start like that:

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

in this case what should i put exactly to obtain my result.a table fulled with the right value from the request

+1  A: 

Since you're using Hibernate but not taking benefit of all its capabilities, I suggest to get yourself through a basic Hibernate tutorial first. The Tutorial as described in Hibernate Reference is good to get started.

Regardless, you need to create a model object which represents one entity of the Opcemployees table (I would rather rename it to Opcemployee, but that's another story) and also another one which represents one entity of the Dailytimesheet table.

public class Opcemployee {
    private String nom;
    private String prenom;
    private Dailytimesheet dailytimesheet;

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    // Add/generate remnant of constructors, getters and setters.
}

public class Dailytimesheet {
    private Date trackingDate;
    private String activity; // Not sure if this is supposed to be String.
    private String projectCode;
    // ...

    // Add/generate constructors, getters and setters.
}

Those objects should have private properties and public getters and setters. Most IDE's like Netbeans, Eclipse and IntelliJ can autogenerate them for you. There exist even the HibernateTools plugin which can do this all almost automatically with a few clicks based on an existing database model.

If everything is properly done and configured, you should end up with querying the data in the Servlet like this:

List<Opcemployee> opcemployees = session.createQuery("from Opcemployees").list();

Now you've a List of model objects which is useable in JSP. You need to let the Servlet store this list in the request scope and forward the request to the JSP.

request.setAttribute("opcemployees", opcemployees);
request.getRequestDispatcher("page.jsp").forward(request, response);

This way it's accessible by ${opcemployees} in page.jsp.

<p>All employees: ${opcemployees}</p>

But this is not nicely formatted. It's the outcome of List#toString(). As if you're doing a System.out.println(opcemployees) in Servlet. You need to iterate over it using JSTL c:forEach tag (just drop jstl-1.2.jar in /WEB-INF/lib to install it):

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:forEach items="${opcemployees}" var="opcemployee">
    <p>${opcemployee}</p>
</c:forEach>

This does the same as the following in normal Java code:

for (Opcemployee opcemployee : opcemployees) {
    System.out.println(opcemployee);
}

This is getting somewhat better. Every item is printed separately. But we want to have every property of an item in a separate table cell. So we need to write a HTML table around it and print a new <tr> on every iteration and access all properties separately inside a <td>.

<table>
    <c:forEach items="${opcemployees}" var="opcemployee">
        <tr>
            <td>${opcemployee.nom}</td>
            <td>${opcemployee.prenom}</td>
            <td>${opcemployee.dailytimesheet.trackingDate}</td>
            <td>${opcemployee.dailytimesheet.activity}</td>
            <td>${opcemployee.dailytimesheet.projectCode}</td>
        </tr>
    </c:forEach>
</table>

The ${opcemployee.nom} basically does a System.out.println(opcemployee.getNom()). As you see, you can chain getter methods in EL. The ${opcemployee.dailytimesheet.projectcode} will print the result of opcemployee.getDailytimesheet().getProjectCode().

Hope this helps. Also see this JSP/Servlet tutorial. The same site also ships with excellent kickoff Hibernate tutorials.

BalusC
ok BalusC it is great now thank you very much for help.I get it now.:)
kawtousse
You're welcome.
BalusC