{"action":"created","issue":{"url":"https://api.github.com/repos/ova2/primefaces-cookbook/issues/6","labels_url":"https://api.github.com/repos/ova2/primefaces-cookbook/issues/6/labels{/name}","comments_url":"https://api.github.com/repos/ova2/primefaces-cookbook/issues/6/comments","events_url":"https://api.github.com/repos/ova2/primefaces-cookbook/issues/6/events","html_url":"https://github.com/ova2/primefaces-cookbook/issues/6","id":89190668,"number":6,"title":"re: executing javascript from java","user":{"login":"eekremer","id":11631875,"avatar_url":"https://avatars.githubusercontent.com/u/11631875?v=3","gravatar_id":"","url":"https://api.github.com/users/eekremer","html_url":"https://github.com/eekremer","followers_url":"https://api.github.com/users/eekremer/followers","following_url":"https://api.github.com/users/eekremer/following{/other_user}","gists_url":"https://api.github.com/users/eekremer/gists{/gist_id}","starred_url":"https://api.github.com/users/eekremer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eekremer/subscriptions","organizations_url":"https://api.github.com/users/eekremer/orgs","repos_url":"https://api.github.com/users/eekremer/repos","events_url":"https://api.github.com/users/eekremer/events{/privacy}","received_events_url":"https://api.github.com/users/eekremer/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-06-18T03:51:53Z","updated_at":"2015-06-19T19:18:51Z","closed_at":null,"body":"\r\n##### Hello Oleg !\r\n\r\nI am currently engaged in developing a web project using PrimeFaces. I bought your cookbook (pretty insightful !). I am putting in practice some concepts shown on this book, but I cannot execute a javacript from java. In short, I need **\"to click a button from java\"**.  I debugged and logged it, checked the browser's console for errors (none), tried jQuery's different notations ($, jQuery, PF), but nothing. I also reviewed the page's source code and jQuery plugin is there. Any hints is welcome !\r\nThanks in advance,\r\n\r\n##### Ed Kremer\r\n\r\n\r\n\r\n##### user.xhtml\r\n```\r\n...\r\n<?xml version='1.0' encoding='UTF-8' ?>\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\"\r\n\txmlns:h=\"http://xmlns.jcp.org/jsf/html\"\r\n\txmlns:f=\"http://xmlns.jcp.org/jsf/core\"\r\n\txmlns:p=\"http://primefaces.org/ui\"\r\n\txmlns:ui=\"http://xmlns.jcp.org/jsf/facelets\">\r\n<f:view contentType=\"text/html\">\r\n\t<div style=\"padding-right: 20px;\">\r\n\t\t<h:form prependId=\"false\" id=\"userData\">\r\n\t\t\t\r\n\t\t\t<h:outputText id=\"indicator\" value=\"Enabled? - #{LoginController.loggedIn}\"/>\r\n\t\t\t\r\n\t\t\t\r\n\t\t\t<p:commandButton id=\"btn2\" widgetVar=\"btn2\"\r\n\t\t\t\tvalue=\"enable cash-in !\" onclick=\"java.enableBillValidator()\" />\r\n...\r\n\r\n```\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n##### LoginController.java\r\n\r\n\tif (password.equals(loggedCustomer.getPassword())) {\r\n\t\t\tif (loggedCustomer.getPassword() == null) {\r\n\t\t\t\tpassword = \"\";\r\n\t\t\t\tpassword2 = \"\";\r\n\t\t\t\tmsg = \"Error este usuario no tiene su clave seteada. Contacte al administrador.\";\r\n\t\t\t\trc.execute(\"PF('dlgmsg').show();\");\r\n\t\t\t} \r\n\t\t\telse {\r\n\t\t\t\tloggedIn = true;\r\n\t\t\t\tscript1 = \"PF('btn2').show();\";\r\n\t\t\t\tscript2 = \"$('btn2').show();\";\r\n\t\t\t\tscript3 = \"$('btn2').show();\";\t\r\n\t\t\t\trc.execute(script1);\r\n\t\t\t\trc.execute(script2);\r\n\t\t\t\trc.execute(script3);\r\n\t\t\t\ttry {\r\n\t\t\t\t\tExternalContext ec = FacesContext.getCurrentInstance()\r\n\t\t\t\t\t\t\t.getExternalContext();\r\n\t\t\t\t\tec.redirect(((HttpServletRequest) ec.getRequest())\r\n\t\t\t\t\t\t\t.getRequestURI());\r\n\t\t\t\t} catch (IOException e) {\r\n\t\t\t\t\tlog.error(\"Error inesperado.\", e);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\tmsg = \"Contraseña incorrecta, reintente.\";\r\n\t\t\trc.execute(\"PF('dlgmsg').show();\");\r\n\t\t}\r\n\t}"},"comment":{"url":"https://api.github.com/repos/ova2/primefaces-cookbook/issues/comments/113611861","html_url":"https://github.com/ova2/primefaces-cookbook/issues/6#issuecomment-113611861","issue_url":"https://api.github.com/repos/ova2/primefaces-cookbook/issues/6","id":113611861,"user":{"login":"eekremer","id":11631875,"avatar_url":"https://avatars.githubusercontent.com/u/11631875?v=3","gravatar_id":"","url":"https://api.github.com/users/eekremer","html_url":"https://github.com/eekremer","followers_url":"https://api.github.com/users/eekremer/followers","following_url":"https://api.github.com/users/eekremer/following{/other_user}","gists_url":"https://api.github.com/users/eekremer/gists{/gist_id}","starred_url":"https://api.github.com/users/eekremer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eekremer/subscriptions","organizations_url":"https://api.github.com/users/eekremer/orgs","repos_url":"https://api.github.com/users/eekremer/repos","events_url":"https://api.github.com/users/eekremer/events{/privacy}","received_events_url":"https://api.github.com/users/eekremer/received_events","type":"User","site_admin":false},"created_at":"2015-06-19T19:18:51Z","updated_at":"2015-06-19T19:18:51Z","body":"##### Hi Oleg,\r\nThanks for your comments regarding the fact that JavaScript code is executed after the view is rendered (I was not certain!). However, when I force the page reload in the managed bean by using the **ExternalContext** class, this seems to prevent the JavaScript code from executing (please see code below). On the opposite, if I remove the **ExternalContext** code lines from the bean, the JavaScript code is executed correctly, but the page is not reloaded and I need it to do so. Any suggestions welcome !\r\n(by the way: I just sent a mail to Amazon customer service because though your cookbook 2nd edition is among my purchases, the site will not allow me to post my comments on it. I shall have news for you shortly)\r\nCheers\r\nEd Kremer\r\n\r\n\r\n```\r\n                ....\r\n\t\t\t\tloggedIn = true;\r\n\t\t\t\t\r\n\t\t\t\trc.execute(\"$('#btn2').click();\");\r\n\t\t\t\tlog.info(\"about to execute script... \");\r\n\t\t\t\t\t\r\n\t\t\t\ttry {\r\n\t\t\t\t\tExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();\r\n\t\t\t\t\tec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());\r\n\t\t\t\t\t\r\n\t\t\t\t} catch (IOException e) {\r\n\t\t\t\t\tlog.error(\"Error inesperado.\", e);\r\n\t\t\t\t}\r\n\r\n\t\t\t}\r\n```\r\n          "}}