版本信息:
Spring3.1.2
Dwr3.0.M1
web.xml配置信息
<!-- spring mvc -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
spring-mvc-servlet.xml中配置信息
<dwr:configuration>
<dwr:convert type="bean" class="org.mvn.dwr.model.User"></dwr:convert>
</dwr:configuration>
<dwr:url-mapping/>
<dwr:controller id="dwrController" debug="true"/>
<bean class="org.mvn.dwr.dao.impl.UserDaoImpl" id="userDaoImpl">
</bean>
<bean class="org.mvn.dwr.service.impl.UserServiceImpl" id="userServiceImpl">
<property name="userDao" ref="userDaoImpl"></property>
<dwr:remote javascript="dwrUserService">
<dwr:include method="checkUsernameIsExists"/>
<dwr:include method="getUsername"/>
<dwr:include method="findUserById"/>
<dwr:include method="listAll"/>
</dwr:remote>
</bean>
User.java
package org.mvn.dwr.model;
public class User {
private long id;
private String username;
private String birthday;
private String address;
public User() {
}
public User(long id, String username, String birthday, String address) {
super();
this.id = id;
this.username = username;
this.birthday = birthday;
this.address = address;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
UserDao.java
package org.mvn.dwr.dao;
import java.util.List;
import org.mvn.dwr.model.User;
public interface UserDao {
boolean checkUsernameIsExists(String username);
String getUsername(long id);
User findUserById(long id);
List<User> listAll();
}
UserDaoImpl.java
package org.mvn.dwr.dao.impl;
import java.util.ArrayList;
import java.util.List;
import org.mvn.dwr.dao.UserDao;
import org.mvn.dwr.model.User;
public class UserDaoImpl implements UserDao{
@Override
public boolean checkUsernameIsExists(String username) {
return true;
}
@Override
public String getUsername(long id) {
return "dwrservice";
}
@Override
public User findUserById(long id) {
return new User(1,"admin","1987-02-03","陝西西安");
}
public List<User> listAll(){
List<User> lists=new ArrayList<User>();
for (int i = 1; i <= 100; i++) {
lists.add(new User(i,"admin"+i,"1987-02-03","陝西西安"+i));
}
return lists;
}
}
UserService.java
package org.mvn.dwr.service;
import java.util.List;
import org.mvn.dwr.model.User;
public interface UserService {
boolean checkUsernameIsExists(String username);
String getUsername(long id);
User findUserById(long id);
List<User> listAll();
}
UserServiceImpl.java
package org.mvn.dwr.service.impl;
import java.util.List;
import org.mvn.dwr.dao.UserDao;
import org.mvn.dwr.model.User;
import org.mvn.dwr.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public boolean checkUsernameIsExists(String username) {
return userDao.checkUsernameIsExists(username);
}
@Override
public String getUsername(long id) {
return userDao.getUsername(id);
}
@Override
public User findUserById(long id) {
return userDao.findUserById(id);
}
@Override
public List<User> listAll() {
return userDao.listAll();
}
}
在spring-mvc-servlet.xml中增加
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
<dwr:configuration>
<dwr:convert type="bean" class="org.mvn.dwr.model.User"></dwr:convert>
</dwr:configuration>
<dwr:url-mapping/>
<dwr:controller id="dwrController" debug="true"/>
<bean class="org.mvn.dwr.dao.impl.UserDaoImpl" id="userDaoImpl">
</bean>
<bean class="org.mvn.dwr.service.impl.UserServiceImpl" id="userServiceImpl">
<property name="userDao" ref="userDaoImpl"></property>
<dwr:remote javascript="dwrUserService">
<dwr:include method="checkUsernameIsExists"/>
<dwr:include method="getUsername"/>
<dwr:include method="findUserById"/>
<dwr:include method="listAll"/>
</dwr:remote>
</bean>
<dwr:configuration>
<dwr:convert type="bean" class="org.mvn.dwr.model.User"></dwr:convert>
</dwr:configuration>
表示把User進行轉換
<dwr:url-mapping/>
<dwr:controller id="dwrController" debug="true"/>
兩個不能少
<dwr:remote javascript="dwrUserService">
<dwr:include method="checkUsernameIsExists"/>
<dwr:include method="getUsername"/>
<dwr:include method="findUserById"/>
<dwr:include method="listAll"/>
</dwr:remote>
dwrUserService爲指定dwr生成js文件的名稱
dwr:include表示那些方法需要開啓遠程調用
最後別忘了配置
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true"></property>
<property name="mappings">
<props>
<prop key="/dwr/*">dwrController</prop>
<prop key="/interface/**">dwrController</prop>
</props>
</property>
</bean>
最後在瀏覽器訪問http://localhost:8080/webapp/dwr/
這裏有一個奇怪的問題
就是上面的dao和service用註解注入的時候會報千奇百怪的異常信息。也不知道是什麼原因,暫且不去管了,在整個整合過程中換了無數個dwr的jar包。可以說dwr的版本真的很坑爹。官方3.0系列至今沒有發行正式版的版本,所以難免會出現千奇百怪的問題,有知道的朋友,可以告訴我一聲,先謝過了。。。。