源码如下:
AJAXPeriodicUpdateMixin.java
/**
* 项目名称:TapestryStart
* 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 网址: http://www.flywind.org
* 版本:1.0
* 编写:飞风
* 时间:2012-02-29
*/
package com.tapestry.app.pages;
import java.util.Date;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
public class AJAXPeriodicUpdateMixin {
@InjectComponent
private Zone timeZone;
@Inject
private Request request;
Object onRefreshTimeZone() {
return request.isXHR() ? timeZone.getBody() : null;
}
public Date getServerTime() {
return new Date();
}
}
AJAXPeriodicUpdateMixin.tml
<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="margin-left: 50px">
<t:zone t:id="timeZone" t:mixins="PeriodicZoneUpdater" t:event="refreshTimeZone" t:frequencySecs="3" t:maxUpdates="4">
serverTime: ${serverTime}
</t:zone>
</div><br/>
</html>
PeriodicZoneUpdater.java
/**
* 项目名称:TapestryStart
* 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 网址: http://www.flywind.org
* 版本:1.0
* 编写:飞风
* 时间:2012-02-29
*/
package com.tapestry.app.mixins;
import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ClientElement;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.annotations.Parameter;
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/PeriodicZoneUpdater.js")
public class PeriodicZoneUpdater {
@Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
private String event;
@Parameter
private Object[] context;
@Parameter(defaultPrefix = BindingConstants.LITERAL, required = true)
private int frequencySecs;
@Parameter(defaultPrefix = BindingConstants.LITERAL, required = true)
private int maxUpdates;
@Inject
private ComponentResources componentResources;
@Inject
private JavaScriptSupport javaScriptSupport;
@InjectContainer
private ClientElement clientElement;
void afterRender() {
JSONObject spec = new JSONObject();
spec.put("zoneElementId", clientElement.getClientId());
spec.put("eventURL", componentResources.createEventLink(event, context).toAbsoluteURI());
spec.put("frequencySecs", frequencySecs);
spec.put("maxUpdates", maxUpdates);
javaScriptSupport.addInitializerCall("periodicZoneUpdater", spec);
}
}
PeriodicZoneUpdater.js
PeriodicZoneUpdater = Class.create( {
initialize : function(spec){
this.zoneElement = $(spec.zoneElementId).getStorage();
this.eventURL = spec.eventURL;
this.frequencySecs = spec.frequencySecs;
this.maxUpdates = spec.maxUpdates;
// Use Prototype's PeriodicalExecuter to run updateZone() every 3 seconds, this.maxUpdates times.
var updatesCount = 0;
new PeriodicalExecuter(function(pe) {
if (updatesCount++ < this.maxUpdates) {
this.updateZone();
}
else {
pe.stop();
}
}.bind(this), this.frequencySecs);
},
updateZone : function() {
var zoneManager = this.zoneElement.zoneManager;
if(!zoneManager){
return;
}
zoneManager.updateFromURL(this.eventURL);
}
} )
// Extend the Tapestry.Initializer with a static method that instantiates a PeriodicZoneUpdater.
Tapestry.Initializer.periodicZoneUpdater = function(spec) {
new PeriodicZoneUpdater(spec);
}