Wicket Ajax

 Apache Wicket > Index > Wicket Roadmap and Versions > Wicket 6.0 > Wicket Ajax
#editReport()

What ?

Since version 6.0 Wicket uses jQuery as a backing library for its Ajax functionality.

Why ?

The previous implementations of wicket-ajax.js and wicket-event.js were home baked solutions that worked well but also suffered from the differences in the browsers. Often users complained that some functionality doesn't work on particular version of particular browser. That's why the Wicket team chose to use jQuery to deal with browser inconsistencies and leave us to do our business logic.

Design and implementation

The new implementations (wicket-ajax-jquery.js and wicket-event-jquery.js) use jQuery internally but expose Wicket.** API similar to the previous version.

All Java components and behaviors should still use the Wicket.** API. This way if someday we decide to not use jQuery anymore we will have less work to do. Also if a user uses Dojo/YUI/ExtJS/... and prefer to not have jQuery in her application then she will be able to provide wicket-ajax-xyz.js implementation and replace the default one.

Table with renamed methods from the previous version

1.56.0
Wicket.fixEventWicket.Event.fix
Wicket.stopEventWicket.Event.stop
Wicket.showWicket.DOM.show
Wicket.showIncrementallyWicket.DOM.showIncrementally
Wicket.hideWicket.DOM.hide
Wicket.hideIncrementallyWicket.DOM.hideIncrementally
Wicket.decodeWicket.Head.Contributor.decode
Wicket.ajaxGet, wicketAjaxGetWicket.Ajax.get
Wicket.ajaxPost, wicketAjaxPostWicket.Ajax.post
Wicket.submitFormWicket.Ajax.submitForm
Wicket.submitFormByIdWicket.Ajax.submitForm
Wicket.replaceOuterHtmlWicket.DOM.replace
Wicket.Form.doSerializeWicket.Form.serializeForm

Configuration

Setup

To replace any of the JavaScript files the user application may use:

MyApplication#init():

public void init() {
  super.init();

  IJavaScriptLibrarySettings jsSettings = getJavaScriptLibrarySettings();

  jsSettings.setJQueryReference(new MyJQueryReference());

  jsSettings.setWicketEventReference(new DojoWicketEventReference());

  jsSettings.setWicketAjaxReference(new DojoWicketAjaxReference());

}
Resource dependencies

Since Wicket 6.0 ResourceReference can have dependencies and it is recommended to properly define the dependency chain between this classes.
See the code of org.apache.wicket.ajax.WicketAjaxJQueryResourceReference to see how the default jQuery based implementation does that.

If the user application needs to upgrade/downgrade to new/old version of jQuery then just the first line above is needed:

  getJavaScriptLibrarySettings().setJQueryReference(new AnotherVersionOfJQueryReference());

If the user application needs to use Dojo instead of jQuery then it has provide JavaScriptResourceReferences for wicket-event-dojo.js and wicket-ajax-dojo.js (e.g. DojoWicketEventReference and DojoWicketAjaxReference). Those references should define dependency to DojoReference (a reference that delivers dojo.js). Wicket uses IJavaScriptLibrarySettings#getWicketAjaxReference() and all its transitive dependencies for its JavaScript needs.

AjaxRequestAttributes

Each Ajax behavior and component can use o.a.w.ajax.attributes.AjaxRequestAttributes to configure how exactly the Ajax call should be executed and how its response should be handled. To do this use:

AnyAjaxComponent/AnyAjaxBehavior.java:

protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
{
    super.updateAjaxAttributes(AjaxRequestAttributes attributes);

    attributes.[set some attribute]();
}

The available attributes are:

