-->

How to retrieve the data from the database and dis

2019-07-30 02:48发布

问题:

I am new to Struts. I want to display all the details from a table to the user on a JSP page.

Here is my code:

public class ListeActeurAction  extends Action{

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest req, HttpServletResponse res) throws Exception {
        System.out.println("Action");

        ListeActeur ListeActeur= (ListeActeur) form;
                String query = "select nomActeur from Acteur " ;
                ListeActeur.setLis( HibernateUtil.ListeActeur(query, req)); 
        req.setAttribute("ListeActeur", ListeActeur.getLis()) ;
                        return mapping.findForward("s");

methode:HibernateUtil.ListeActeur(query, req)

public   static List <Acteur> ListeActeur(String query,HttpServletRequest req){

    System.out.print("hutil");
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
     Iterator results = session.createSQLQuery(query).list().iterator();
     List <Acteur> list = new ArrayList<Acteur>();


     while((results.hasNext()))
     {
         Acteur gg =new Acteur();
        //Object[] row = (Object[]) results.next();
        //gg.setActeurId((Integer)row[0]);
        gg.setNomActeur(( java.lang.String)results.next());

    list.add(gg);
     }


    req.getSession(true).setAttribute("ListeActeur", list);
     session.getTransaction().commit();
     HibernateUtil.getSessionFactory().close(); 
     return list;
}

Form:listeActeur

public class ListeActeur extends ActionForm {
private List <Acteur> lis = new ArrayList<Acteur>();    
public List <Acteur> getLis(){System.out.println("gets");return lis;}
public void setLis(List <Acteur> lis){System.out.println("set");this.lis=lis;}
public ListeActeur()
{super () ;}

The code displays a blank page. Even the table does not display.

Can anyone help?


there is the code of my jsp

<html:form  action="Liste" >  <table>  
  <logic:iterate  name="ListeActeur" property= "lis"  id="Acteur" >
   <td><b>Nom Acteur:<bean:write name="Acteur" property="nomActeur"/></b> <br></td>
   <td><b>Adresse IP :<bean:write name="Acteur" property="adresseIp"/></b> </b>  </td>
  </tr>

I dont understand what am i doing wrong,Please help. Thanks!!

回答1:

Fields in your jsp form will be represented by the Action form class. You need to configure this. so when you submit the jsp form, the action form object gets created.

Use that Actionform object to query the database and save the data in a collection of your choice.

Access this collection in your jsp page to display the data.