您的位置:首页 > 编程语言 > Java开发

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

2017-04-04 22:26 260 查看
看到网上好多有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" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: