dao层代码
packagecom.hanqi.dao;importjava.util.ArrayList;importjava.util.List;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.hibernate.boot.registry.StandardServiceRegistryBuilder;importorg.hibernate.cfg.Configuration;importorg.hibernate.service.ServiceRegistry;importcom.hanqi.utility.USer;public classUSerDAO
{private Configuration cfg = null;private ServiceRegistry sr = null;private SessionFactory sf = null;private Session se = null;
Transaction ts= null;publicUSerDAO()
{//初始化Hibernate
cfg = newConfiguration().configure() ;
sr= newStandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build() ;
}public voidinit()
{
sf=cfg.buildSessionFactory(sr) ;
se=sf.openSession() ;
ts=se.beginTransaction() ;
}public voiddestory()
{
ts.commit() ;
se.close() ;
sf.close() ;
}//查询列表
public List getAll(intpage)
{
List list = new ArrayList<>() ;
init() ;//设置每页两行及起始页
list = se.createQuery("from USer").setMaxResults(2)
.setFirstResult((page-1)*2).list() ;
destory();returnlist;
}
}
jsp页面
Insert title here Insert title herelist = (List)request.getAttribute("selectuser") ;for(USer us : list )
{
out.print(us+ " 【删除】"
+" 【修改】"
+"
") ;
}int pages = Integer.parseInt(request.getParameter("pages")) ;%>
response.setHeader("refresh", "3;url=selectUSer?pages=1") ;}else{ %>
Struts文件配置
/WEB-INF/pages/fail.jsp
/WEB-INF/pages/selectUSer.jsp
service层方法
publicString selectUSer()
{
String rtn= "fail";try{//得到原生request
HttpServletRequest hsr =ServletActionContext.getRequest() ;int page = Integer.parseInt(hsr.getParameter("pages")) ;//page = Integer.parseInt(hsr.getParameter("pagess")) ;//调用查询的方法
List list = newUserService().getAll(page);
hsr.setAttribute("pages", page+1);
hsr.setAttribute("selectuser", list);
rtn= "success";
}catch(Exception e)
{
e.printStackTrace();
}returnrtn;
}