Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>When I save the key and open the new Window, the key is empty. Maybe the process of saving the key is too slow?</p> </blockquote> <p>The problem here is that JavaScript and server side methods run at different places:</p> <ul> <li><p>JavaScript code runs in client browser, it's like executing code locally in your pc.</p></li> <li><p>Server side code runs on server. The browser must do a request to the server, the server will do its job and execute the method defined in the <code>action</code> of your <code>&lt;h:commandLink&gt;</code> and then return a response to the browser. The browser will handle the response. NOTE: this is a very basic explanation, for more info about JSF requests read <a href="https://stackoverflow.com/q/33476/1065197">JSF Lifecycle and Custom components</a></p></li> </ul> <p>Knowing this, is not that the method execution is too slow, it's executed at a different time.</p> <blockquote> <p>And how to prevent the page from reloading?</p> </blockquote> <p>In short, you must do an ajax request to the server instead of a full request. To accomplish this, JSF 2 offers you <code>&lt;f:ajax&gt;</code> that will convert a simple request into an ajax request and the browser won't do a page refresh.</p> <p>The solution should be a two step process:</p> <ol> <li><p>Execute the server action to save the data in session as an ajax request.</p></li> <li><p>After the ajax request has been processed, you should open your window using JavaScript.</p></li> </ol> <p>The <code>&lt;f:ajax&gt;</code> component by nature doesn't offer an <code>oncomplete</code> javascript method,but you can use the power of the <code>onevent</code> method to execute a javascript code after completing the ajax request. The example is based on <a href="https://stackoverflow.com/q/5570180/1065197">JSF 2.0 javascript onload/oncomplete</a>.</p> <pre class="lang-html prettyprint-override"><code>&lt;h:commandLink action="#{main.setClickedId(items.itemId)}" value="Hello"&gt; &lt;f:ajax onevent="handleOnComplete" /&gt; &lt;/h:commandLink&gt; &lt;h:outputScript&gt; function handleOnComplete(e) { if (e.status == 'success') { openWin2(); } } &lt;/h:outputScript&gt; </code></pre> <p>Another way to do it would be using third party libraries like <a href="http://docs.jboss.org/richfaces/latest_4_2_X/Component_Reference/en-US/html/chap-Component_Reference-Actions.html#sect-Component_Reference-Actions-a4jcommandLink" rel="nofollow noreferrer"><code>&lt;a4j:commandLink&gt;</code></a> from RichFaces or <a href="http://www.primefaces.org/showcase-labs/ui/commandLink.jsf" rel="nofollow noreferrer"><code>&lt;p:commandLink&gt;</code></a> from PrimeFaces. I'll post a sample using PrimeFaces code:</p> <pre class="lang-html prettyprint-override"><code>&lt;p:commandLink action="#{main.setClickedId(items.itemId)}" value="Hello" oncomplete="openWin2()" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload