Java/Velocity/Calculation — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:07, 1 июня 2010
Calculation Based on Model
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
public class ProductList {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/model.vm");
VelocityContext ctx = new VelocityContext();
Collection products = new ArrayList();
products.add(new Product("Product 1", 142.919));
products.add(new Product("Product 2", 133.199));
products.add(new Product("Product 3", 112.991));
ProductListModel model = new ProductListModel(products);
ctx.put("model", model);
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
import java.util.Collection;
import java.util.Iterator;
public class ProductListModel {
private Collection productList = null;
private double totalPrice = Double.MIN_VALUE;
public ProductListModel(Collection aProductList) {
productList = aProductList;
}
public Collection getProductList() {
return productList;
}
public void setProductList(Collection aProductList) {
productList = aProductList;
}
public double getTotalPrice() {
if (totalPrice == Double.MIN_VALUE) {
totalPrice = 0;
Iterator itr = productList.iterator();
while (itr.hasNext()) {
Product p = (Product) itr.next();
totalPrice += p.getPrice();
}
}
return totalPrice;
}
}
-------------------------------------------------------------------------------------
public class Product {
private String name;
private double price;
public Product(String aName, double aPrice) {
name = aName;
price = aPrice;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
-------------------------------------------------------------------------------------
#foreach($product in $model.productList)
$product.Name $$product.Price
#end
Total Price: $$model.totalPrice
Calculation In Java
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
public class ProductList {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/calculation.vm");
VelocityContext ctx = new VelocityContext();
Collection products = new ArrayList();
products.add(new Product("Product 1", 12.99));
products.add(new Product("Product 2", 13.99));
products.add(new Product("Product 3", 11.99));
ctx.put("productList", products);
// calculate total
Iterator itr = products.iterator();
double total = 0.00;
while (itr.hasNext()) {
Product p = (Product) itr.next();
total += p.getPrice();
}
ctx.put("totalPrice", new Double(total));
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
public class Product {
private String name;
private double price;
public Product(String aName, double aPrice) {
name = aName;
price = aPrice;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
-------------------------------------------------------------------------------------
#foreach($product in $productList)
$product.Name $$product.Price
#end
Total Price: $$totalPrice