免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1655 | 回复: 0
打印 上一主题 下一主题

Dwr在Struts2中的运用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-29 14:25 |只看该作者 |倒序浏览
首先编写了一个要调用的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;ideptInfo.length;i++)
                {
                    //获取传递过来的数据
                    var id = deptInfo.articlesId; //文章id
                    var title = deptInfo.articlesTitle;//文章标题
                    //var arttype = deptInfo.email;//文章内容
                    //var tel = deptInfo.tel;//文章类别
                    //var address = deptInfo.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 = "+id+"&pageNow=1'>"+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的工作原理参照我的一篇转载的文章。

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/65552/showart_1808763.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP