2

I need to send a JS variable to my ManagedBean and use it in a function in my ManagedBean, i'm using JSF2 and PrimeFaces 6.

Code Js & .Xhtml:

<script type="text/javascript">
 timezone = jstz.determine();
 var myLocalZone =timezone.name(); // myLocalZone = "Europe/Paris"
</script>

<p:column headerText="blabla" width="125">
<h:outputText value="#{myBean.date}">
<f:convertDateTime type="both" timeZone="#{myBean.TimeZone}">  </f:convertDateTime>
</h:outputText>

ManagedBean

public void getCurrentTimezoneOffset() {

    TimeZone tz = TimeZone.getTimeZone("I need To Use My Js variable Here");

    Calendar cal = GregorianCalendar.getInstance(tz);
    int offsetInMillis = (tz.getOffset(cal.getTimeInMillis()) * 2);

    String offset = String.format("%02d", Math.abs(offsetInMillis / 3600000));
    offset = (offsetInMillis >= 0 ? "+" : "-") + offset;

    TimeZone = "GMT" + offset;

}

EDIT :

I tried RemoteCommand but i don't know why can't work for me !!

    function customfunction() {
increment([{name:'x', value:10}, {name:'y', value:20}]);
}
</script>

I don't know where i should make the remoteCommand so i added it in the first line of my Form !

<p:remoteCommand name="increment" actionListener="#{MyBean.increment}"/>        

in my Bean i have :

public void increment() {
    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    String x = params.get("x");
    String y = params.get("y");
    System.out.println("X " + x + " Y " + y);
}

NB : I need just to refresh my page and Get The x and y in my Beans..

If You have a Solution or a suggestions to solve this problem. Thanks

TheLastOne
  • 61
  • 5
  • And also read https://stackoverflow.com/questions/7497750/how-do-you-pass-parametersfrom-javascript-into-remotecommand-then-send-it-to which is marked as a duplicate of the 'duplicate' – Kukeltje Jul 25 '17 at 09:27
  • I saw this solution ! but when i do something like this in my JS remoteCommandFunctionName({name1:'value1', name2:'value2'}); i have Js Error : remoteCommandFunctionName is not a function !! I'm not pro in JS – TheLastOne Jul 25 '17 at 09:34
  • @Kukeltje i try it but i get nothing ... i paste remoteCommand in the first line of my form. public void method() { String value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("param"); System.out.println("VALUE " + value); } Sorry but i get nothing when i refresh the page !! i need just refresh the page for getting my results no ? – TheLastOne Jul 25 '17 at 10:40
  • [ask] states to **mention** what you tried/searched/found/... And the code in the comment is unreadable. Please edit your question and add it there. – Kukeltje Jul 25 '17 at 11:28
  • @Kukeltje ok i edit it – TheLastOne Jul 25 '17 at 11:38
  • _"but i don't know why can't work for me !!"_ What does happen? What did you debug? – Kukeltje Jul 25 '17 at 11:40
  • @Kukeltje i want to do the same Stuff what do M. BalusC in this link - - > https://stackoverflow.com/questions/28591301/how-to-pass-javascript-variables-as-parameters-to-jsf-action-method But WITHOUT clicking in any button !! just when i load the page !! this link worked for me but i don't need the Button please! Thanks – TheLastOne Jul 25 '17 at 15:07
  • Then read the `p:remoteCommand` documentation. Or search the internet on remoteCommand and 'onload' or 'page load' or 'automatic' – Kukeltje Jul 25 '17 at 15:44

0 Answers0