公司项目中有用到DWR这种方式,初次学习不免遇到一些问题。
一、先从简单的Hello World说起,参考[url]http://blog.csdn.net/caterpillar_here/archive/2006/09/06/1186566.aspx [/url]
web.xml代码如下
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
JAVA代码如下:
package com.zqw.dwr.test;
public class Hello {
public String hello(String name) {
return "哈楼!" + name + "!World 世界与DWR实验!";
}
}
dwr.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="Hello">
<param name="class" value="com.zqw.dwr.test.Hello" />
</create>
</allow>
</dwr>
index.jsp部分代码如下:
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/Hello.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>不要漏了引用。
<input id="user" type="text" />
<input type='button' value='哈楼' onclick="hello()" />
<div id="result"></div>
hello world的简单Demo也就完成了。
二、填充下拉框
XinxiServiceImpl的代码如下:
public List findDWRallxinxi() {
List ls= xinxiDao.findAllXinxi();//把所有的信息查询出来
List rtnList = new ArrayList();
Map optMapKong = new HashMap();
optMapKong.put("xinxisex", "-----请选择-----");
optMapKong.put("xinxisal", "");
optMapKong.put("xinxiid", "");
rtnList.add(optMapKong);
for (int i = 0; ls != null && i < ls.size(); i++) {
Xinxi jf = (Xinxi) ls.get(i);
Map optMap = new HashMap();
optMap.put("xinxisex", jf.getSex());
optMap.put("xinxisal", jf.getSal());
optMap.put("xinxiid", jf.getId());
rtnList.add(optMap);
}
return rtnList;
}
dwr.xml代码如下:
<create creator="spring" javascript="xinxiDWRallxinxi">
<param name="beanName" value="xinxiDWRallxinxi"/>
<include method="findDWRallxinxi"/>
</create>
当然在Spring配置文件applicationContext.xml下增加bean
<bean id="xinxiDWRallxinxi" class="com.zqw.service.impl.XinxiServiceImpl">
</bean>
index.jsp代码如下:
var $j = jQuery.noConflict();
$j(document).ready(function() {
getXinxiSelect();
});
function getXinxiSelect(){
xinxiDWRallxinxi.findDWRallxinxi(CallbackXinxi);
}
function CallbackXinxi(returnValue){
var xinxiSelectList = '<s:property value="Xinxivalue"/>';
DWRUtil.removeAllOptions("xinxiSelectList");
DWRUtil.addOptions("xinxiSelectList", returnValue, "xinxisex", "xinxisal");
DWRUtil.setValue("xinxiSelectList",xinxiSelectList);
}
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/xinxiDWRallxinxi.js"></script><!--不要漏了添加-->
<select name="xinxi.sal" id="xinxiSelectList" >
这样便把所有的sal全部加载到下拉框中