Posted on 2008-08-18 17:04
beauty9235 阅读(350)
评论(0) 编辑 收藏
作者:
beauty9235
链接:
http://beauty9235.javaeye.com/blog/229619
发表时间: 2008年08月08日
声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
dwr开发总结 dwr+spring
参考资料
http://wiki.javascud.org/display/dwrcn/Spring+Integration
http://wiki.javascud.org/display/dwrcn/Getting+Started
测试http://{your app}//dwr/
第一步 web.xml
<!--***********************dwr setting***********************-->
<servlet>
<servlet-name>dwr-invoker</servlet-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>
<!--*********************** spring setting ***********************-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
第二步 dwr.xml
<!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="JDate">
<param name="class" value="java.util.Date"/>
</create>
<create creator="new" javascript="TestBean">
<param name="class" value="com.photo.test.TestBean"/>
</create>
<create creator="spring" javascript="userAction"> 这里交给spring进行创建
<param name="beanName" value="userAction"/>
</create>
<convert converter="bean" match="com.photo.dao.domain.User"/>返回类型需要反换的bean
</allow>
</dwr>
第三步 applicationContext.xml
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close" singleton="true">
....
</bean>
<bean id="userService" class="com.photo.service.UserService">
...
</bean>
<bean id="userAction" class="com.photo.action.UserAction"> 这里是spring创建bean的定义
<property name="userService" ref="userService"/> 将业务层注入
</bean>
第四步 类、网页
User.java 返回的实体
public class User {
Integer id;
String username;
String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserAction.java dwr框架调用
public class UserAction {
private UserService userService; //spring依赖注入,实现与持久层通信
public void setUserService(UserService userService) {
this.userService = userService;
}
public Map getUserList(String id){
Map map=new HashMap();
map.put("userList",userService.queryUser(new User())) ;//查询实体bean User
map.put("id",id);//用于显示的div id
return map ;
}
}
测试页面testajax.html
<html>
<head>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
<title>First Example</title>
<script type='text/javascript' src='/dwr/interface/userAction.js'></script>
<script type='text/javascript' src='/dwr/engine.js'></script>
<script type='text/javascript' src='/dwr/util.js'></script>
<script type="text/javascript">
var showUser = function(data) {
var id = data["id"];//取出页面显示的id
var userList = data["userList"];//取出页面显示的Userlist
if ($(id).style.display == "none") {
$(id).style.display = "block";
var htmlStr = ""; //alert(id);
for (var property in userList) {
var user = userList[property];
htmlStr += user.id + " " + user.username + " " + user.password + "<br>";
}
$(id).innerHTML = htmlStr;
return;
}
$(id).style.display = "none";
}
</script>
</head>
<body>
这是触发函数
<a style="cursor:hand" id="" onclick="userAction.getUserList('div_show',showUser);">show Users</a> <br>
这块区域用于显示
<div id="div_show" style="display:none"></div>
</body>
</html>
本文的讨论也很精彩,浏览讨论>>
JavaEye推荐