3

In an html page, I have checkboxes like following:

<input name="serviceareas" type="checkbox" id="cb1" value="1">
<input name="serviceareas" type="checkbox" id="cb2" value="2">
...

with the help of jquery I create javascript array of values for all the checked checkboxes

getSelectedValues : function() {
            var allVals = [];
            $('#checkboxTable :checked').each(function() {
                   allVals.push($(this).val());
              });
            //alert("allVals: "+allVals);

            return allVals;
        }

, and sends it to Struts 2 Action. For example in firebug request, I see :-

serviceareas 21,26,30

In the Action I have tried mapping it to

private List<String> serviceareas = new ArrayList<String>();

But instead SOP is printing it as an Object and it isn't able to cast it to java List

public class CreateEventAction extends ActionSupport {

    private List<String> serviceareas = new ArrayList<String>();

    public List<String> getServiceareas() {
        return serviceareas;
    }
    public void setServiceareas(List<String> serviceareas) {
        this.serviceareas = serviceareas;
    }

    @Override
    public String execute() throws Exception {

        if(this.serviceareas != null) {

                for (String serviceAreaId : this.serviceareas) {

                        System.out.println("String :"+serviceAreaId);

                }

            }

        return SUCCESS;
    }

Output: String :21,26,30

Please help. Thanks in advance.

2
  • How are you creating the javascript array? Can you show that code. Also why do you need to create an array when you can submit the checkboxes in a form? Also this should work actionUrl?serviceareas=21,26,30 Commented Nov 1, 2012 at 8:00
  • @Anu - I am not very good in JS, but I can tell you what I am doing. Instead of submitting the form, I am making an ajax call. I have pasted above the javascript function creating array. Commented Nov 1, 2012 at 8:53

3 Answers 3

2

Assuming that you submit to action works, try to construct your parameters like that:

serviceareas=21&serviceareas=26&serviceareas=30
Sign up to request clarification or add additional context in comments.

Comments

1

Aleksandr M's answer should work. I am posting this answer just to make it more clear how to implement it.

Instead of submiting javascript array, submit a string.

getSelectedValues : function() {
    var allVals = '';
    $('#checkboxTable :checked').each(function() {
         allVals += "serviceareas="+$(this).val()+"&";  //prepare the string
    });
    if(allVals.length>0){
         allVals = allVals.substring(0,allVals.length-1); //remove last '&'
    }    
    return allVals; //submit this string as parameter
}

2 Comments

Thanks. I will give it a go and let you know by tomorrow.
Thanks @anu for the above method. I really appreciate :)
0

I think this is not possible. When you submit a form which has a check box or multiple list in the servlet we normally call getPrameterValues() , this method internally convert the coma seperated string to and array and returns it as an array of string. But here in your case you just submit a coma separated string and action servlet call interanlly call getPrameter() , so it will be a sting there and initilaise your List with one string. to get it done you will have to submit the form which has the checkbox group.

here you can do a quick fix by spliting this coma separated string in your action class ....or you need to find out the way how you can submit with respective property in your actionFor( struts 1.2). Let me have a quick refferences of struts 2. anyhow the current approach is wrong.

1 Comment

since all checkboxes have same name i.e. name="serviceareas", it is actually a checkbox group, isn't it?

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.