Set parameter for commandButton

UI Components for JSF
User avatar
benpad
Posts: 58
Joined: 24 Jun 2010, 04:54
Location: Makati, City Philippines
Contact:

24 Jul 2010, 10:37

Hello,

I'm trying to implement a simple command button component that will send a value in the backing bean.

Code: Select all

<p:commandButton value="Save" actionListener="#{myBean.someMethod}" async="true" ajax="true" update="@form">
	<f:attribute name="parameter" value="composeTheParameters()"/>
</p:commandButton> 
I used the f:attribute and it's working fine. However, my goal is to compose first the parameters using a JavaScript method. In my case, I used composeTheParameters().

Code: Select all

function composeTheParameters() {
	return 'simple parameter';
};
The backing bean is:

Code: Select all

public void someMethod(ActionEvent event) {
	String param = (String) event.getComponent().getAttributes().get("parameter");
	logger.debug(param);
}
The result in the debug is the JavaScript method name. "composeTheParameters()". Is there a way to pass the result of a JavaScript method to the backing bean method? Thanks
PrimeFaces 2.1
JSF 2.0
GlassFish 3 Server
Mojarra 2.0.2

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

24 Jul 2010, 16:12

Since you are on JSF2 and Glassfish, better solution would be to pass your parameter to the method itself;

Code: Select all

<p:commandButton value="Save" actionListener="#{myBean.someMethod('composeTheParameters')}" async="true" ajax="true" update="@form" />

Code: Select all

public void someMethod(String param) {
   //param = composeTheParameters
}

User avatar
benpad
Posts: 58
Joined: 24 Jun 2010, 04:54
Location: Makati, City Philippines
Contact:

24 Jul 2010, 18:48

Hi Prime, thanks for the reply however, the composeTheParameters is a JavaScript function that returns a value.
PrimeFaces 2.1
JSF 2.0
GlassFish 3 Server
Mojarra 2.0.2

callahan
Posts: 768
Joined: 27 May 2010, 22:52

24 Jul 2010, 19:09

Hi,

Do you mean a value that's not a constant? If so, one way to send the data to the Web server would be by using the AjaxRequest function.

You could try something like this:

Page:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:p="http://primefaces.prime.com.tr/ui">

  <h:head>
    <script type="text/javascript">
      var count = 0;

      function func() {
        PrimeFaces.ajax.AjaxRequest (
          '#{request.contextPath}/views/button/buttonparam.xhtml',
          {formId:'frm',global:true},
          {
            'frm:btn':'frm:btn',
            'primefacesPartialUpdate':'frm:message',
            'myParameter1':getMyParameter1(),
            'myParameter2':getMyParameter2()
          }
        );
      }

      function getMyParameter1() {
        return "foo" + ++count;
      }

      function getMyParameter2() {
        return "bar" + ++count;
      }
    </script>
  </h:head>

  <h:body>
    <h:form id="frm">
      <p:commandButton id="btn" value="Do It" actionListener="#{buttonBean.doIt}" onclick="func(); return false;" />
      <p:growl id="message" showDetail="true"/>
    </h:form>
  </h:body>

</html>

Bean:

Code: Select all

package controller;

import java.io.Serializable;

import javax.faces.application.FacesMessage;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import javax.faces.context.FacesContext;

import javax.faces.event.ActionEvent;

@ManagedBean
@ViewScoped
public class ButtonBean implements Serializable {
    public void doIt(ActionEvent ev) {
        FacesContext fc = FacesContext.getCurrentInstance();

        String myParameter1 = fc.getExternalContext().getRequestParameterMap().get("myParameter1");
        String myParameter2 = fc.getExternalContext().getRequestParameterMap().get("myParameter2");

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "doIt", myParameter1 + " " + myParameter2);
        fc.addMessage(null, message);
    }
}


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

24 Jul 2010, 19:18

Another way I can think of is setting the value returned by your javascript to a hidden input and do a value binding.

Code: Select all

<h:inputHidden id="param" value="#{bean.param}" />

Code: Select all

<p:commandButton onclick="document.getElementById('param').value= composeTheParameters()" value="Save" actionListener="#{myBean.someMethod}" ajax="true" update="@form" />

Code: Select all

public void someMethod() {
   //param = value from client side.
}

callahan
Posts: 768
Joined: 27 May 2010, 22:52

24 Jul 2010, 19:24

Yes, that would be another way. You're so old fashioned Optimus!

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

24 Jul 2010, 19:27

I guess I am :)

User avatar
benpad
Posts: 58
Joined: 24 Jun 2010, 04:54
Location: Makati, City Philippines
Contact:

24 Jul 2010, 19:42

Thanks. Both solutions worked. :-)
PrimeFaces 2.1
JSF 2.0
GlassFish 3 Server
Mojarra 2.0.2

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

24 Jul 2010, 21:09

Glad it works!

TaraPledge
Posts: 13
Joined: 16 Dec 2010, 22:18

23 Mar 2011, 15:39

When I try the above suggestion by Optimus I get: document.getElementById('param') is null in Firefox. I've tried adding the form id like this and it still doesn't find the component: document.getElementById('formId:param')

If I add the prefix to the id, the component is found.

I'm developing portlets in Liferay 6.

Any ideas?

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: Google [Bot] and 31 guests