wicket如何在页面之间传值

发布于 2021-11-14 05:00:28 字数 41 浏览 778 评论 1

如题,,,用pageparameters传值,好像传过去是StringValue,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2021-11-18 15:28:50

Wicket学习:Modal Dialog的使用和参数传递

学习目标:掌握Modal Dialog的开发与控制,特别是主窗体与对话框之间的数据传递,包括主窗体如何判断用户按下的是对话框中的Ok还是Cancel还是Close按钮,以及输入参数和输出参数的传递。

Modal window: javascript modal window example  (0.5天,必学)

dialog对于开发有两大贡献:一是控制操作流程,dialog是一种非常出色的UI流程控制手段,特别是针对需要顺序执行的环节;二是方便了模块复用,Dialog本身通常就是一个可直接复用的基本模块。例如查询条件输入对话框、联系人选择对话框、打印预览对话框等。

wicket的dialog用到了AJAX技术,故这部分例子列在AJAX examples中。

Example:Show modal dialog with a page 

Example:Show modal dialog with panel

建议初学者重点掌握好上面两个例子。

注:基类选择WebPage还是Panel目前有何明显区别尚不很清楚。大家可以comment

 

参数传递

dialog使用中的难点是如何在调用方(通常是主窗体)和被调用方(即dialog)之间传递数据。下面的一些方法也适合在两个WebPage模块之间传递数据。以下假定模块A是主模块,模块B是对话框模块。

方法1:直接通过构造函数传递

如果仅仅需要从模块A向模块B传递数据,可在B中修改构造函数,并通过构造函数的参数传递。

对Wicket Page模块而言,似乎Page对象的构造函数是不可更改的(未尝试),但是如果基于Panel实现,构造函数中就是可以加入自己的参数的。

Reference

  • How to use the same Form for editing and new,https://cwiki.apache.org/WICKET/how-to-use-the-same-form-for-editing-and-new.html(重)
  • Wicket Ajax Confirmation Modal window,2010, http://www.mysticcoders.com/blog/wicket-ajax-confirmation-modal-window/ (good example,略显复杂) 
  • Passing parameters to pages, https://cwiki.apache.org/WICKET/passing-parameters-to-pages.html (通过构造函数中的PageParameters对象和RequestCycle对象)
  • Reusable multi step panel by using a delegate pattern, https://cwiki.apache.org/WICKET/reusable-multi-step-panel-by-using-a-delegate-pattern.html (重)

 

方法2:如果模块A需要获得模块B中的数据,则可重载模块B的onWindowClose()方法,并在该方法中访问模块B中与控件关联的各种model对象获取数据。但是由于该函数写在模块A中,违背了两个模块要尽可能独立的Hollywood原则,此方法不太好,但还算简单。

chooserWindow.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {

public void onClose(AjaxRequestTarget target) {

yourTextField.setModelObject(chooserPanel.getYourValue());

target.addComponent(yourTextField);

}

});

Reference

  • Refreshing page when a modal window is closed, https://cwiki.apache.org/WICKET/modal-windows.html (该文提供了两种方法,分别是在主调方A和被调方B实现,但都不是太好)
  • Pass form input from Modal Window back to the caller page, https://cwiki.apache.org/WICKET/pass-form-input-from-modal-window-back-to-the-caller-page.html
  • Passing parameters to pages, https://cwiki.apache.org/WICKET/passing-parameters-to-pages.html (通过构造函数中的PageParameters对象和RequestCycle对象)

 

方法3:在方法2基础上,主模块提供一onOutputReady(...)方法,通过模块B的构造函数将onOutputReady函数传给B,由模块B在自己将要Close的时候调用之,在调用前由模块B将自己要返回给模块A的数据整理好,并通过该函数的输入参数调用之。这样模块A就可以通过函数参数直接拿到模块B的输出数据。但是在这种情况下,由于Java不直接支持函数指针,因此从语法形式上要通过接口interface或者delegate形式传递,也就是要把onOutputReady()封装到一个interface或一个小java类中传递。