NameDescriptionDefault valueShort name
methodthe request method to use (GET or POST)GETm
multipartwhether form submittion should use content type: multipart/form-data. Implies POST methodfalsemp
event namesa list of event names for which the Ajax call will be executed. For example: click, change, keyup, etc.domreadye
form idthe id of the form which should be submitted with this Ajax call. f
submitting component namethe input name of the component which submits the form. sc
data typewhat kind of data is expected in the response of the Ajax call (e.g. XML, JSON, HTML, JSONP).xmldt
wicket ajax responsea boolean flag which indicates whether the response is <ajax-response> which is handled by wicket-ajax.js or custom response type which can be handled by application's code (e.g. in IAjaxCallListener's success handler).truewr
channelthe name and type of the Ajax channel to use. Channels are used to queue the Ajax requests at the client side. See org.apache.wicket.ajax.AjaxChannel javadoc for more details.channel with name '0', and 'queue' behaviorch
ajax call listenersa list of listeners which are called at the most important points of the lifetime of the Ajax call. See below for more information.empty listbh, pre, bsh, ah, sh, fh, coh
extra parametersa map of parameters which should be added to the query string/post data of the Ajax call. The name and value of such parameters should be known at the server side.empty mapep
dynamic extra parametersparameters which values are calculated at the client side and added dynamically to the query string/post data of the Ajax call.empty listdep
request timeouta timeout to abort the request if there is no response.0 (no timeout)rt
allow defaulta boolean flag which indicates whether to allow the default behavior of the HTML element which listens for the event. For example: clicking on Ajax checkbox should allow the default behavior to actually check the box.falsead
asynca boolean flag that indicates whether the Ajax call should be asynchronous or not.trueasync
throttling settingssettings which define whether the Ajax call should be throttled and for how long. See the javadoc of org.apache.wicket.ajax.attributes.ThrottlingSettings for more information.no throttlingtr

Attributes 'c' (component id) and 'u' (callback url) are automatically set by the Ajax behavior and they are not part of AjaxRequestAttributes.

While constructing the JavaScript that will register the event listener for that Ajax component/behavior these settings are serialized to optimized JSON object which is passed as a parameter to Wicket.Ajax.(get|post|ajax) methods.
For example an AjaxLink contributes JavaScript similar to :

  Wicket.Ajax.get({"u":"the/url/to/the/link", "e": "click", "c":"linkId"});

Many of the attributes have default values which are not written in the JSON settings and they are initialized at the client side (i.e. wicket-ajax.js knows the defaults). The example above can be read as: when HTML element with id 'linkId' is clicked fire an Ajax call with Url 'the/url/to/the/link'.
If you need more examples how to use it from JS, take a look at wicket sources: wicket-core/src/test/js/ajax.js

Migration steps

o.a.w.ajax.IAjaxCallDecorator is replaced with o.a.w.ajax.attributes.IAjaxCallListener.

Since Wicket Ajax now register DOM events (like click, change, ...) instead of using inline attributes like onclick, onchange, ... there is no more a script to decorate. Instead the new implementation provides points to listen to:

  • before handler - executed before the data for the Ajax call is calculated. Even before the preconditions.
  • precondition - if it returns false then the Ajax call (and all handlers below) is not executed at all
  • beforeSend handler - executed before the actual execution of the Ajax call.
  • after handler - if the Ajax call is asynchronous then it is executed right after its firing. If it is synchronous then it is executed after the complete handler
  • success handler - executed on successful return of the Ajax call
  • failure handler - executed on unsuccessful return of the Ajax call
  • complete handler - executed at the very end. After either the success or failure handlers.

To use it do:
AnyAjaxComponent/AnyAjaxBehavior.java:

protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
{
    super.updateAjaxAttributes(attributes);

    AjaxCallListener myAjaxCallListener = new AjaxCallListener() {

        @Override 
        public CharSequence getBeforeHandler(Component component) { 
            return "alert('I\'m executed before the firing of the Ajax call')"; 
        }
    };
    attributes.getAjaxCallListeners().add(myAjaxCallListener);
}

There are also handy methods like onBefore(CharSequence), onComplete(CharSequence), ... but they do not provide access to the component which is bound with the Ajax behavior.

