标签:javascript servlet jsp dwr
1、自定义User对像
package com.mol.info;
package com.mol.info;public class User {public String name;public User(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
2、Java类代码:
package com.mol.info;public class UserDWR {public User say() {User user = new User("YangBang");return user;}}
3、dwr.xml配置文件。如果DWR返回的的是对象,那个这个配置文件中,就要添加JavaBean对象的相关信息,至于web.xml中servlet的配置就不用讲了。
<?xml version="1.0" encoding="UTF-8"?><dwr><allow><create creator="new" javascript="obj"><param name="class" value="com.mol.info.UserDWR" /></create><!-- 配置的JavaBean对象 --><convert converter="bean" match="com.mol.info.User"></convert></allow></dwr>
4、JS接收DWR返回的代码:
二、JS接收DWR返回的List,与Set对象(List或中,存入的是JavaBean对象)
<script type="text/javascript">
function test() {obj.say(cb);}function cb(data) {//在返回参数中,调用JavaBean的getName()方法,取得相应的值document.getElementById("in").innerHTML = data.getName();}</script>
1、Java代码:
2、dwr.xml配置文件代码:
public List<User> strList(){User user = new User("YangBang");User user1 = new User("WGR");ArrayList<User> list = new ArrayList<User>();list.add(user);list.add(user1);return list;}
<dwr><allow><create creator="new" javascript="list"><param name="class" value="com.fs.listdwr.ListDwr" /><include method="strList"/></create><convert converter="bean" match="com.fs.listdwr.User"><param name="include" value="name" /></convert></allow></dwr>
3、JS接收DWR返回的代码:
三、JS接收DWR返回的Map对象(Map中,存入的是JavaBean对象)
function test() {list.strList(cb);}
function cb(data) {for(var i=0;i<data.length;i++){var value = data[i].name;var values = values + value;document.getElementById("in").innerHTML = values;}}
<script type="text/javascript">function test() {map.myMapDwr(ab);}function ab(data) {var str = "";for ( var key in data) {alert(key + "," + data[key]);var value = data[key];str += value;}document.getElementById("in").innerHTML = str;}</script>
标签:javascript servlet jsp dwr
原文地址:http://blog.csdn.net/sndtb/article/details/45310123