Calling Bean-Method with Parameters from JavaScript

UI Components for JSF
Post Reply
Heety
Posts: 4
Joined: 14 Apr 2010, 17:22
Location: Austria

15 Apr 2010, 15:10

Hi everyone!

Is it possible to call a Bean-Method with Parameters using JavaScript?
A way to transfer the String-Parameter to a Member-Variable of the Bean and then call the method would also be ok...

thank for your help!
Bernd

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

16 Apr 2010, 09:07

Hello,

Yes, there is a way. Look p:remoteCommand please. It has "process", "update", "actionListener", "action" attributes and is called by JavaScript. With "process" you can transfer parametes to managed bean, then execute your logic in action / actionListener and have subsequent update at the end. I use that in my web app and it works fine. I set e.g. some values in h:hiddenField (client-side by JS) and point to this field in "process" attribute of p:remoteCommand.

Best regards.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

Heety
Posts: 4
Joined: 14 Apr 2010, 17:22
Location: Austria

16 Apr 2010, 15:16

so the first step in the p:remoteCommand is to do the process-attribute, the second one is to do the action-listener and the third is to update everything you want?

thanks for your help, i'll try it out an report if it worked out.

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

16 Apr 2010, 16:18

Heety wrote:so the first step in the p:remoteCommand is to do the process-attribute, the second one is to do the action-listener and the third is to update everything you want?

thanks for your help, i'll try it out an report if it worked out.
Yes. I have

Code: Select all

   <h:inputHidden id="infoForProps" value="#{newSocBean.infoForProp}"/>

   ..................

	<p:remoteCommand name="rcShowMorePropsInfo" process="@this,infoForProps" update="dataTableMorePropInfos" 
		actionListener="#{newSocBean.buildInfoProperties}" oncomplete="showMorePropInfosTable('#{newSocBean.sidebarSelection}')"/>
and call JS function rcShowMorePropsInfo() from another one

Code: Select all

function showMorePropsInfo(listTyp, hiddenFieldId)
{
	var selectedOptions;
	if (listTyp == 'source') {
		selectedOptions = pickListPropsWidget.selectedSource();
	} else if (listTyp == 'target') {
		selectedOptions = pickListPropsWidget.selectedTarget();
	} else {
		alert("Undefined list type");
	}
	
	jQuery("#" + hiddenFieldId).val(selectedOptions.join(";"));
	
	rcShowMorePropsInfo();
}
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

Heety
Posts: 4
Joined: 14 Apr 2010, 17:22
Location: Austria

16 Apr 2010, 17:19

thanks for the example...

i've builded somethink similar to yours, but it looks like the action/actionlistener method is never executed...

my bean:

Code: Select all

public class Karte {
 private String mapOpp;
 ...

public String mapOperation() {
        System.out.println(this.mapOpp);
        String[] werte = this.mapOpp.split(":");

        if (werte[0].equals("Drag")) {

            String[] xyKoords = werte[1].split(";");
            KarteDao.getInstance().moveMapXY(this, Double.parseDouble(xyKoords[0]), Double.parseDouble(xyKoords[1]));

        }

        if (werte[0].equals("slider")) {


            KarteDao.getInstance().zoomMap(this, Double.parseDouble(werte[1]));
        }
        if (werte[0].equals("wheel")) {
            werte = werte[1].split(";");
            KarteDao.getInstance().zoomMapWheel(this, Double.parseDouble(werte[0]), Double.parseDouble(werte[1]), Double.parseDouble(werte[2]));
        }
        return "";
    }
}
jsf:

Code: Select all

<h:form id="myForm">
   <h:inputHidden id="myFormIH" value="#{SessionBean.karte.mapOpp}"></h:inputHidden>
   <p:remoteCommand onstart="alert('start');" oncomplete="alert('complete');" name="myFormRC" actionListener="#{SessionBean.karte.mapOperation}" process="@all"></p:remoteCommand>
</h:form>
and finally the js call:

Code: Select all

function moveMapRight()
{
    mapSrc=document.getElementById("map").src;
    document.getElementById("myForm:myFormIH").value="Drag:70;0";
    //document.forms["myForm"].submit();
    myFormRC();
    waitForLoad();

}
the 2 alerts at onstart and oncomplete work, but the actionmethod is never executed...
any idea why this didn't work out?

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

19 Apr 2010, 01:03

Try;

Code: Select all

public void mapOperation(ActionEvent ae) {
In JSF 2, actionevent is optional parameter.

Heety
Posts: 4
Joined: 14 Apr 2010, 17:22
Location: Austria

19 Apr 2010, 07:50

i've tried out both, the action and actionlistener, but none of them seems to be executed...

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 37 guests