SpringMVC整合DWR

jopen 12年前

版本信息:

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系列至今没有发行正式版的版本,所以难免会出现千奇百怪的问题,有知道的朋友,可以告诉我一声,先谢过了。。。。

来自:http://blog.csdn.net/sxyx2008/article/details/8000381