An Ajax request can have 0 or more IAjaxCallListener's.
o.a.w.ajax.attributes.AjaxCallListener is an adapter of IAjaxCallListener that implements all methods with noop bodies. If the body returns null or empty string then it is discarded.

Global Ajax call listeners

IAjaxCallListener's can be used to listen for the lifecycle of an Ajax call for a specific component.
If the user application needs to listen for all Ajax calls then it may subscribe to the following topics:

  • /ajax/call/before
  • /ajax/call/precondition
  • /ajax/call/beforeSend
  • /ajax/call/after
  • /ajax/call/success
  • /ajax/call/failure
  • /ajax/call/complete

Those replaces the old Wicket.Ajax.(registerPreCallHandler|registerPostCallHandler|registerFailureHandler) methods and uses publish/subscribe mechanism.

Example (JavaScript):

Wicket.Event.subscribe('/ajax/call/failure', function(jqEvent, attributes, jqXHR, errorThrown, textStatus) {
  // do something when an Ajax call fails
});

All global listeners receive the same arguments as the respective IAjaxCallListener handler plus jQuery.Event that is passed by the PubSub system. This event is not the event that caused the Ajax call. The one that caused the Ajax call is 'attrs.event'.

There are two additional topics which are used to notify the subscribers when an HTML element is about to be removed and when a new one is added to the document:

  • /dom/node/removing - receives as parameters the jQuery.Event and the HTMLElement that will be removed
  • /dom/node/added - receives as parameters the jQuery.Event and the HTMLElement that has just been added
Automatically migrated attributes.

Some of the attributes are available from the previous versions of Wicket as an overridable methods in AbstractDefaultAjaxBehavior. These methods are marked as deprecated and will be removed in Wicket 7.0 and for now Wicket automatically translates them into AjaxRequestAttributes.
These methods are:

  • org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getPreconditionScript()
  • org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getSuccessScript()
  • org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getFailureScript()
  • org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getChannel()

It is recommended to override org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#updateAjaxAttributes(AjaxRequestAttributes) and configure those directly in the passed 'attributes'.

Tests for the client side Wicket Ajax functionality

At ajax.js you may see the currently available JavaScript unit tests that we have for the Ajax functionality in wicket-ajax.js

Blog articles

An introduction to the new functionalities are described at Wicket In Action. There is also a link to a demo application.

FAQ

How to check whether my custom version of the backing JavaScript library (jQuery) doesn't break Wicket internals somehow ?
  1. Clone Wicket from its Git repository
    git clone http://git-wip-us.apache.org/repos/asf/wicket.git
  2. Open wicket-core/src/test/js/all.html and change it to point to your version of the backing library.
  3. Run the non-Ajax tests by opening file:///path/to/wicket/wicket-core/src/test/js/all.html
  4. To run the Ajax tests see the description at the top of wicket-core/src/test/js/ajax.js. It is required to run them through Web Server
What parameters are passed to the handlers ?
  1. before handler - attributes (the Ajax call attributes)
  2. beforeSend handler - attributes, jqXHR (the jQuery XMLHttpRequest object), settings (the jQuery ajax settings)
  3. after handler - attributes
  4. success handler - attributes, jqXHR, data (the response), textStatus (the response status)
  5. failure handler - attributes, errorMessage (the error message from jQuery)
  6. complete handler - attrs, jqXHR, textStatus

The global listeners receive the same parameters prepended by jqEvent. This is the event triggered by jQuery. See section Global Ajax call listeners above.

How to use the preconditions with non-native/JavaScript-based confirm dialogs ?

