如何从数据库中检索数据并使用Struts和Hibernate将其显示在jsp上 [英] How to retrieve the data from the database and display it on a jsp using Struts and Hibernate

查看:165
本文介绍了如何从数据库中检索数据并使用Struts和Hibernate将其显示在jsp上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Struts的新手。我想在JSP页面上向用户显示表中的所有细节。



以下是我的代码:

  public class ListeActeurAction extends Action { 

public ActionForward execute(ActionMapping映射,ActionForm表单,
HttpServletRequest req,HttpServletResponse res)抛出Exception {
System.out.println(Action);

ListeActeur ListeActeur =(ListeActeur)表单;
String query =从Acteur中选择nomActeur;
ListeActeur.setLis(HibernateUtil.ListeActeur(query,req));
req.setAttribute(ListeActeur,ListeActeur.getLis());
返回mapping.findForward(s);

方法:HibernateUtil.ListeActeur(query,req)

public static List< Acteur> ListeActeur(字符串查询,HttpServletRequest req){

System.out.print(hutil);
Session session = HibernateUtil.getSessionFactory()。getCurrentSession();
session.beginTransaction();
Iterator results = session.createSQLQuery(query).list()。iterator();
列表< Acteur> list = new ArrayList< Acteur>(); ((results.hasNext()))
{
Acteur gg = new Acteur();


while((results.hasNext()))
// 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();
返回列表;

表单: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 = ();}
public ListeActeur()
{super();}

代码显示一个空白页面。

任何人都可以帮忙吗?




有我的jsp代码

 < html:form action =Liste> <表> 
< logic:iterate name =ListeActeurproperty =lisid =Acteur>
< td>< b> Nom Acteur:< bean:write name =Acteurproperty =nomActeur/>< / b> <峰; br>< / TD>
< td>< b> Adresse IP:< bean:write name =Acteurproperty =adresseIp/>< / b> < / B个< / TD>
< / tr>

我不明白我做错了什么,请帮忙。谢谢!!

解决方案

jsp表单中的字段将由Action表单类表示。你需要配置这个。所以当你提交jsp表单时,动作表单对象被创建。

使用该Actionform对象查询数据库并将数据保存在您选择的集合中。

访问jsp页面中的这个集合来显示数据。

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!!

解决方案

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.

这篇关于如何从数据库中检索数据并使用Struts和Hibernate将其显示在jsp上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