例:

在主模块A中:

onMemberSelectOutputReady( param )

{

    更新显示

}

new UiMemberSelect( owner, input, onMemberSelectOutputReady())

或者更详细点,如下:

interface DialogModuleDataDelegate{

  onOutputReady( param );

};

在主模块A中:要implement DialogModuleDataDelegate且提供onOutputReady的具体实现。

在需要调用B的地方:new UiMemberSelect( owner, input, this )

这里,我们假定主模块A本身就实现了DialogModuleDataDelegate接口,这样只要在new UiMemberSelect中传递this即可。(这样做的小缺点是如果主模块A需要同时调用多个dialog,实现多个dialog对应的onOutputReady会造成命名冲突,需要在语法上规避)。

这种写法改进了方法2。因此,以后就不要再采用方法2了。

参考:

  • Reusable multi step panel by using a delegate pattern, https://cwiki.apache.org/WICKET/reusable-multi-step-panel-by-using-a-delegate-pattern.html (重)
  • Function Pointer in Java,http://stackoverflow.com/questions/1163367/function-pointer-in-java?rq=1
  • Wicket Ajax Confirmation Modal window,2010, http://www.mysticcoders.com/blog/wicket-ajax-confirmation-modal-window/ (good example,略显复杂)

 

方法4:虚函数法

在dialog模块中设计一个onDataReady()虚函数,在数据准备好(如用户按ok和cancel按钮的时候负责整理数据并且调用之),但是该虚函数的真正实现由模块A在调用的时候实现。

被调用的模块B:

virtual onDataReady(modalresult, params)

在主模块A中:

new UiMemberSelect(){

onDataReady(modalresult, params){

switch (modalresult){

case OK:

case CANCEL:

}

}

}

这样进一步简化了语法形式,且面对多个dialog和多种data ready的情况,也可方便处理。推荐采用。

Reference

  • Wicket: how to write a reusable modal window popup,2008,http://stuq.nl/weblog/2008-06-05/wicket-how-to-write-a-reusable-modal-window-popup (good!)
  • How to send a Java object to the ModalWindow,2010,http://apache-wicket.1842946.n4.nabble.com/How-to-send-a-Java-object-to-the-ModalWindow-td2312589.html
  • Wicket Ajax Confirmation Modal window,2010, http://www.mysticcoders.com/blog/wicket-ajax-confirmation-modal-window/ (good example,略显复杂)

 

方法5:使用URL参数 

Wicket已经提供了PageParameters对象,可以考虑如下形式:

PageParameters pars = new PageParameters();

pars.add("id", 12345);

setResponsePage(MyPage.class, pars);

 

Reference:

  • Wicket redirect: how to pass on parameters and keeps URLs “pretty”? 2010,http://stackoverflow.com/questions/4155591/wicket-redirect-how-to-pass-on-parameters-and-keeps-urls-pretty
  • How to reference a page with parameters Wicket,http://stackoverflow.com/questions/7516705/how-to-reference-a-page-with-parameters-wicket?rq=1
  • Passing info message to a Wicket page while using PageParameters,http://stackoverflow.com/questions/10189570/passing-info-message-to-a-wicket-page-while-using-pageparameters?rq=1 (该文提到:You can't use both PageParameters and another parameter as constructor arguments, because Wicket can't create your page instance with such a constructor when the page is requested. Wicket only knows how to instantiate pages with default constructors or pages with a PageParameters parameter.)
  • Getting parameters from another class in Java,2012,http://stackoverflow.com/questions/12806638/getting-parameters-from-another-class-in-java?rq=1

 

方法6:利用Session来传递数据

Session相当于是个全局数据缓冲区。这种方法使用方便,但是能不用就不用

 

方法7:Wicket 1.5 offers a simple, yet flexible, way for component to communicate with each other in a decoupled manner. (IEventSource和IEventSink接口)

