2007-1-20 周三
配置DWR:
1、Web.xml中添加如下内容:
xml 代码
- < 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 >
2、在web下新建dwr.xml配置文件
入门级的DWR例子
java 代码
- public class Apartment {
- private int id;
- private int bedrooms;
- private int bathrooms;
- private int price;
- private String address;
- private String city;
- private String province;
- public int getId() {
- return id;
- }
- public void setId( int id) {
- this .id = id;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this .address = address;
- }
- public int getBathrooms() {
- return bathrooms;
- }
- public void setBathrooms( int bathrooms) {
- this .bathrooms = bathrooms;
- }
- public int getBedrooms() {
- return bedrooms;
- }
- public void setBedrooms( int bedrooms) {
- this .bedrooms = bedrooms;
- }
- public String getCity() {
- return city;
- }
- public void setCity(String city) {
- this .city = city;
- }
- public int getPrice() {
- return price;
- }
- public void setPrice( int price) {
- this .price = price;
- }
- public String getProvince() {
- return province;
- }
- public void setProvince(String province) {
- this .province = province;
- }
- }
java 代码
- package ajax.dwr.simpledwr;
- import java.util.ArrayList;
- import java.util.List;
- public class ApartmentDAO {
- public List findApartments( int bedrooms, int bathrooms, int price) {
- List list = findList(bedrooms, bathrooms, price);
- return list;
- }
- public int countApartments( int bedrooms, int bathrooms, int price) {
- return findList(bedrooms, bathrooms, price).size();
- }
- private List findList( int bedrooms, int bathrooms, int price) {
- List list = initData();
- for ( int i = 0 ; i < list.size(); i++) {
- Apartment apartment = (Apartment) list.get(i);
- if (apartment.getBedrooms() < bedrooms
- && apartment.getBathrooms() < bathrooms
- && apartment.getPrice() >= price) {
- list.remove(i);
- }
- }
- return list;
- }
- private List initData() {
- List list = new ArrayList();
- Apartment apartment = new Apartment();
- apartment.setId( 16001 );
- apartment.setBedrooms( 1 );
- apartment.setBathrooms( 1 );
- apartment.setPrice( 850 );
- apartment.setAddress( "123 King St. East" );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16002 );
- apartment.setBedrooms( 2 );
- apartment.setBathrooms( 1 );
- apartment.setPrice( 1000 );
- apartment.setAddress( "1023 Yonge Ave" );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16003 );
- apartment.setBedrooms( 2 );
- apartment.setBathrooms( 2 );
- apartment.setPrice( 1050 );
- apartment.setAddress( "27 Winchester St" );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16004 );
- apartment.setBedrooms( 2 );
- apartment.setBathrooms( 2 );
- apartment.setPrice( 1350 );
- apartment.setAddress( "774 Yonge Ave" );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16005 );
- apartment.setBedrooms( 4 );
- apartment.setBathrooms( 3 );
- apartment.setPrice( 1550 );
- apartment.setAddress( "1054 Rouge Valley Dr." );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16006 );
- apartment.setBedrooms( 5 );
- apartment.setBathrooms( 1 );
- apartment.setPrice( 1850 );
- apartment.setAddress( "207 Queens Quay West" );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16007 );
- apartment.setBedrooms( 3 );
- apartment.setBathrooms( 1 );
- apartment.setPrice( 1850 );
- apartment.setAddress( "10 Yorkville Ave." );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16008 );
- apartment.setBedrooms( 4 );
- apartment.setBathrooms( 2 );
- apartment.setPrice( 1400 );
- apartment.setAddress( "3 Watline Dr" );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16009 );
- apartment.setBedrooms( 2 );
- apartment.setBathrooms( 1 );
- apartment.setPrice( 1180 );
- apartment.setAddress( "98 St. Joseph St." );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16010 );
- apartment.setBedrooms( 2 );
- apartment.setBathrooms( 2 );
- apartment.setPrice( 1050 );
- apartment.setAddress( "93 Lombard St." );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16011 );
- apartment.setBedrooms( 3 );
- apartment.setBathrooms( 3 );
- apartment.setPrice( 1150 );
- apartment.setAddress( "7201 St. Clair Ave. West" );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16012 );
- apartment.setBedrooms( 2 );
- apartment.setBathrooms( 1 );
- apartment.setPrice( 950 );
- apartment.setAddress( "12 Eglington Ave. E." );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16013 );
- apartment.setBedrooms( 2 );
- apartment.setBathrooms( 2 );
- apartment.setPrice( 1100 );
- apartment.setAddress( "352 Dufferin St" );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16014 );
- apartment.setBedrooms( 2 );
- apartment.setBathrooms( 2 );
- apartment.setPrice( 1350 );
- apartment.setAddress( "2224 Wellington St. W" );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16015 );
- apartment.setBedrooms( 4 );
- apartment.setBathrooms( 3 );
- apartment.setPrice( 1550 );
- apartment.setAddress( "1454 College St." );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16016 );
- apartment.setBedrooms( 5 );
- apartment.setBathrooms( 1 );
- apartment.setPrice( 1850 );
- apartment.setAddress( "256 Peter St." );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16017 );
- apartment.setBedrooms( 3 );
- apartment.setBathrooms( 1 );
- apartment.setPrice( 1850 );
- apartment.setAddress( "6 The Esplanade" );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- apartment.setId( 16018 );
- apartment.setBedrooms( 4 );
- apartment.setBathrooms( 2 );
- apartment.setPrice( 1400 );
- apartment.setAddress( "9 Mercer St." );
- apartment.setCity( "Toronto" );
- apartment.setProvince( "ON" );
- list.add(apartment);
- return list;
- }
- }
dwr.xml文件的配置
xml 代码
- < dwr >
- < allow >
- < convert converter = "bean" match = "ajax.dwr.simpledwr.Apartment" /> //定义bean
- < create creator = "new" javascript = "ApartmentDAO" class = "ajax.dwr.simpledwr.ApartmentDAO" >
- < include method = "findApartments" />
- < include method = "countApartments" />
- create >
- allow >
- dwr >
js 代码
- "text/css" media= "screen" >
- @ import url( style.css );
- <script src=' dwr="" <="" span="">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);
- //DWR使用得巧妙之处 ,调用ApartmentDAO的countApartments方法在传入bedrooms, bathrooms, price三个参数的同时
- //将其返回的结果 赋值给loadTotal(data)方法
- }
- function updateResults() {
- DWRUtil.removeAllRows( "apartmentsbody" ); //删除apartmentsbody的所有子child
- 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>