<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'MyJsp.jsp' starting page</title> <script type='text/javascript' src='<%=path %>/dwr/interface/userService.js'></script> <script type='text/javascript' src='<%=path %>/dwr/engine.js'></script> <script type='text/javascript' src='<%=path %>/dwr/util.js'></script> <script type="text/javascript"> function test() { userService.testDWR(function(data){ alert(data); }); } function testdwr() { //var data ={id:3,userName:"yonghuming",psw:"mima"}; var data = {}; data.id = 3; data.userName = 'chengrenyin'; data.psw = '123456'; userService.testDWR2(data,function(res){ alert(res.id+' '+res.psw); }); } function testdwrStr() { var name = DWRUtil.getValue("nameID"); userService.testDWR3(name,function(res){ alert(res); }); } </script> </head> <body onload="test()"> This is my JSP page. <br> <input type="button" value="button1" onclick="testdwr()"> <input type="button" value="button2" onclick="testdwrStr()"> <input type="hidden" value="xman" id="nameID"> </body> </html>
/** * UserServiceImpl.java * com.isoftstone.ssh.user.service * * Function: TODO * * ver date author * ────────────────────────────────── * May 16, 2011 程仁银 * * Copyright (c) 2011, All Rights Reserved. */ package com.zte.ace.user.service; import com.zte.ace.po.TbUser; import com.zte.ace.user.dao.UserDao; import com.zte.ace.vo.TestUser; /** * ClassName:UserServiceImpl * Project: * Company: isoftStone * * @author 程仁银 * @version * @since Ver 1.1 * @Date May 16, 2011 2:30:15 AM * @see */ public class UserServiceImpl implements UserService { private UserDao userDao; public void insertUser(String userName, String psw) { try { TbUser user = new TbUser(); user.setUserName(userName); user.setPwd(psw); this.userDao.insertUser(user); // test 事物 TbUser user2 = new TbUser(); user2.setUserName(userName+"2"); user2.setPwd(psw+"2"); this.userDao.insertUser(user2); // -----------------------------ERROR // TbUser user3 = new TbUser(); // user3.setUserName("超过四个字"); // this.userDao.insertUser(user3); } catch (RuntimeException e) { System.out.println("RuntimeException ......... "); e.printStackTrace(); } } public String testDWR(){ System.out.println("################# dwr ###############"); return "return params"; } public TestUser testDWR2(TestUser user){ System.out.println(user.getId()+" "+user.getUserName()+" "+user.getPsw()); TestUser tb = new TestUser(); tb.setId(8); tb.setUserName("888"); tb.setPsw("88888"); return tb; } public String testDWR3(String str){ System.out.println("testDWR3 = "+str); return str+"love"; } // UserDao IOC public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd"> <dwr:configuration> <dwr:convert type="bean" class="com.zte.ace.vo.TestUser"></dwr:convert> </dwr:configuration><!-- 必须要configuration --> <bean id="userService" class="com.zte.ace.user.service.UserServiceImpl"> <property name="userDao" ref="userDao"></property> <dwr:remote javascript="userService"></dwr:remote> </bean> 。。。。。。 。。。。。
<!-- 配置dwrSpring --> <servlet> <servlet-name>dwrSpring</servlet-name> <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwrSpring</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <!-- 加载DWR end -->
如果不交给spring管理,在WEB-INF同级别下面新建个dwr.xml,然后bean参数传递用 <dwr:convert type="bean" class="com.zte.ace.vo.TestUser"></dwr:convert> 有个子标签 <param ...即可,不用再配置dwr:configuration