11

when we use apex:param inside an apex:actionFunction component as given below

<apex:page controller="testCon">
<apex:outputText value="Clicked? {!state}" id="showstate" />
<apex:outputPanel onclick="methodOneInJavascript('Yes!')" styleClass="btn">
    Click Me
</apex:outputPanel>
<apex:form>
    <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
        <apex:param name="firstParam" assignTo="{!state}" value="" />
    </apex:actionFunction>
</apex:form>

public class testCon {

    public void setState(String n) {
        state = n;
    }

    public String getState() {
        return state;
    }

    public PageReference methodOne() {
        return null;
    }

    private String state = 'no';
}

How will be the request parameter firstParam sent to the controller when an Ajax request happens? Is it via query string parameter or in the body of the request (as in POST request) ?

3 Answers 3

10

When you declare an actionFunction a javascript function name equal to actionFunction's attribute name named is declared and initialised on rendered visualforce page.

Ex:

<apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
        <apex:param name="firstParam" assignTo="{!state}" value="" />
</apex:actionFunction>

Above code will rendered as:

var methodOne = function(firstParam, otherConfigParams) {

// Ajax implemetation

}

and when you call this actionfunction by apex:commandButton or by any other method it sends post request to visualforce page url and the request url seems like:

https://www.salesforcedomain/apex/vfpagemae

and the Ajax request constructed ( Body ) as:

com.salesforce.visualforce.ViewState: "i:COMPLETE VIEWSTATE"
com.salesforce.visualforce.ViewStateCSRF: "SecurityChecks"
com.salesforce.visualforce.ViewStateMAC: "AJUrHNkMoCfHaWCeqkFxDJVJ2D+b"
com.salesforce.visualforce.ViewStateVersion: "201310242028360004"
j_id0:j_id9: "j_id0:j_id9" // FORM_ID
j_id0:j_id9:j_id10: "j_id0:j_id9:j_id10"
firstParam: "VALUE_SENDING"

So each request send by actionFunction is POST request and content sent is exist in body.

We cannot say that request is sent like https://www.salesforcedomain/apex/vfpagemae?Params=abcdf and page can only get params through it.

Basically Salesforce has its own way and it assign all values to MAP ApexPages.CurrentPage().getParametrs() either it recieved from Query URL String or from body.

7

So when you are using a actionFunction there are two ways you can access the value from the controller.

  • You can use the assignTo attribute of the apex:param to set a variable in the controller.
  • You can use Apexpages.Currentpage().getParameters().get('yourParamName')

For your question

How will be the request parameter firstParam sent to the controller when an Ajax request happens? Is it via query string parameter or in the body of the request (as in POST request) ?

If you check the option "2" we can retrieve the value of the param using URL parameter. So the Answer is it is Send via Query Parameters

0

You have to add a public attribute in the controller.

This attr should have getter and setter.

public String state {get; set;}
1
  • 1
    Martin, I have declared the variable state with explicit getter and setter methods.
    – codebandit
    Oct 29, 2013 at 8:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .