如何将动作类列表发送到jsp页面?

时间:2014-07-08 09:56:37

标签: hibernate jsp struts2

我正在使用struts& amp; hibernate。在这种情况下,我想在动作类的jsp页面中显示结果。从动作类列表商店信息&该列表应该打印在jsp页面上。但我只有一个pojo类,我的动作类& hibernate的映射是在同一个类中完成的。

index.jsp - > MusicAction-> a.jsp

package action;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import com.opensymphony.xwork2.ActionSupport;
@Action(value="Music")
@javax.persistence.Entity
@Table(name="item")
@Results({
    @Result(name="success",location="a.jsp"),
    @Result(name="error",location="b.jsp")
 })
public class MusicAction extends ActionSupport 
{

@Id
private int item_no;
private String item_Name;
private String item_type;
private float item_price;

public int getItem_no() {
    return item_no;
}
public void setItem_no(int item_no) {
    this.item_no = item_no;
}
public String getItem_Name() {
    return item_Name;
}
public void setItem_Name(String item_Name) {
    this.item_Name = item_Name;
}
public String getItem_type() {
    return item_type;
}
public void setItem_type(String item_type) {
    this.item_type = item_type;
}
public float getItem_price() {
    return item_price;
}
public void setItem_price(float item_price) {
    this.item_price = item_price;
}




@Override
public String execute() throws Exception {
    List l =getItem();

    System.out.println("in execute"+l);
    return "success";

}


public List getItem()
{   
    System.out.println("yah i m in getItem");

    Session session=new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory().openSession();

    Transaction t = session.beginTransaction();


    MusicAction m = new MusicAction();

    //System.out.println("configured");

    Query query = session.createQuery("from MusicAction");

    java.util.List list = query.list();
    Iterator it=list.iterator();
    while(it.hasNext())
    {
        m=(MusicAction)it.next();

        System.out.println(m.getItem_no()+"| "+m.getItem_Name()+"\t|"+m.getItem_type()+"\t"+m.getItem_price());
    }
    t.commit();
    session.close();


    return list;
   }

   }

如何在a.jsp中访问该列表?

1 个答案:

答案 0 :(得分:1)

你被解释为确切的错误。所以,试试这个,

MusicAction.class

    public class MusicAction extends ActionSupport 
   {
    private List<Item> item=new ArrayList<Item>();//setter method.
    @Override
    public String execute() throws Exception {
      System.out.println("in execute"+item.size());
       return "success";
    }
    public List<Item> getItem(){ try{
            System.out.println("yah i m in getItem");
            Session session=new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory().openSession();
            Transaction t = session.beginTransaction();
            MusicAction m = new MusicAction();
            Query query = session.createQuery("from MusicAction");
            item = (List<Item>)query.list();
            t.commit();
            session.close();}catch(Exception e){}
         return item;}
    }

a.jsp

  <%@ taglib prefix="s" uri="/struts-tags" %>
  <s:iterator value="item"> 
      <s:property value="item_no" />
      <s:property value="item_Name" />
      <s:property value="item_type" />
      <s:property value="item_price" />
  </s:iterator>