開源俱樂部

          開源框架

          日歷

          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          公告

          解放思想,求實創(chuàng)新

          文章檔案(12)

          友情鏈接

          搜索

          •  

          最新評論

          Dwr---example

          Posted on 2007-01-21 23:35 HandSoft 閱讀(741) 評論(0)  編輯  收藏 所屬分類: 開源學(xué)習(xí) 、Ajax技術(shù)相關(guān)

          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
          ???數(shù)據(jù)庫鏈接類
          ?? 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
          ?? 實現(xiàn)了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
          ?? 業(yè)務(wù)邏輯層
          ?? 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>

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

          主站蜘蛛池模板: 汉沽区| 大余县| 远安县| 济宁市| 连平县| 孟津县| 兴城市| 曲靖市| 永春县| 安平县| 台前县| 拜泉县| 平原县| 邻水| 富平县| 霍州市| 鹿邑县| 宜章县| 五指山市| 镇远县| 元氏县| 晋城| 海原县| 平谷区| 台江县| 崇仁县| 辽阳市| 凯里市| 永福县| 申扎县| 象州县| 广德县| 萨迦县| 栖霞市| 外汇| 南乐县| 乌鲁木齐市| 望谟县| 葫芦岛市| 山东| 福州市|