Calling a JSF bean method within Javascript

Asked

Viewed 2,242 times

0

I would like to call a JSF method within my Javascript code. For every time he executes this method in Javascript within the function call another method that is within JSF.

Does anyone have any idea how to do that?

1 answer

2

You can use the framework Primefaces which contains a component for this, follow an example.

<h:form>
  <p:remoteCommand name="rc" update="msgs" actionListener="#{remoteCommandView.execute}" />

  <p:growl id="msgs" showDetail="true" />
  <p:commandButton type="button" onclick="rc()" value="Execute" icon="ui-icon-refresh" />
</h:form>

In this component, <p:remoteCommand>, the attribute name determines a Javascript function in which you can call to execute the JSF method defined in the attribute actionListerner.

For more information and documentation about the component see this link.

  • Thanks for answering. but in this case there, I wanted to call this remote methodCommandView.execute within a javascrpt function. I don’t know if I understand.

  • Exactly. When you call rc() will be calling a Javascript function that will execute the method #{remoteCommandView.execute}. Soon, rc() can be called within a Javascript function as well. This you need?

  • Yes, I did a test here, remoteCommand inside a form and it does not call the rc method();

  • You can show how you did it?

  • Sorry for the delay in answering, I tried a lot and I didn’t end up being sad :(. <h:form > <script src="code.js"></script> <p:remoteCommand name="rc" actionListener="#{gamesBean.addMissao()}" /> <p:commandButton type="button" onclick="test()" value="Execute" icon="ui-icon-refresh" /> </h:form> inside a code file.js Function test(){ rc(); }

  • Face discover the problem pq does not recognize the method. when it has <h:inputHidden inside the form it does not recognize the method of p:remoteCommand.

  • Um... good to know.

  • solve here. the problem is with inputhidden gets a list, so I send a list as string and convert to javascript. I don’t know if I understand it. but vlw for the help.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.