Reference

  • Wicket 1.5 offers a simple, yet flexible, way for component to communicate with each other in a decoupled manner. (IEventSource和IEventSink接口),https://cwiki.apache.org/WICKET/migration-to-wicket-15.html#MigrationtoWicket1.5-Intercomponentevents
  • calling a child page component from parent page component in wicket,2011,http://stackoverflow.com/questions/7595976/calling-a-child-page-component-from-parent-page-component-in-wicket?rq=1

Reference

[参数传递问题]

How to send a Java object to the ModalWindow,

http://apache-wicket.1842946.n4.nabble.com/How-to-send-a-Java-object-to-the-ModalWindow-td2312589.html

Q: Pass form input from modal window back to the caller page

http://apache-wicket.1842946.n4.nabble.com/Pass-form-input-from-modal-window-back-to-the-caller-page-td1855573.html

Wicket redirect: how to pass on parameters and keeps URLs “pretty”?, 2010

http://stackoverflow.com/questions/4155591/wicket-redirect-how-to-pass-on-parameters-and-keeps-urls-pretty

Modal Windows, https://cwiki.apache.org/WICKET/modal-windows.html

(推荐采用setWindowClosedCallback方法以方便实现调用方和被调用方的解耦)

 

Reference 

Modal Windows, https://cwiki.apache.org/WICKET/modal-windows.html

JQuery UI Dialog as Wicket behavior example

http://code.google.com/p/jqwicket/wiki/DialogExample(仅用于理解dialog背后的实现机制)

 

如何改变Wicket modal window/dialog的外观--用CSS,下文给出了代码

Can the Wicket modal window be customized?

http://stackoverflow.com/questions/1385314/can-the-wicket-modal-window-be-customized

 

下文给出了modal window/dialog的一种规范写法,实现调用方和被调用方相对较松的解耦。建议初学者使用。

Wicket: how to write a reusable modal window popup,2008

http://stuq.nl/weblog/2008-06-05/wicket-how-to-write-a-reusable-modal-window-popup

(with source code)

 

 

Wicket Ajax Confirmation Modal window, 2008,

http://www.mysticcoders.com/blog/wicket-ajax-confirmation-modal-window/

该文给出了YES,NO两个按钮的实现方法。不过,应该用Ok, Cancel或YES/NO/Cancel更好些。

 

Close wicket modal window by pressing a button, 2011,

http://stackoverflow.com/questions/4944728/close-wicket-modal-window-by-pressing-a-button

在modal window中嵌入了视频

 

Q: Passing info to the modal window,2011,

http://markmail.org/message/ec5rf4oe6djgbb2q

R: I often pass the 'real' or 'domain' model object that a form is editing as a parameter to the form/modal constructor and then store a IModel wicket model wrapper object around that model object in the form/modal.

利用构造函数的参数来传递数据对象。

 

 

Q:Download file from ModalWindow problem

http://apache-wicket.1842946.n4.nabble.com/Download-file-from-ModalWindow-problem-td4169964.html

 

 

 

Q:

Subject: Re: modal window before submitting ajax-less form

Makes sense. In that case, Sven, how can I have a modal window rendered

before hand so I can hide/show as needed, something like an ajax-less modal?

I wouldn't like to duplicate the effort of rendering a modal window myself.

Any idea?

R: View this message in context: 

http://apache-wicket.1842946.n4.nabble.com/modal-window-before-submitting-ajax-less-form-tp4653108p4653128.html

Sent from the Users forum mailing list archive at Nabble.com.

 

Q:I Want to implement a confirmation box with ok and cancel button that will appear on page load if it satisfies some conditions. 

and if user clicks on Ok/Cancel button it should perform an operation.

Please suggest me

http://www.linkedin.com/groups/Confirmation-Box-80181.S.55805285

 

 

 How to Wicket Redirect with large parameters? 2012, http://stackoverflow.com/questions/13853320/how-to-wicket-redirect-with-large-parameters?rq=1 (If you don't have any other choice, I think you can try to do what you want through Javascript, asynchronously loading the webpage with this huge page parameter (via AjaxRequestTarget.appendJavaScript) and displaying the result in your modal window. )

 

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文