4

我是 Wicket 的新手,如果按下 Functionkey,我想创建一个调用我的 Javacode 的 ajax 行为。

我的想法是创建一种将一些 Javascript 发送到浏览器的行为,只有 F 键会导致 ajax 回调。

public class HomePage extends WebPage {
public HomePage(final PageParameters parameters) {
    super(parameters);

    add(new AbstractDefaultAjaxBehavior(){

        @Override
        protected void respond(AjaxRequestTarget target) {
            //retrieve the Parametervalue from request
            final Request request = RequestCycle.get().getRequest();
            final String jsKeycode = request.getRequestParameters()
                            .getParameterValue("keycode").toString("");

            //test output   
            target.appendJavaScript("alert('from wicket ajax. you pressed "+jsKeycode+"')");
        }

        @Override
        public void renderHead(Component component, IHeaderResponse response) {
            super.renderHead(component, response);

            //Append JavaScriptcode 
            response.render(OnDomReadyHeaderItem.forScript(
                    "\n\n" +
                    "$(document).keydown(" +
                        "function(event){\n" + //120, 121 Example for F9 and F10
                            "if((event.keyCode == 120) || (event.keyCode == 121)){\n" + 
                                "event.preventDefault();\n" +
                                "window.alert('F-Key pressed');\n" +
                                //perform ajax-callback with keyCode
                            "}\n" +
                        "});\n"));   
        }
    });

现在我的问题是:我必须编写什么代码,将按下的键码作为参数执行 ajax 回调?

4

2 回答 2

7

osmdamv 提示我为我的问题找到“Wicketsolution”。现在这是我的代码,用于捕获按键并仅在某些情况下向 wicketserver 发送 ajaxrequest。

使用此示例,其他用户应该可以根据他们的需要调整此代码。

public HomePage(final PageParameters parameters) {
    super(parameters);

    add(new AjaxEventBehavior("keydown"){
        @Override
        protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
            super.updateAjaxAttributes(attributes);

            IAjaxCallListener listener = new AjaxCallListener(){
                @Override
                public CharSequence getPrecondition(Component component) {
                    //this javascript code evaluates wether an ajaxcall is necessary.
                    //Here only by keyocdes for F9 and F10 
                    return  "var keycode = Wicket.Event.keyCode(attrs.event);" +
                            "if ((keycode == 120) || (keycode == 121))" +
                            "    return true;" +
                            "else" +
                            "    return false;";
                }
            };
            attributes.getAjaxCallListeners().add(listener);

            //Append the pressed keycode to the ajaxrequest 
            attributes.getDynamicExtraParameters()
                .add("var eventKeycode = Wicket.Event.keyCode(attrs.event);" +
                     "return {keycode: eventKeycode};");

            //whithout setting, no keyboard events will reach any inputfield
            attributes.setAllowDefault(true);
        }

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            //Extract the keycode parameter from RequestCycle
            final Request request = RequestCycle.get().getRequest();
            final String jsKeycode = request.getRequestParameters()
                            .getParameterValue("keycode").toString("");

            target.appendJavaScript("alert('from wicket ajax. you pressed "+jsKeycode+"')");
        }
 });

编辑:

我插入 attributes.setAllowDefault(true)。现在它工作正常。

于 2013-01-22T22:09:15.257 回答
0

你应该使用装饰器来使用 AjaxEventBehavior

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

      IAjaxCallListener listener = new IAjaxCallListener()
      {
         @Override
         public CharSequence getBeforeHandler(Component c) { return handler; }
        .....
   };

  attributes.getAjaxCallListeners().add(listener);

}

于 2013-01-22T00:18:44.627 回答