源碼以下:javascript
CreatingMixins1.javahtml
/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.pages;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import com.tapestry.app.entities.MyOrder;
public class CreatingMixins1 {
@SessionState
@Property
private MyOrder myOrder;
void setupRender() {
myOrder.setApplesQuantity(0);
myOrder.setOrangesQuantity(0);
myOrder.setBananasQuantity(0);
}
Object onSuccessFromPlainForm() {
orderOneApple();
return CreatingMixins2.class;
}
Object onOrderOneOrange() {
orderOneOrange();
return CreatingMixins2.class;
}
Object onActionFromOrderOneBanana() {
orderOneBanana();
return CreatingMixins2.class;
}
Object onSuccessFromMixinForm() {
orderOneApple();
return CreatingMixins2.class;
}
Object onActionFromOrderOneBananaWithMixin() {
orderOneBanana();
return CreatingMixins2.class;
}
void orderOneApple() {
sleep(1500); // Sleep 1.5 seconds to simulate busy system
myOrder.setApplesQuantity(myOrder.getApplesQuantity() + 1);
}
void orderOneOrange() {
sleep(1500); // Sleep 1.5 seconds to simulate busy system
myOrder.setOrangesQuantity(myOrder.getOrangesQuantity() + 1);
}
void orderOneBanana() {
sleep(1500); // Sleep 1.5 seconds to simulate busy system
myOrder.setBananasQuantity(myOrder.getBananasQuantity() + 1);
}
private void sleep(long duration) {
try {
Thread.sleep(duration);
}
catch (InterruptedException e) {
}
}
}
CreatingMixins1.tmljava
<html t:type="layout" title="tapestryStart Index" t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<form t:type="form" t:id="plainForm">
<div style="padding: 20px 50px;">
<input t:type="submit" value="Order 1 Apple"/>
<a t:type="eventlink" t:event="orderOneOrange" href="#">Order 1 Orange</a>
<a t:type="actionlink" t:id="orderOneBanana" href="#">Order 1 Banana</a>
</div>
</form>
<form t:type="form" t:id="mixinForm" t:clientValidation="blur">
<div style="padding: 20px 50px;">
<input t:type="submit" value="Order 1 Apple" t:mixins="clickonce"/>
<a t:type="eventlink" t:event="orderOneOrange" t:mixins="clickonce" href="#">Order 1 Orange</a>
<a t:type="actionlink" t:id="orderOneBananaWithMixin" t:mixins="clickonce" href="#">Order 1 Banana</a>
</div>
</form>
</html>
ClickOnce.jssql
var alreadyClickedOnce = false;
ClickOnce = Class.create( {
initialize: function(elementId) {
Event.observe($(elementId), 'click', this.doClickOnce.bindAsEventListener(this));
},
doClickOnce: function(e) {
if (alreadyClickedOnce) {
e.stop();
}
alreadyClickedOnce = true;
}
} )
Tapestry.Initializer.clickOnce = function(spec) {
new ClickOnce(spec.elementId);
}
ClickOnce.javaapache
/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.mixins;
import org.apache.tapestry5.ClientElement;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
@Import(library="context:assets/js/ClickOnce.js")
public class ClickOnce {
@Inject
private JavaScriptSupport javaScriptSupport;
@InjectContainer
private ClientElement clientElement;
public void afterRender() {
JSONObject spec = new JSONObject();
spec.put("elementId", clientElement.getClientId());
javaScriptSupport.addInitializerCall("clickOnce", spec);
}
}
CreatingMixins2.javajson
/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.pages;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import com.tapestry.app.entities.MyOrder;
public class CreatingMixins2 {
@SessionState
@Property
@SuppressWarnings("unused")
private MyOrder myOrder;
}
CreatingMixins2.tmlapp
<html t:type="layout" title="tapestryStart Index" t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<div style="padding: 20px 50px;">
You have ordered ${myOrder.applesQuantity} apples, ${myOrder.orangesQuantity} oranges, ${myOrder.bananasQuantity} bananas.
</div>
</html>
MyOrder.javaide
/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.entities;
public class MyOrder {
private int applesQuantity;
private int orangesQuantity;
private int bananasQuantity;
public int getApplesQuantity() {
return applesQuantity;
}
public void setApplesQuantity(int applesQuantity) {
this.applesQuantity = applesQuantity;
}
public int getOrangesQuantity() {
return orangesQuantity;
}
public void setOrangesQuantity(int orangesQuantity) {
this.orangesQuantity = orangesQuantity;
}
public int getBananasQuantity() {
return bananasQuantity;
}
public void setBananasQuantity(int bananasQuantity) {
this.bananasQuantity = bananasQuantity;
}
}