刚学,只做了简单的分页显示,借鉴别人的,网址忘了,呵呵
数据库:
?? news表(id,newsTypeId,newsTitle,newsContent,newsDateTime)
?? newstype表(id,newstypeName)
NewsDAO.java //一页显示的新闻 public List findPageAll(final int start,final int limit){ return getHibernateTemplate().executeFind(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery("from News n left join fetch n.newstype"); q.setFirstResult(start*limit); q.setMaxResults(limit); return q.list(); } }); } //得到记录总条数 public int totalRecord(){ int count = 0; Long l = (Long)getHibernateTemplate().execute(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery("select count(n.id) from News n"); return q.uniqueResult(); } }); count = l.intValue(); return count; } NewsAction.java public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { int totalProperty = adminService.totalPropertyByNews(); int start; try { start = Integer.parseInt(request.getParameter("start")); } catch (NumberFormatException e1) { start = 0; } int limit; try { limit = Integer.parseInt(request.getParameter("limit")); } catch (NumberFormatException e1) { limit = 2 ; } if(start>0) start = start/limit; //Hibernate级联时,用json有错误,要把被级联的那个对象干掉,这样就没法显示被级联的对象了 // JsonConfig conf = new JsonConfig(); // conf.setExcludes(new String[]{"newstype"}); List list = adminService.findPageAllNews(start, limit); JSONArray jsonArray = new JSONArray(); Iterator ite = list.iterator(); //对list转变成的ite进行遍历,得到每个对象,并put到map里,再把map放到json数组里 while(ite.hasNext()){ News news = (News) ite.next(); Map map = new HashMap(); map.put("id", news.getId()); map.put("newstype", news.getNewstype().getNewstypeName()); map.put("newsTitle", news.getNewsTitle()); map.put("newsContent", news.getNewsContent()); String time = new SimpleDateFormat("yyyy-MM-dd").format(news.getNewsDateTime()); map.put("newsDateTime", time); jsonArray.add(map); } String jsonStr = jsonArray.toString(); //为了分页,要加上start,limit,totalProperty,下面的符合json数据的形式 String jsonString = "{start:"+start+",limit:"+limit+",totalProperty:"+totalProperty+",newsList:"+jsonStr+"}"; response.setContentType("text/html;charset=utf-8"); try { //输出来,到jsp页面调用 response.getWriter().write(jsonString); } catch (IOException e) { e.printStackTrace(); } return null;//返回null }
?
newsList.jsp <script type="text/javascript"> Ext.onReady(function(){ var sm = new Ext.grid.CheckboxSelectionModel(); var store = new Ext.data.Store({ proxy: new Ext.data.HttpProxy({ url:"news.do?method=list", method:"post" }), reader: new Ext.data.JsonReader({//读取json数据 root:'newsList', // totalProperty:'totalProperty', //总记录数 id:'id' },[{name:'id'}, {name:'newstype'}, {name:'newsTitle'}, {name:'newsContent'}, {name:'newsDateTime'} ]) }); //创建列 var column = new Ext.grid.ColumnModel([ sm, //复选框 {header:'编号',dataIndex:'id'}, {header:'类型',dataIndex:'newstype', renderer:function(value){ if(value=="国内新闻") return "<font color='red'>国内新闻</span>"; } }, {header:'标题',dataIndex:'newsTitle'}, {header:'内容',dataIndex:'newsContent'}, {header:'时间',dataIndex:'newsDateTime'} ]); column.defaultSortable = true;//默认可排序 //创建一个工具条 var tba = new Ext.Toolbar(); //面板 var grid = new Ext.grid.GridPanel({ el:'showNews', width:600, height:300, title:'新闻列表', store:store, cm:column, //创建的列 trackMouseOver:false, autoScroll: true, loadMask: {msg:'正在加载数据,请稍侯……'}, sm:sm, //下边 bbar:new Ext.PagingToolbar({ pageSize:2, store:store, displayInfo:true, displayMsg:'显示第 {0} 条到 {1} 条记录,一共 {2} 条', emptyMsg:'没有记录' }) }); grid.render(); store.load({params:{start:0,limit:2 }}); }); </script> <body> <div id="showNews" ></div> </body>
?