Skip to content
Advertisement

How to send Object from JavaScript to Action class in Struts 2?

In my Action class I have an object of the class which is a POJO.

public class ConfigureTspThresholdAction extends
    ActionSupport implements SessionAware, ModelDriven<GmaThresholdParameter>{

    private Map<String,Object> session;

    private String circleId;
    private String tspId;
    private String thresholdTypeFlag;

    GmaThresholdParameter gmaThresholdParameters = new GmaThresholdParameter();

GmaThresholdParameter is also the POJO (my Entity class) here which has various members whose values I want to get filled from the user.

I get the values filled from user in textfields in my JSP:

JSP:

<s:div id="thresholdParametersDiv" cssStyle="display: none">
<table>
    <tr>
        <td>Minimum Number of OG Calls</td>
        <td><s:textfield id="thresholdParameter_1"
                name="minNumberOc"
                onkeypress="return isNumber(event,'thresholdParameter_1')"></s:textfield></td>
    </tr>
    <tr>
        <td>Minimum Duration of OG Calls (in secs)</td>
        <td><s:textfield id="thresholdParameter_2"
                name="minDurationOc"
                onkeypress="return isNumber(event,'thresholdParameter_2')"></s:textfield></td>
    </tr>
    <tr>
        <td>Maximum Number of IC Calls</td>
        <td><s:textfield id="thresholdParameter_3"
                name="maxNumberIc"
                onkeypress="return isNumber(event,'thresholdParameter_3')"></s:textfield></td>
    </tr>
    ..........similarly other textfileds
</table>

There’s the name attribute in textfields whose values are the member variables of GmaThresholdParameter which I want to get filled.

Now, I want to pick up the values from these textfields and fill my GmaThresholdParameter gmaThresholdParameters = new GmaThresholdParameter(); in my Action class.

For other primitive variables I get them filled through getter/setters and sending in my AJAX call by the same name as in Action class like:

JS:

$.ajax({
    type: 'POST',
    traditional: true,                  
    url: '/gma/updateThresholdParameters.action',
    data:
    {
        circleId: circleId,
        tspId: tspId,
        thresholdTypeFlag: thresholdTypeFlag,

        // HERE I want to send my GmaThreshholdParameter object. How to send it so that it fills my object in action class ?
    }

I want to send my GmaThreshholdParameter object from JavaScript to Action class. How to send it so that it fills my object in action class?

Should I collect the values from textfileds in an array and send it or create a JavaScript Object to send the object from JavaScript which maps the Java POJO object? Is there any solution for this?

Advertisement

Answer

You can get the values from textfields when you construct a data object. As far as you implement ModelDriven and modelDriven interceptor for it is referenced you don’t need to specify a path to nested properties because they are on the top of the valueStack.

data:
{
    circleId: circleId,
    tspId: tspId,
    thresholdTypeFlag: thresholdTypeFlag,

    minNumberOc: $("#thresholdParameter_1").val(),
    minDurationOc: $("#thresholdParameter_2").val(),
    maxNumberIc: $("#thresholdParameter_3").val()

}
Advertisement