1.下载dwr.jar,导入lib
2.web.xml
<!-- 配置DWR -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<!-- 是否开启调试模式,开发模式true,正式模式false -->
<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>
3.dwr.xml(与web.xml平行)
<?xml version="1.0" encoding="UTF-8"?>
<dwr>
<allow>
<create creator="new" javascript="Name" >
<param name="class" value="com.oracle.action.UserAction" />
<include method="nameSearch"/>
</create>
</allow>
</dwr>
4.部署,启动服务器,地址栏输入
http://localhost:8080/当前项目名称/dwr/index.html
.点击进入
url:http://localhost:8080/auction/dwr/test/**
注意:在web.xml文件中将/ 暂时设置为.action或.do,否则进不到下一页面,
复制当前
<script type='text/javascript' src='/auction/dwr/engine.js'></script>
<script type='text/javascript' src='/auction/dwr/interface/Name.js'></script>
<script type='text/javascript' src='/auction/dwr/util.js'></script>
至相应jsp页面
仅供参考:
<script type="text/javascript">
$(document).ready(
function (){
$("#uname").keyup(
function (){
var uname=$("#uname").val();
//alert(uname);
Name.nameSearch(uname,callback);
}
);
function callback(msg){
//alert(msg);
//msg 用于接收服务器传回的值,在本例中为“用户名已存在!”
$("#result").text(msg);//在页面id值为result的div中显示
}
}
);
</script>
-------------------------------------------------------------------
6.action 中暴露给其方法为
/**
ajax
*/
public String nameSearch(String uname){
String message=null;
Session session=HibernateUtil.getSession();
Transaction ta=session.beginTransaction();
Query query=session.createQuery("from User u where u.uname=:name");
query.setParameter("name", uname);
User user=(User)query.uniqueResult();
ta.commit();
session.close();
if(null==user){
message="用户名可用!";
}else{
message="用户名已存在,换个注册吧!";
}
return message;
}
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123