Emil Iakoupov
Emil Iakoupov

Reputation: 189

How to submit data from s:select as ajax in struts 2

I have a select tag. s:selectinside a form. I want to send a post request to the action. I have struts 2 json plugin in place. I am not any in javascript or jquery.

<s:form action="selectFileType" method="post"
                             id="selectFileTypeForm">

              <div>
                 <s:select id="select" headerKey=""
                 headerValue="%{getText('uploadAttachmentType.please.select')}"
                        name="uploadAttachmentType" list="attachmentTypeList"
                        required="true" 
                        onchange="this.form.submit();"></s:select>

                    <s:hidden id="declarationObj_ideclare"
                        name="declarationObj.ideclare" />
                    <s:hidden id="declarationObj_completingpersonname"
                        name="declarationObj.completingpersonname" />
                    <s:hidden id="declarationObj_completingpersonofficaltitle"
                        name="declarationObj.completingpersonofficaltitle" />
                    <s:hidden id="declarationObj_completingpersonphone"
                        name="declarationObj.completingpersonphone" />
                    <s:hidden id="declarationObj_completingpersonext"
                        name="declarationObj.completingpersonext" />

                </div>


                  </s:form>

`

Upvotes: 0

Views: 1382

Answers (1)

Andrey Sbrodov
Andrey Sbrodov

Reputation: 88

Struts 2 has not good tools to perform ajax but you can send post request using jQuery only.

At first you need to find a target element in source code of rendered page and to make selector. It should look something like select[name='searchEngine'] option:selected.

Then you can write script that send value of your selected element to destination_url. For example:

// get element value
var selectedItem = $("select[name='searchEngine'] option:selected").val();
// send post request
$.post("destination_url", {item: selectedItem}, function() {
    alert("ok");
});

See an example there and read getting started guide there.

That's it.

Upvotes: 1

Related Questions