home.java

来自「Enjoy Web Dev With Tapestry 一书的源代码」· Java 代码 · 共 53 行

JAVA
53
字号
package com.ttdev.postage;

import java.util.*;

import org.apache.hivemind.util.*;
import org.apache.tapestry.*;
import org.apache.tapestry.annotations.*;
import org.apache.tapestry.html.*;
import org.apache.tapestry.valid.*;

public abstract class Home extends BasePage {
	private KnownPatrons knownPatrons;
	
	@InjectPage("Result")
	public abstract IPage getResult();
	public abstract double getWeight();
	public abstract String getPatronCode();

	@Bean
	public abstract ValidationDelegate getDelegate();

	public Home() {
		knownPatrons = new KnownPatrons();
	}
	public KnownPatrons getKnownPatrons() {
		return knownPatrons;
	}
	public abstract Date getShippingDate();
	public abstract String getDesc();

	public IPage onSubmit() {
		System.out.println(getShippingDate());
		System.out.println(getDesc());
		ValidationDelegate delegate = getDelegate();
		double weight = getWeight();
		Integer discount = knownPatrons.getDiscount(getPatronCode());
		if (getPatronCode() != null && getPatronCode().equals("p1") && weight > 50) {
			delegate.setFormComponent(null);
			delegate.record("Can't ship 50kg or more for p1", ValidationConstraint.CONSISTENCY);
		}
		if (delegate.getHasErrors()) {
			return null;
		}
		int postagePerKg = 10;
		int postage = (int) (weight * postagePerKg);
		if (discount != null) {
			postage = postage * discount.intValue() / 100;
		}
		IPage resultPage = getResult();
		PropertyUtils.write(resultPage, "postage", new Integer(postage));
		return resultPage;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?