This question already has an answer here:
-
How to call JSF backing bean method only when onclick/oncomplete/on… event occurs and not on page load
1 answer
I have image and would like to call a method that is store in bean. I was thinking of
<h:graphicImage value="DisplayImage?id=1&stuff=photo&mainID=main"
onclick="
alert('clicked me 1');
#{PersonalInformationDataBean.doSaveImage()};
alert('clicked me 2');
" styleClass="modal"/>
I was expecting #{PersonalInformationDataBean.doSaveImage()};
to work, however this is not working. I am only getting alert as clicked me 1
Any idea how to get this done?
Update 1
Forget to update that I am doing this in JSP Page.
how bout something like this
<h:graphicImage value="DisplayImage?id=1&stuff=photo&mainID=main"
onclick="$('#myFormID\\:myButtonID').click();" styleClass="modal"/>
<h:commandButton id="myButtonID" action="#{PersonalInformationDataBean.doSaveImage()}" style="display:none">
<f:ajax/>
</h:commandButton>
you might have to play a bit with the jquery selector $('#myFormID\\:myButtonID')
or $('#myButtonID')
OR
<h:commandLink>
<f:ajax event="action" listener="#{PersonalInformationDataBean.doSaveImage()}"/>
<h:graphicImage value="DisplayImage?id=1&stuff=photo&mainID=main"
styleClass="modal"/>
</h:commandLink>
I'm assuming that `#{PersonalInformationDataBean.doSaveImage()}; is a function which you've defined in your server and is not a javascript object method. Correct me if I'm wrong.
The problem here is you're trying to access server side methods from client side javascript. Since HTTP is a stateless protocol, you need some intermediate technology to access those methods.
DWR is a powerful tool that will allow you to achieve exactly this. Check out http://directwebremoting.org/dwr/documentation/index.html .I've used this countless times and it never fails.