DWR - Direct Web Remoting 实际使用
2007-09-07 16:20
330 查看
用在一个很简单的地方,就是检查是否有重复的名称。
首先写好一个类
在web.xml中加上
在WEB-INF/dwr.xml中
这是配一个类,对应于js中哪一个类,基本上象一个bean管理器。
在jsp中
先引入js
前两个是必须的。CommonServices.js会动态生成,根据dwr.xml。
写一些方法:
checkRepeat:传入一个文本框的对象名,取出内容,调用后台CommonServices,根据返回值显示结果。
showResult:回调函数,处理后台CommonServices的返回结果。不知道为什么要用这种方式,干脆直接返回值不是挺好?还需要用回调?
getElement:自定义的实用工具函数
定义一下按钮:
这样点一个按钮之后就会弹出对话框,显示是否重复的信息。
调用的大致流程是这样的:
首先写好一个类
public class CommonServices { public boolean checkRepeatUser(String loginname) { boolean result=false; ...省去具体代码 return result; } }
在web.xml中加上
<servlet> <servlet-name>dwr-invoker</servlet-name> <display-name>DWR Servlet</display-name> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
在WEB-INF/dwr.xml中
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd"> <dwr> <allow> <create creator="new" javascript="CommonServices"> <param name="class" value="com.test.CommonServices"/> </create> </allow> </dwr>
这是配一个类,对应于js中哪一个类,基本上象一个bean管理器。
在jsp中
先引入js
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/CommonServices.js'></script> <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script> <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>
前两个是必须的。CommonServices.js会动态生成,根据dwr.xml。
写一些方法:
function showResult(b) { if(b) { alert("已经有重复的登录名了!"); } else { alert("登录名尚未注册过!"); } } function checkRepeat(s) { var o=getElement(s); if(o) { CommonServices.checkRepeatUser(o.value,showResult); } } function getElement(name) { var result=null; var objs=document.getElementsByName(name); if(objs) { var o=objs[0]; result=o; } return result; }
checkRepeat:传入一个文本框的对象名,取出内容,调用后台CommonServices,根据返回值显示结果。
showResult:回调函数,处理后台CommonServices的返回结果。不知道为什么要用这种方式,干脆直接返回值不是挺好?还需要用回调?
getElement:自定义的实用工具函数
定义一下按钮:
<input type="button" onclick="checkRepeat('user.loginname')" value="检查是否重复">
这样点一个按钮之后就会弹出对话框,显示是否重复的信息。
调用的大致流程是这样的:
button->checkRepeat->CommonServices.js->engine.js->dwr servlet->CommonServices.java->DB
相关文章推荐
- DWR - Direct Web Remoting 实际使用
- 面向 Java 开发人员的 Ajax: 使用 Jetty 和 Direct Web Remoting 编写可扩展的 Comet 应用程序
- 面向Java开发人员的Ajax: 结合Direct Web Remoting使用Ajax
- 结合Direct Web Remoting使用AJAX
- org.directwebremoting.servlet.DwrServlet
- 结合Direct Web Remoting使用AJAX
- 面向Java开发人员的Ajax:结合Direct Web Remoting使用Ajax
- 面向 Java 开发人员的 Ajax: 结合 Direct Web Remoting 使用 Ajax
- 面向 Java 开发人员的 Ajax: 结合 Direct Web Remoting 使用 Ajax----数据序列化不可能比这更简单了!
- 面向 Java 开发人员的 Ajax: 结合 Direct Web Remoting 使用 Ajax
- 面向 Java 开发人员的 Ajax: 结合 Direct Web Remoting 使用 Ajax
- DWR(Direct Web Remoting)原理和实例
- Failed to read schema document 'http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd',
- 面向 Java 开发人员的 Ajax: 结合 Direct Web Remoting 使用 Ajax
- 结合Direct Web Remoting使用AJAX
- 面向 Java 开发人员的 Ajax: 使用 Jetty 和 Direct Web Remoting 编写可扩展的 Comet 应用程序
- 结合Direct Web Remoting使用AJAX
- 面向 Java 开发人员的 Ajax: 使用 Jetty 和 Direct Web Remoting 编写可扩展的 Comet 应用程序
- java.lang.ClassNotFoundException: org.directwebremoting.servlet.DwrServlet
- 面向 Java 开发人员的 Ajax: 使用 Jetty 和 Direct Web Remoting 编写可扩展的 Comet 应用程序