<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    開源俱樂部

    開源框架
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Dwr---example

    Posted on 2007-01-21 23:35 HandSoft 閱讀(735) 評論(0)  編輯  收藏 所屬分類: 開源學習Ajax技術相關

    1. 在web.xml文件中注冊dwr
    ?? <servlet>
    ??? <servlet-name>dwr-invoker</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-invoker</servlet-name>
    ??? <url-pattern>/dwr/*</url-pattern>
    ?</servlet-mapping>

    ?<welcome-file-list>
    ??? <welcome-file>search.jsp</welcome-file>
    ?</welcome-file-list>

    2.dwr.xml
    ?<dwr>
    ??? <allow><convert convert="bean"? match="dwr.sample.Apartment"/>
    ??? <create>
    ?????????? <creator="new" javascript="ApartmentDAO" class="dwr.sample.ApartmentDAO">
    ????????????????? <include method="findApartments"/>
    ????????????????? <include method="countApartments"/>
    ?????????? </creator>
    ??? </create>
    ??? </allow>
    </dwr>
    3.DB
    CREATE TABLE APARTMENTS (id INTEGER, bedrooms INTEGER, bathrooms INTEGER, price INTEGER, address VARCHAR, city VARCHAR, province VARCHAR);
    INSERT INTO APARTMENTS VALUES (16001, 1, 1, 850, '123 King St. East', 'Toronto', 'ON');
    INSERT INTO APARTMENTS VALUES (16002, 2, 1, 1000, '1023 Yonge Ave.', 'Toronto', 'ON');
    INSERT INTO APARTMENTS VALUES (16003, 2, 2, 1050, '27 Winchester St.', 'Toronto', 'ON');
    4.Apertment.java
    普通的javabean
    5.DBUtils.java
    ???數據庫鏈接類
    ?? public class DBUtils {

    ?/*
    ? * Creates the sample data (table and records).
    ? */
    ?public static void setupDatabase(BufferedReader reader) {
    ??Connection c = null;
    ??Statement stmt = null;
    ??try {
    ???c = openConnection();
    ???stmt = c.createStatement();
    ???// reads the file with the SQL statements
    ???String line;
    ???while ((line = reader.readLine()) != null) {
    ????stmt.execute(line);
    ???}
    ???stmt.close();
    ???c.close();
    ??} catch (IOException e) {
    ???e.printStackTrace();
    ??} catch (SQLException e) {
    ???e.printStackTrace();
    ??} finally {
    ???try {
    ????stmt.close();
    ????c.close();
    ???} catch (SQLException e) {
    ????e.printStackTrace();
    ???}
    ??}
    ?}
    ?
    ?/*
    ? * Opens a database connection.
    ? */
    ?public static Connection openConnection() throws SQLException {
    ??Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:dwr-sample", "sa", "");
    ??return c;
    ?}
    ?
    }

    6.ContextListener.java
    ?? 實現了ServletContextListerer接口的類
    ?? public class ContextListener implements javax.servlet.ServletContextListener {

    ?/**
    ? * This method is invoked when the Web Application has been removed and is
    ? * no longer able to accept requests.
    ? * @param event
    ? */
    ?public void contextDestroyed(ServletContextEvent event) {
    ?}

    ?/**
    ? * This method is invoked when the Web Application is ready to service requests.
    ? * @param event
    ? */
    ?public void contextInitialized(ServletContextEvent event) {
    ??try {
    ???// load the driver
    ???Class.forName("org.hsqldb.jdbcDriver");
    ???// create the table and add sample data
    ???InputStreamReader in = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("db.sql"));
    ???BufferedReader reader = new BufferedReader(in);
    ???DBUtils.setupDatabase(reader);
    ??} catch (ClassNotFoundException e) {
    ???e.printStackTrace();
    ??}
    ??
    ?}

    }
    7.ApartmentDAO.java
    ?? 業務邏輯層
    ?? public class ApartmentDAO {
    ?
    ?/**
    ? * Returns the available apartments based on the search criteria.
    ? * @param bedrooms minimum number of bedrooms
    ? * @param bathrooms minimum number of bathrooms
    ? * @param price maximum price to be paid
    ? * @return
    ? */
    ?public Collection findApartments(int bedrooms, int bathrooms, int price) {
    ??Collection list = new Vector();
    ??String sql = "select * from APARTMENTS" +
    ????createSearchWhereClause(bedrooms, bathrooms, price) +
    ????"order by bedrooms, bathrooms, price";

    ??// define db variables
    ??Connection c = null;
    ??Statement stmt = null;
    ??try {
    ???c = DBUtils.openConnection();
    ???stmt = c.createStatement();
    ???// just run the sql statement
    ???ResultSet rs = stmt.executeQuery(sql);
    ???while(rs.next()) {
    ????Apartment apartment = this.getApartment(rs);
    ????list.add(apartment);
    ???}
    ??} catch (SQLException e) {
    ???e.printStackTrace();
    ??} finally {
    ???try {
    ????stmt.close();
    ????c.close();
    ???} catch (SQLException e) {
    ????e.printStackTrace();
    ???}
    ??}

    ??return list;
    ?}
    ?
    ?/**
    ? * Returns the number of available apartments based on the search criteria.
    ? * @param bedrooms minimum number of bedrooms
    ? * @param bathrooms minimum number of bathrooms
    ? * @param price maximum price to be paid
    ? * @return
    ? */
    ?public int countApartments(int bedrooms, int bathrooms, int price) {
    ??String sql = "select count(*) as total from APARTMENTS" + createSearchWhereClause(bedrooms, bathrooms, price);
    ??int numberApartments = -1;
    ??// define db variables
    ??Connection c = null;
    ??Statement stmt = null;
    ??try {
    ???c = DBUtils.openConnection();
    ???stmt = c.createStatement();
    ???// just run the sql statement
    ???ResultSet rs = stmt.executeQuery(sql);
    ???if (rs.next()) {
    ????numberApartments = rs.getInt("total");
    ???}
    ??} catch (SQLException e) {
    ???e.printStackTrace();
    ??} finally {
    ???try {
    ????stmt.close();
    ????c.close();
    ???} catch (SQLException e) {
    ????e.printStackTrace();
    ???}
    ??}
    ??
    ??return numberApartments;
    ?}
    ?
    ?/**
    ? * Creates a Unit object from the database.
    ? * @param rs
    ? * @return
    ? * @throws SQLException
    ? */
    ?private Apartment getApartment(ResultSet rs) throws SQLException {
    ??Apartment ap = new Apartment();
    ??ap.setId(rs.getInt("id"));
    ??ap.setAddress(rs.getString("address"));
    ??ap.setBedrooms(rs.getInt("bedrooms"));
    ??ap.setBathrooms(rs.getInt("bathrooms"));
    ??ap.setPrice(rs.getInt("price"));
    ??ap.setCity(rs.getString("city"));
    ??ap.setProvince(rs.getString("province"));
    ??return ap;
    ?}
    ?
    ?
    ?/**
    ? * Creates the where clause for the search SQL statement.
    ? * @param bedrooms
    ? * @param bathrooms
    ? * @param price
    ? * @return
    ? */
    ?private String createSearchWhereClause(int bedrooms, int bathrooms, int price) {
    ??String where = " where bedrooms >= " + bedrooms +
    ????" and bathrooms >= " + bathrooms +
    ????" and price < " + price;
    ??return where;
    ?}

    }
    8. search.jsp
    ? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    ? <title>DWR Example</title>

    ?? <style type="text/css" media="screen">
    ?????? @import url( style.css );
    ?? </style>?
    ?
    ? <script src='dwr/interface/ApartmentDAO.js'></script>
    ? <script src='dwr/engine.js'></script>
    ? <script src='dwr/util.js'></script>
    ? <script>
    ?
    ? function updateTotal() {
    ??? $("resultTable").style.display = 'none';
    ??? var bedrooms = document.getElementById("bedrooms").value;
    ??? var bathrooms = document.getElementById("bathrooms").value;
    ??? var price = document.getElementById("price").value;
    ??? ApartmentDAO.countApartments(loadTotal, bedrooms, bathrooms, price);
    ? }

    ? function updateResults() {
    ??? DWRUtil.removeAllRows("apartmentsbody");
    ??? var bedrooms = document.getElementById("bedrooms").value;
    ??? var bathrooms = document.getElementById("bathrooms").value;
    ??? var price = document.getElementById("price").value;
    ??? ApartmentDAO.findApartments(fillTable, bedrooms, bathrooms, price);
    ??? $("resultTable").style.display = '';
    ? }
    ?
    ? var getId = function(unit) { return unit.id };
    ? var getAddress = function(unit) { return unit.address };
    ? var getBedrooms = function(unit) { return unit.bedrooms };
    ? var getBathrooms = function(unit) { return unit.bathrooms };
    ? var getPrice = function(unit) { return unit.price };
    ???
    ? function loadTotal(data) {
    ??? document.getElementById("totalRecords").innerHTML = data;
    ? }
    ?
    ? function fillTable(apartment) {
    ??? DWRUtil.addRows("apartmentsbody", apartment, [ getId, getAddress, getBedrooms, getBathrooms, getPrice ]);
    ? }
    ?
    </script>

    </head>

    <body onload="updateTotal();">

    <h2>Find an apartment to rent</h2>

    <table border="0">
    <form name="rentalForm">
    ? <tr width="400">
    ?? <td width="100">City</td>
    ?? <td width="300">Toronto</td>
    ? </tr>
    ? <tr>
    ?? <td>Beds</td>
    ?? <td>
    ??? <select id="bedrooms" onchange="updateTotal()">
    ???? <option value="1">1 or more</option>
    ???? <option value="2">2 or more</option>
    ???? <option value="3">3 or more</option>
    ???? <option value="4">4 or more</option>
    ??? </select>
    ?? </td>
    ? </tr>

    ? <tr>
    ?? <td>Baths</td>
    ?? <td>
    ??? <select id="bathrooms" onchange="updateTotal()">
    ???? <option value="1">1 or more</option>
    ???? <option value="2">2 or more</option>
    ???? <option value="3">3 or more</option>
    ???? <option value="4">4 or more</option>
    ??? </select>
    ?? </td>
    ? </tr>

    ? <tr>
    ?? <td>Price</td>
    ?? <td>
    ??? <select id="price" onchange="updateTotal()">
    ???? <option value="800">under $800</option>
    ???? <option value="1000">under $1,000</option>
    ???? <option value="1250">under $1,250</option>
    ???? <option value="1500" selected="selected">under $1,500</option>
    ???? <option value="1800">under $1,800</option>
    ???? <option value="2000">under $2,000</option>
    ??? </select>
    ?? </td>
    ? </tr>

    ? <tr>
    ?? <td colspan="2">
    ??? <blockquote>
    ???? Available apartments: <span id="totalRecords" style="font-weight:bold;"></span>
    ??? </blockquote>
    ?? </td>
    ? </tr>

    </form>
    </table>

    <p><input type="button" value="Show results!" onClick="updateResults();"></p>

    <div id="resultTable">

    <h2>Results</h2>

    ?<table border="1">
    ? <thead>
    ??? <tr>
    ????? <th width="40">Id</th>
    ????? <th width="180">Address</th>
    ????? <th width="60">Beds</th>
    ????? <th width="60">Baths</th>
    ????? <th width="60">Price</th>
    ??? </tr>
    ? </thead>
    ? <tbody id="apartmentsbody">

    ? </tbody>
    ?</table>
    </div>

    </body>
    </html>

    由于項目需要,需在我們現有的struts、hibernate工程上集成ajax功能.項目組決定使用Dwr.
    剛開始研究Dwr.覺得真的很不錯, 只需很少量的代碼,就能在現有的工程上集成ajax技術. 期待Dwr有更輝煌的明天!
    喜歡Dwr技術的IT界朋友可以與本人聯系!望提出好的意見與建議.

    主站蜘蛛池模板: 国产亚洲玖玖玖在线观看| 亚洲今日精彩视频| 亚洲av永久无码精品网址| 97国产免费全部免费观看| 亚洲精品亚洲人成在线麻豆| 久久国产乱子伦精品免费一| 亚洲va在线va天堂va四虎| 99热这里只有精品免费播放| 亚洲视频在线视频| 69视频免费观看l| 亚洲色图.com| 99视频在线精品免费观看6| 亚洲精品无码专区在线播放| 成人性生免费视频| 高潮毛片无遮挡高清免费 | 成熟女人牲交片免费观看视频| 亚洲国产成人久久77| 18禁无遮挡无码网站免费| 亚洲欧洲日产国码久在线| 久久亚洲私人国产精品vA| 亚洲免费网站在线观看| 亚洲人成人无码网www国产| 人人公开免费超级碰碰碰视频| 亚洲综合色区在线观看| 野花香高清视频在线观看免费 | 成全在线观看免费观看大全 | 亚洲av鲁丝一区二区三区| 99久久免费观看| 亚洲欧美日韩一区二区三区 | 热久久这里是精品6免费观看| 亚洲av无码成h人动漫无遮挡 | 亚洲性线免费观看视频成熟| 亚洲欧洲无码AV不卡在线| 中文字幕精品亚洲无线码一区应用| 免费视频一区二区| 亚洲1区2区3区精华液| 亚洲av无码专区国产乱码在线观看 | 91麻豆精品国产自产在线观看亚洲 | 亚洲三级在线视频| yy6080久久亚洲精品| 毛片无码免费无码播放|