SpringMVC整合DWR – JAVA編程語言程序開發技術文章

版本信息:
Spring3.1.2
Dwr3.0.M1
web.xml配置信息
[html
<!– 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中配置信息
[html] 
<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
[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
[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
[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
[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
[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
[html]
<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表示那些方法需要開啟遠程調用
最後別忘瞭配置
[html] 
<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> 

 這裡有一個奇怪的問題
就是上面的dao和service用註解註入的時候會報千奇百怪的異常信息。也不知道是什麼原因,暫且不去管瞭,在整個整合過程中換瞭無數個dwr的jar包。可以說dwr的版本真的很坑爹。官方3.0系列至今沒有發行正式版的版本,所以難免會出現千奇百怪的問題,有知道的朋友,可以告訴我一聲,先謝過瞭。。。。

發佈留言