struts2中从后台读取数据到<s:select>

看到网上好多有struts2中从后台读取数据到<s:select>的,但都 不太详细,可能是我自己理解不了吧!所以我自己做了 一个,其中可能 有很多不好的地方,望广大网友指出

结果如图

public class Books implements java.io.Serializable {

    // Fields

    private String bookId;
    private String bookType;
    private String bookName;
    private String bookAuthor;
    private String bookPress;
    private Float bookPrice;
    private Integer bookNumber;
    private Set readers = new HashSet();
//省略相应的set和get方法
}


impl中

//查询所有的图书类型
    public List findBookType()
    {
        Session session = HibernateSessionFactory.getSession();
        Transaction tx = session.beginTransaction();
        String hql="from Books group by bookType";
        Query query = session.createQuery(hql);
        List list = query.list();
        return list;
    }

Action中

public String findBookType()
    {
        String result="error";
        List list = null;

        BooksDao booksDao =new BooksImpl();            
        list = booksDao.findBookType();
        if(list!=null)
        {
            ActionContext ctx = ActionContext.getContext();
            Map map = ctx.getSession();    
            map.put("bookType", list);
            //result="error";
            System.out.println("--->"+list.size());
        }            
        return result;
    }

jsp中

 <s:select label="类别" name="books.bookType" list="#session.bookType"
                            listKey="id" listValue="bookType" headerKey="-1" headerValue="请选择书的类别"
                            emptyOption="false" />

 

posted on 2016-04-30 19:43  丨丿丶  阅读(798)  评论(0编辑  收藏  举报