0

Call method java use javascript got error

asked 2020-12-08 13:49:38 +0800

onsir gravatar image onsir
132 2

Hi all,

how to call method java use Javascript, i have try like this

in zul file

<zk xmlns="http://www.zkoss.org/2005/zul"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.zkoss.org/2005/zul
                        http://www.zkoss.org/2005/zul/zul.xsd"
    xmlns:w="http://www.zkoss.org/2005/zk/client"
    xmlns:n="http://www.zkoss.org/2005/zk/native">

<script src="js/jquery-1.7.1.min.js" type="text/javascript"/>
<script>
   function doPrintIframe(){
         WaitForIFrame();
         resetF();
   }

   function WaitForIFrame(){
       //display iframe
   }

   function resetF() { 
       zk.Widget.$('$CSController').fire('resetForm', null, {toServer: true});
       zAu.send(new zk.Event(zk.Widget.$('$CSController'), "resetForm", {'' : {'data' : {'nodeId': ''}}}, {toServer:true}));
    } 

</script>

<window id="win1" hflex="1"  border="normal" sizable="true" maximizable="true"
    apply="${CSController},org.zkoss.bind.BindComposer">

</window>
</zk>

in java class

@org.springframework.stereotype.Component("CSController")

@Scope("prototype") public class CSalesController extends GenericForwardComposer{

@Override
public void doAfterCompose(Component comp) throws Exception {
     super.doAfterCompose(comp);

}

      @Command
      public void resetForm() {
         System.out.println("reset form processs");
      }

}

but get error

[http-bio-8084-exec-20] ERROR org.zkoss.zk.ui.impl.DesktopImpl - [Desktop z_fsc:/WEB-INF/ui/sales.zul] client error: Failed to process script Cannot read property 'fire' of null (TypeError)

Thank you very much

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-12-09 16:50:32 +0800

MDuchemin gravatar image MDuchemin
2560 1 6
ZK Team

Hi there,

Regarding the error message first:

You are using the $ selector to retrieve the widget for a ZK component with id="CSController" If there is no widget in the page associated with this component, then the result of zk.Widget.$('$CSController') will be null, and calling the .fire() function from it will result in the error shown in your message. the "client error" means that the error has happened at client side, and was sent back to the server.

Regardinge how to send and event to the server: example here: https://zkfiddle.org/sample/3l4eee1/3-send-composer-custom-event-from-JS

I trimmed a few things, to keep the essential. You can use this structure while adding your own code on top for spring, iframe, etc.)

In this case, you are most likely targeting the wrong component. In your zul file, I see a id="win1" on the window. If that's the target for your custom event, that's what you should be targetting with zk.Widget.$('$win1') to retrieve a valid widget.

To make it work you need to:

  1. target a valid ZK widget. It can be almost any widget in the page as long as you have a matching listener.
  2. Send an event. (events start with "on": "onFoo", "onBar", "onMyCustom")
  3. Have the matching event listener on the target component matching your widget. In that case, I use the widget for the window with id win1 ("$win1") selector. Therefore, I will declared the event listener on the same window in java code.

{toServer: true} is helpful, since it will force the client to send the event to the server, instead of simply using it at client-side.

Using both wgt.fire() and zAu.send() is overkill, since wgt.fire will end up doing zAu send internally. Same result, no need to send twice.

There is doc on these structures here: https://www.zkoss.org/wiki/ZKClient-sideReference/Communication/AURequests/Client-sideFiring

have a look ;)

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2020-12-08 13:49:38 +0800

Seen: 11 times

Last updated: Dec 09 '20

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More