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?

1
  • I don't understand why you need to send an object from javascript? If you want to send all the values use data : $("#form_id").serialize(),. Commented Jan 3, 2014 at 12:05

2 Answers 2

1

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()

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

Comments

1

Try to define a getter and a setter for gmaThresholdParameters in your Struts2 action.

Then, in your javascript, try this:

...
data:
{
   circleId: circleId,
   tspId: tspId,
   thresholdTypeFlag: thresholdTypeFlag,
   "gmaThresholdParameters.property1": whateverValueProperty1,
   "gmaThresholdParameters.property2": whateverValueProperty2,
   "gmaThresholdParameters.property3": whateverValueProperty3, 
   ...
}
...

If you have defined an empty constructor for the gmaThresholdParameters object and getters and setters for its properties, it should fill it properly.

8 Comments

That will mean I will have to keep 7 new members to my Action class and 7 getter setters as compared to now I have getter/setter for my gmaThresholdParameters Object alone. Isn't there another approach?
No, no, Struts2 allows you keep your action structure as is. The idea is it allows you to access nested properties for the actions inside objects, accesible as "object.property1", "object.property2", etc. from your HTML form. The only change you need in your action is getters and setters for its published properties, and a getter and a setter for the entire gmaThresholdParameters object. In its turn, it needs getters and setters (defined in GmaThresholdParameter.java) for every property it wants to publish.
Can u post via code as I am not able to understand. Presently I have a getter and a setter for the entire gmaThresholdParameters object.
Then you need no more code changes but in javascript. Try that javascript snippet I put there, to see if it can properly fill the properties of your threshold parameters object
Since I am implementing Model Driven for gmaThresholdParameters object, "property1": whateverValueProperty1 should be sufficient here ???
|

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.