配置方法:
1 官网 dwr.jar包
2 web.xml
<!-- dwr --> <listener> <listener-class>org.directwebremoting.servlet.DwrListener</listener-class> </listener> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <description></description> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet>
3 dwr.xml
A 和spring 整合
<dwr> <allow> <create creator="spring" javascript="dwrService" > <param name="beanName" value="dwrServiceImpl" ></param> </create> <create creator="spring" javascript="basicDwrService" > <param name="beanName" value="basicDwrServiceImpl" ></param> </create> <convert match="com.ass.common.generated.model.TUser" converter="bean" /> </allow> </dwr>
B 没有和spring整合
<dwr> <allow> <create creator="new" javascript="testClass" > <param name="class" value="/com.dwr.TestClass" /> <include method="testMethod1"/> </create> </allow> </dwr>
4 jsp页面引用
<script type='text/javascript' src="${root }dwr/engine.js"></script> <script type='text/javascript' src="${root }dwr/util.js"></script> <script type='text/javascript' src="${root }dwr/interface/dwrService.js"></script>
***********************上面配置网上一大堆,重点是下面使用范例*****************************************************************************
1 jsp页面写法 和 2 后台写法
1:
<script type="text/javascript"> function testStr(){ console.info("begin test str"); dwrService.testStr("wangt",function(data){console.info(data)}); } function testMap(){ //*******Map参数写法1 begin var data = { u : 'username', p : 'password' } //*******Map参数写法1 end //*******Map参数写法2 begin var d = new Object(); d.name = 'aa'; d.sdf='ss'; //*******Map参数写法2 end console.info(d); console.info(data); dwrService.testMap(d, function(data){ console.info(data); console.info(data.ok); }) } function testshuzu(){ var a = new Array(); a[0] = "1"; a[1] = "2"; a[2] = "3"; dwrService.testShuzu1(a,function(d){//return String[] console.info(d); console.info(d[0]); }) dwrService.testShuzu2(a,function(d){//return List<String> console.info(d); console.info(d[0]); }) } function testList(){ var lst = []; //var a = {name : "aname", password: 'paassword'}; //var b = {name : "bname", password: 'bpassword'}; var a = new Object(); var b = new Object(); a.name = "aname"; a.password = "apassword"; b.name = "bname"; b.password = "bpassword"; lst[0] = a; lst[1] = b; console.info(lst); dwrService.testList(lst, function(da){ console.info(da); console.info(da[0]); console.info(da[0].ok); }) } function testuser(){ var u = new Object(); u.name="uname"; dwrService.testUser(u,function(d){ console.info(d); console.info(d.name); }) } </script> </head> <body> <button onclick="testStr();" >testStr</button><br/> <button onclick="testMap();" >testMap</button><br/> <button onclick="testshuzu();" >testshuzu</button><br/> <button onclick="testList();" >testList</button><br/> <button onclick="testuser();" >testUser</button><br/>
2 java代码
@Override public String testStr(String str) { System.out.println(str); return "hello"+str; } /* * * @see com.ass.common.service.DwrService#testMap(java.util.Map) * @author wangt 2014年12月1日 上午10:51:08 */ @Override public Map<String, String> testMap(Map<String, String> m){ System.out.println(m); System.out.println(m.get("u")); System.out.println(m.get("p")); Map<String, String> m1 = new HashMap<String, String>(); m1.put("ok", "is ok"); return m1; } /* * * @see com.ass.common.service.DwrService#testShuzu1(java.lang.String[]) * @author wangt 2014年12月1日 上午10:51:08 */ @Override public String[] testShuzu1(String[] s){ System.out.println(s); System.out.println(s[1]); String[] c = {"aa","vv","cc"}; return c; } /* * * @see com.ass.common.service.DwrService#testShuzu2(java.util.List) * @author wangt 2014年12月1日 上午10:51:08 */ @Override public List<String> testShuzu2(List<String> l){ System.out.println(l); System.out.println(l.get(0)); List<String> ll = new ArrayList<String>(); ll.add("aa"); ll.add("bb"); return ll; } /* * * @see com.ass.common.service.DwrService#testList(java.util.List) * @author wangt 2014年12月1日 上午10:51:08 */ @Override public List<Map<String,String>> testList(List<Map<String, String>> l){ System.out.println(l.get(0)); Map<String, String> m1 = new HashMap<String, String>(); Map<String, String> m2 = new HashMap<String, String>(); m1.put("ok", "okoko"); m1.put("oooo", "oooo"); m2.put("ok", "okoko222"); m2.put("oooo", "oooo222"); List<Map<String, String>> ll = new ArrayList<Map<String, String>>(); ll.add(m1); ll.add(m2); return ll; } /* * * @see com.ass.common.service.DwrService#testUser(com.ass.common.generated.model.TUser) * @author wangt 2014年12月1日 上午10:51:08 */ @Override public TUser testUser(TUser m){ System.out.println(m.getName()); TUser c = new TUser(); c.setName("aaa"); return c; }
版权声明:本文为博主原创文章,未经博主允许不得转载。