Calling managed bean methods in JavaScript in with normal HTML button

£可爱£侵袭症+ 提交于 2020-05-27 06:25:11

问题


I need to call managed bean methods in JavaScript while clicking the normal HTML button. Is it possible to do that, provided that I use JSF2.x and Primefaces?


回答1:


Yes, it is possible. Primefaces provides for a useful hook to do that with its component <p:remoteCommand>. It basically offers you with a javascript function that will be able to communicate with your bean.

Basic usage example:

The view:

<p:remoteCommand name="remote" actionListener="#{bean.listener}" update="text"/>
<h:outputText id="text" value="#{bean.text}/>
<div onclick="remote()">...<div>

The bean:

private String text = "Starting text";//getter + setter

public void listener(ActionEvent event) {
    text = "Text was changed via remote command";
}

In the above example the remote command is executed whenever your <div> is clicked. Of course, the same function could be called by clicking on your button.

Also, it would be wise to check out the Primefaces documentation.



来源:https://stackoverflow.com/questions/15152314/calling-managed-bean-methods-in-javascript-in-with-normal-html-button

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