dwr與spring相結合

來源:互聯網
上載者:User
 AJAX,或者說是非同步JavaScript和XML,
描述了一種使用混合了HTML(或XHTML)和層疊樣式表作為表達資訊,
來建立互動Web應用的開發技術;文件物件模型(DOM),
JavaScript,動態地顯示和與表達資訊進行互動;並且XMLHttpRequest對象與Web伺服器非同步地交換和處理資料。
DWR(直接Web遠端控制)項目是在Apache許可下的一個開源的解決方案,
它供給那些想要以一種簡單的方式使用AJAX和 XMLHttpRequest的開發人員。
它具有一套Javascript功能集,它們把從HTML頁面調用應用伺服器上的Java對象的方法簡化了。
它操控不同類型的參數,並同時保持了HTML代碼的可讀性,它可以與spring結合在一起。
舉個非常實用的小例子,檢測是否有重複的使用者名稱
1。web。xml的配置(包括spring和dwr)
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<web-app>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/main-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
          
        <servlet-name>dwr</servlet-name>
          
        <display-name>DWR Servlet</display-name>
          
        <description>Direct Web Remoter Servlet</description>
          
        <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</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>
</web-app>
2。dwr。xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
    <allow>
      
         <create
          creator="spring"
          javascript="UserManager">
          <param name="beanName" value="UserManager"/>
        </create>
      </allow>

</dwr>
3。applicationContext.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>/WEB-INF/jdbc.properties</value>
        </property>
    </bean>

    <!-- Local DataSource that works in any environment -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>${jdbc.driverClassName}</value>
        </property>
        <property name="url">
            <value>${jdbc.url}</value>
        </property>
        <property name="username">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
    </bean>
     <!--OrderManager-->
    <bean id="UserManager" class="dwr.manager.UserManager">
        <property name="dataSource">
            <ref local="dataSource"/>
        </property>
    </bean>
   
    </beans>
4。。spring的bean
package dwr.manager;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

 

public class UserManager extends JdbcDaoSupport {
          private final Log log = LogFactory.getLog(getClass().getName());
public boolean getUser(String username)
{
    String sql="select count(*) from user where userName=? ";

   int i=getJdbcTemplate().queryForInt(sql,new Object[]{username});
    if(i>0)
    return true;
        else
     return false;

}
}
5。jsp的代碼
<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head><title>Simple jsp page</title></head>

<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/UserManager.js'>
</script>

<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'>
</script>
<script type='text/javascript'
        src='<%=request.getContextPath()%>/dwr/util.js'></script>

 

<script  type="text/javascript">
    function showResult(b) {
        if (b) {
            alert("已經有重複的登入名稱了!");
        } else {
            alert("登入名稱尚未註冊過!");
        }
    }function checkRepeat(s) {
        var o = getElement(s);
        if (o) {
            UserManager.getUser(o.value, showResult);
        }
    }
    function getElement(name) {
        var result = null;
        var objs = document.getElementsByName(name);
        if (objs) {
            var o = objs[0];
            result = o;
        }
        return result;
    }
 
</script>

<body>
<input type="text" name="user" >
<input type="button" onclick="checkRepeat('user')" value="檢查是否重複">

</body>
</html>
6 classpath加入dwr和spring的jar檔案,注意必須有hibernate3.jar和bsf.jar,在IE和FireFox下,就可以用了。
其中參考了magicgod's blog(http://www.matrix.org.cn/blog/magicgod/archives/week_2005_09_04.html#001419)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.