See possible solutions from a mailing list discussion


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录 1.简介 2.如何使用示例代码 3.我为什么要学习Wicket? 3.1。我们都喜欢意大利面:-) ... 3.2。面向组件的框架 - 概述 3.3。面向组件的Web开发框架的优点 3.4。Wicket与其他面向组件的框架相比 威克特说“你好世界!” 4.1。Wicket分发和模块 4.2。Wicket应用程序的配置 4.3。HomePage类 4.4。Wicket链接 4.5。摘要 5. Wicket作为页面布局管理器 5.1。页眉,页脚,左侧菜单,内容等... 5.2。这是继承! 5.3。划分et impera! 5.4。使用wicket标记继承:扩展标记 5.5。摘要 6.保持对HTML的控制 6.1。隐藏或禁用组件 6.2。修改标签属性 6.3。生成标记属性“id” 6.4。使用WebMarkupContainer创建内嵌面板 6.5。使用标记片段 6.6。将标题内容添加到最终页面 6.7。在我们的页面/面板中使用存根标记 6.8。如何仅渲染组件主体 6.9。用wicket隐藏装饰元素:enclosure标签 6.10。使用Border包围现有标记 6.11。摘要 7.组件生命周期 7.1。组件的生命周期阶段 7.2。组件生命周期的钩子方法 7.3。初始化阶段 7.4。渲染阶段 7.5。删除阶段 7.6。独立舞台 7.7。摘要 8.页面版本控制和缓存 8.1。有状态页面与无状态页面 8.2。有状态页面 8.3。无状态页面 8.4。摘要 9.在请求处理的引擎盖下 9.1。类应用和请求处理 9.2。请求和响应类 9.3。请求处理的“主管” - RequestCycle 9.4。会话类 9.5。异常处理 9.6。摘要 10. Wicket链接和URL生成 10.1。PageParameters 10.2。可收藏的链接 10.3。使用标记wicket自动创建可收藏的链接:链接 10.4。外部链接 10.5。无状态链接 10.6。生成结构清晰的URL 10.7。摘要 11. Wicket模型和表格 11.1。什么是模特? 11.2。IModel和Lambda 11.3。模型和JavaBeans 11.4。Wicket形式 11.5。组件DropDownChoice 11.6。模型链 11.7。可拆卸型号 11.8。在组件中使用多个模型 11.9。使用型号! 11.10。摘要 12. Wicket详细说明 12.1。默认表单处理 12.2。表单验证和反馈消息 12.3。输入值转换 12.4。使用JSR 303验证 12.5。使用IFormSubmittingComponent提交表单 12.6。嵌套表格 12.7。多行文字输入 12.8。上传文件 12.9。使用FormComponentPanel创建复杂的表单组件 12.10。无国籍形式 12.11。使用单选按钮和复选框 12.12。使用ListMultipleChoices和Palette选择多个值 12.13。摘要 13.使用中继器显示多个项目 13.1。RepeatingView组件 13.2。ListView组件 13.3。RefreshingView组件 13.4。可分页的中继器 13.5。摘要 14.组件排队 14.1。标记层次结构和代码 14.2。改进了汽车组件 14.3。组件什么时候出列? 14.4。排队的限制 14.5。摘要 15.与Wicket的国际化 15.1。本土化 15.2。Wicket的本地化 15.3。捆绑查找算法 15.4。组件选择的本地化 15.5。国际化和模型 15.6。摘要 16. Wicket的资源管理 16.1。静态与动态资源 16.2。资源参考 16.3。包资源 16.4。向页眉部分添加资源 16.5。上下文相关资源 16.6。资源依赖性 16.7。使用资源包聚合多个资源 16.8。将JavaScript放在页面正文中 16.9。标题贡献者定位 16.10。自定义资源 16.11。安装资源 16.12。Lambda支持 16.13。共享资源 16.14。自定义资源加载 16.15。CssHeaderItem和JavaScriptHeaderItem压缩 16.16。NIO资源 16.17。资源通过模型得出 16.18。摘要 17.与JavaScript集成的示例 17.1。我们想做什么...... 17.2。......以及我们将如何做到这一点 17.3。摘要 18. Wicket高级主题 18.1。通过行为丰富组件 19.使用AJAX 19.1。如何使用AJAX组件和行为 19.2。内置AJAX组件 19.3。内置的AJAX行为 19.4。使用活动指示器 19.5。AJAX请求属性和调用侦听器 19.6。创建自定义AJAX调用侦

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值