calling bean method on image onClick event [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-13 20:37:52

问题


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&amp;stuff=photo&amp;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.


回答1:


how bout something like this

<h:graphicImage value="DisplayImage?id=1&amp;stuff=photo&amp;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&amp;stuff=photo&amp;mainID=main" 
    styleClass="modal"/>
</h:commandLink>



回答2:


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.



来源:https://stackoverflow.com/questions/11701143/calling-bean-method-on-image-onclick-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!