首先编写了一个要调用的Dao的函数:
public List Indexarticles(int classid){/*首页添加类别的时候通过dwr进行的调用2009-1-26*/ List artList = new ArrayList(); try { String sql = "select * from articles where artclassid="+classid+" order by artid desc limit 9"; ResultSet rs = getStatement().executeQuery(sql); while (rs.next()) { articlesVO articles = new articlesVO(); articles.setarticlesId(rs.getInt("artid")); articles.setarticlesTitle(rs.getString("arttitle")); //articles.setarticlesContent(rs.getString("artcontent"));
articles.setartTime((rs.getDate("arttime"))); // articles.setartHits(rs.getInt("arthit"));
//articles.setclassName(rs.getString("className"));
//articles.setartAuthor(rs.getString("artauthor"));
artList.add(articles); } rs.close(); getStatement().close(); getConnection().close(); } catch (Exception e) { e.printStackTrace(); } return artList; }
|
返回一个List的数组。
接下编写一个dwr的接口的类
public class DwrService { artticleDao indexDao=new artticleDao(); @SuppressWarnings("unchecked") public List IndexArt(int classid) { return indexDao.Indexarticles(classid); } }
|
然后更改配置文件dwr.xml
<convert match="office.articlesVO" converter="bean"> <param name="include" value="artAuthor,artHits,articlesContent,articlesId,articlesTitle,artTimes,arttype,classid,className"></param> </convert> <create javascript="office" creator="new"> <param name="class" value="office.articles.service.DwrService"/> <include method="IndexArt"/> </create>
|
JSP文件中的JS调用如下
<script type='text/javascript' src='/office2/dwr/interface/office.js'></script> <script type='text/javascript' src='/office2/dwr/engine.js'></script> <script type='text/javascript' src='/office2/dwr/util.js'></script> <script type="text/javascript" src="main.js"></script> <script type="text/javascript"> var classid; office.IndexArt('10',hallnpsot);//默认调用就业的
function goClass(classid) { if(classid>0) office.IndexArt(classid,hallnpsot); } function hallnpsot(infoList) { var deptInfo = infoList; var table = document.getElementById("tableInfo"); //清楚表中的数据
while(table.rows.length>0) { table.deleteRow(0); } //给表格添加头部
var newTh = table.insertRow(-1); // newTh.style.backgroundColor="#3D93F1";
//newTh.align="center";
//表头TD
var newTh1 = newTh.insertCell(-1); var newTh2 = newTh.insertCell(-1); newTh1.style.width="10"; newTh2.style.width="300"; for(var i=0;i<deptInfo.length;i++) { //获取传递过来的数据
var id = deptInfo[i].articlesId; //文章id
var title = deptInfo[i].articlesTitle;//文章标题
//var arttype = deptInfo[i].email;//文章内容
//var tel = deptInfo[i].tel;//文章类别
//var address = deptInfo[i].address;//文章作者
//添加一行
var newTr = table.insertRow(-1); newTr.style.backgroundColor="#FFFFFF"; newTr.align="left"; newTr.className = "style3"; //添加5列
var newTd1 = newTr.insertCell(-1); var newTd2 = newTr.insertCell(-1); newTd1.innerHTML = ""+i+""; newTd2.innerHTML = ""+title+""; } } </script>
|
页面中的HTML代码的调用,二者在同一个JSP的文件中
<div id="classnew2"><h3><a href="javascript:void(0)" onClick="goClass('10') ">就业</a>| <a href="javascript:void(0)" onClick="goClass('5') ">学习</a>| <a href="javascript:void(0)" onClick="goClass('6') ">通知</a>| <a href="javascript:void(0)" onClick="goClass('9') ">娱乐</a>| <a href="javascript:void(0)" onClick="goClass('8') ">IT资讯</a> </h3> <div class="style1" id="message" align="center"></div> <table id="tableInfo" align="left" width="100%" border="0" cellpadding="1" cellspacing="1" bordercolor="#CCCCCC" > </table> </div>
|
至于dwr的工作原理参照我的一篇转载的文章。
阅读(4689) | 评论(1) | 转发(0) |