0

I have a standard jquery ajax function like so

    $.ajax({
    type: "POST",
    url: "/myApp/InsertRecord.action", 
    data: {
        table:table,
        issueNumber:issue_number,
        resultPointer:result_pointer
    }, 
    success: function(data) {
        $("#GRID_DIV").html(data);
    }
    });

If I were to have a dynamic number of additional parameters with dynamic names, would it still be possible to use the jquery ajax function?

To show what I am doing, in a standard .submit() scenario, I would grab the data like so:

    public MyAction extends ActionSupport implements ParameterAware, SessionAware, ServletContextAware {
    private Map<String, String[]> parameters;
    .
    .
    .
    Set<String> keySet = parameters.keySet();

EDIT:

To create an array, I can do this to get all the relevant names in my form:

var allNames=new Array();
var numberOfRecords = 0;
$('input:text[name^=rec_]').each(function() {
   allNames[numberOfRecords++] = $(this).attr('name');
});

because I know that all the dynamic names will at least start with rec_ but then the problem with this is I do not have specific getters/setters for each field because I don't know what their full names will be.

3 Answers 3

1

The number/type of parameters isn't relevant; all you need is ParameterAware.

There's no need to put the fields in an array.

Sign up to request clarification or add additional context in comments.

2 Comments

When I do a submit, my parameter set has all the entries I would expect with the above code and everything is working fine. However, the parameter set is not populated on the ajax call. I would need some sort of approach like the array generation method but then have the Struts be able to handle the dynamically named parameters.
@demongolem ParameterAware puts a reference to the underlying request's parameter map into your local map; the type of request isn't relevant. If they're being sent, and nothing in your application stack is doing anything to them, they'll be in the map. Check the wire, check your interceptor stack. The names of the parameters are irrelevant.
1

The piece I was really missing was the .serialize() jQuery function.

My javascript now looks like this:

var formForm = $('#issueRecordInputForm').serialize();

var table = '<s:property value="table" />';
var issue_number = '<s:property value="issueNumber" />';
var result_pointer = '<s:property value="resultPointer" />';

$("#table").val(table);
$("#issueNumber").val(issue_number);
$("#resultPointer").val(result_pointer);

$.ajax({

    type: "POST",
    url: "/myApp/InsertRecord.action", 
    data: formForm,
    success: function(data) {
        $("#GRID_DIV").html(data);
    }
});

As you can see, I also removed three previous parameters I had and instead set hidden ids with the appropriate values in the form instead. I could not really mix the three parameters I had with the serialize call in the data: portion of the ajax call. Having done this serialization, the ParameterAware portion of struts will handle this information correctly as @Dave Newton has stated.

Comments

0

I'm not 100% sure about this, but I'm pretty sure you could just aggregate all of the data you want to send to the server into a single array and then send that. So instead of

data: {
    field1: data1
    field2: data2
    ...
}

Just put all fields into an array and then just send the array

2 Comments

Yep, it will serialize the array for you
In my case, to pass the array I needed to do like JSON.stringify(allNames, null, 2) because OGNL was choking on something or another. Then everything gets through fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.