dwr学习札记1(2.22)

dwr学习笔记1(2.22)
1.关于XML的配置

为了将网页和后台的类关联起来,首先需要被配好xml文件。
先在web-inf/lib下创建一个dwr.xml文件,
<dwr>
  <allow>
     <!--js中调用的类-->
     <create creator="new" javascript="People" scope="script">
  	<param name="class" value="dwrServer.People"/>
     </create>
     <!--调用类中要使用的类-->
     <convert match="dataPojo.Person" converter="bean"/>
  </allow>
</dwr>


具体的还有很多,但是初期都不太会用到,详情查询 http://directwebremoting.org/dwr/documentation/server/configuration/dwrxml/index.html(虽然是英文的,慢慢看吧,一个星期左右就和看中文差不多了的)
配置好并且将调用类写好以后,别忘了进入http://localhost:8080/[工程名]/dwr把相关的js文件链接加入到所写的网页里

2.回调函数的使用
dwr是对ajax技术的简化和规范,所以回调函数的使用会更加简便快捷。
function fillTable(){
  People.getSmallCrowd(12,function(people){
                          alert(people.name)
                          }
  );
}

People是在后台已经写好的类,并在dwr.xml中配置好了的,getSmallCrowd()是已经写好的方法,12是传入得参数,function(people)则是当服务器端返回参数后页面前台所要执行的函数,people则是getSmallCrowd()中返回的参数。
详情:http://directwebremoting.org/dwr/introduction/scripting-dwr.html

3.工具函数dwr.util.*的使用
getValue() & setValue()
var obj = dwr.util.getValue([id])
//则可以获得id所对应的对象,并赋给obj
dwr.util.setValue([id],[value]);
//则可将[value]的值赋给[id]所对应的对象

getValues() & setValues()
这两个一开始还没有看懂,后来才发现是两个非常好的工具函数
var person = {id:12, name:SwineX, address:CSU, age:20,superhero:No};
dwr.util.setValues(person);

var person = { id:-1 , name:null, address:null , age:null , superhero:null};
dwr.util.getValues(person);


对于:setValues()那么前台会在页面中搜寻ID与person中属性名相符的对象并将person中每个属性的值赋给网页中对应的对象,getValues()也是同样的道理,那么页面则要这样写:

<table class="plain">
	<tr>
		<td>Name:</td>
		<td><input id="name" type="text" size="30"></td>
	</tr>
	<tr>
		<td>Address:</td>
		<td><input id="address" type="text" size="40"/></td>
	</tr>
	<tr>
		<td>Age:</td>
		<td><input id="age" type="text" size="20"/></td>
	</tr>
	<tr>
		<td>Superhero?:</td>
		<td><input id="superhero" type="checkbox"         size="20"/></td>
	</tr>
</table>

注意:属性名和ID必须相同