0

I have the code in JavaScript in which i am calling JSF backing bean. I put JSF backing bean code inside JavaScript if else block but JSF backing bean is called every time, if conditon true or false. Here is my code of report.xhtml page

   <script type="text/javascript">
        let paymentsData;
        let firstTime = localStorage.getItem('transactionData');

        if (!firstTime) {
            alert("Why u are calling");
            let a = '#{reportBean.getG2PPaymentReport()}';
            localStorage.setItem('transactionData',a);
            paymentsData = JSON.parse(localStorage.getItem('transactionData'));
        }else
        {
            paymentsData = JSON.parse(localStorage.getItem('transactionData'));
        }
    </script>

This #{reportBean.getG2PPaymentReport() JSF backing bean is calling every time, I had also checked in JSF Backend to verify but i did not called this method on init or on JSF page load.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Imran Hussain
  • 67
  • 1
  • 7
  • But also https://stackoverflow.com/questions/16588327/how-to-invoke-a-jsf-managed-bean-on-a-html-dom-event-using-native-javascript – Kukeltje May 16 '20 at 07:19
  • And https://stackoverflow.com/questions/14966205/getting-backing-bean-value-with-javascript – Kukeltje May 16 '20 at 07:21
  • Based on the title of the 'duplicate' it might not seem related (nor on the content initially) but it is. Maybe a new combined answer is better or an update of the duplicate – Kukeltje May 16 '20 at 07:23
  • @Kukeltje my Question is differnet from all the link which you provided, Every time page load i am checking if value set in local storage or not. On this basis i am calling JSF backing bean. – Imran Hussain May 16 '20 at 07:44
  • No it is 100% a duplicate cause wise. Title and second part of the content it does not seem related but never the less it is... believe me. You expect it to be called ony when the javascript condition matches (compare to the onclick/oncomplete) but it is executed render-time like stated in the duplicate. The other links help you to find a solution for you functional solution – Kukeltje May 16 '20 at 08:08
  • Rightclick page in webbrowser and *View page source*. Hopefully you'll see it. – BalusC May 16 '20 at 10:52
  • It will not be called from the browser but it will be called when your page is rendered. The page will be delivered already with the result of your bean call in the content. You need a remoteCommand to do a bean call out of javascript. – Holger May 18 '20 at 07:23

0 Answers0