??xml version="1.0" encoding="utf-8" standalone="yes"?>
line[j]是要复制的一?Action中可以获取到要复制的行的ID.
因ؓline[j]中有很多属?要是一个一个的属性去get,然后set的话,代码量会
很大,而且会出现很多冗余代码?br>q是我要复制出来的一?br>if (j == rowId && !line[j].getNewRecord()) {
rowList.add(line[j]);
//这一行全部复?br> }
现在要得其中的某几个属性复制出来ؓI?br>则需要一个一个的set,get.
if (j == rowId && !line[j].getNewRecord()) {
CreateDeliveryLineRow cdlr = new CreateDeliveryLineRow ();
if(line[j].getMfgLot() != null){
cdlr.setMfgLot = null;
}
。。。。。?br> rowList.add(cdlr);
//这一行全部复?br> }
以下是比较好的解x?
利用apache的commoncM的BeanUtils来实现对象属性的复制
if (j == rowId && !line[j].getNewRecord()) {
CreateDeliveryLineRow row = new CreateDeliveryLineRow();
BeanUtils.copyProperties(row,line[j]); //复制出对象line[j],其属性赋予row
row.setQuantity(null); //在row中轻杄实现Ҏ几个属性的控制
row.setMfgLot(null);
row.setMiniQuantity(null);
row.setBoxQuantity(null);
rowList.add(row);
//rowList.add(cdr);
}
===================================================
CreateDeliveryForm getForm = (CreateDeliveryForm) form;
。。。。。?br>CreateDeliveryLineRow[] line = getForm.getLine();
if (line != null && line instanceof CreateDeliveryLineRow[]) {
int size = line.length;
for (int j = 0; j < size; j++) {
if (!line[j].getNewRecord() && !line[j+1].getNewRecord()) {
if (line[j].getBoxQuantity() == 0L) {
line[j].setBoxQuantity(null);
}
if (line[j].getMiniQuantity() == 0L) {
line[j].setMiniQuantity(null);
}
if (line[j].getQuantity() == 0D) {
line[j].setQuantity(null);
}
rowList.add(line[j]);
}
if (j == rowId && !line[j].getNewRecord()) {
CreateDeliveryLineRow row = new CreateDeliveryLineRow();
BeanUtils.copyProperties(row,line[j]);
row.setQuantity(null);
row.setMfgLot(null);
row.setMiniQuantity(null);
row.setBoxQuantity(null);
rowList.add(row);
//rowList.add(cdr);
}
}
}
<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
业务逻辑?br /> 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功能.目l决定用Dwr.
刚开始研IDwr.觉得真的很不? 只需很少量的代码,p在现有的工程上集成ajax技? 期待Dwr有更辉煌的明天!
喜欢Dwr技术的IT界朋友可以与本h联系Q望提出好的意见与徏?
-vmargs
command line argument, which must follow all other Eclipse specific arguments. Thus, to increase the available heap memory, you would typically use:
eclipse -vmargs -Xmx<memory size>
with the <memory size>
value set to greater than "256M" (256 megabytes -- the default).
When using a Sun VM, you may also need to increase the size of the permanent generation memory. The default maximum is 64 megabytes, but more may be needed depending on your plug-in configuration and use. The maximum permanent generation size is increased using the -XX:MaxPermSize=<memory size> argument:
This argument may not be available for all VM versions and platforms; consult your VM documentation for more details.
eclipse -vmargs -XX:MaxPermSize=<memory size>
Note that setting memory sizes to be larger than the amount of available physical memory on your machine will cause Java to "thrash" as it copies objects back and forth to virtual memory, which will severely degrade your performance.
在eclipse安装根目录下Q用此命? eclipse.exe -vmargs -Xms256M -Xmx512M
如果说我q两q在Sun公司作了哪些对中国开发h员有益的事的话,我想Java API文中文版毫无疑问的应该第一个。我非常清楚仍然有众多开发h员坚持认Z个好的程序员应该完全参考英文版的文,但是我坚信该文的中文版有其存在的意义,因ؓ Java作ؓ一U程序设计语aQ我们希望能够有更多的开发h员——而不仅仅是那些能够熟l阅读英语的清华北大毕业生——来使用它,掌握它,_N它?/font>
也可以这么说QJava语言的前途,更多取决于草根,而不是精英?/font>