Question

I have a form that I would like to pass the values of the form to an action. I also don't want to use a submit button and would like to use struts2 anchor tag. Here is my below code:

 <s:form id="form" theme="xhtml" >                  
        <sj:datepicker id="startDate" name="startDate" label="Start Date" required="true"/>       
                                    <sj:datepicker id="endDate" name="endDate" label="End Date" required="true" /> 

    </s:form> 
    <s:url action="excelExport" id="excelurl" > 
    </s:url>
    <s:a formId="form" href="%{excelurl}" onclick="document.forms['form'].submit();" >Export</s:a>

The problem here is that the start date and end date are not passed to the action. Can someone advise how to pass the form values to the action. I am using struts2 jquery plugin btw. Let me know if more information is needed. Thanks in advance!

Was it helpful?

Solution 2

I was able to use javascript to get the form values and then dynamically create the url with the form values. A bit hacky but it worked. Let me know if anybody has any more elegant solutions.

<s:a id="exportLink" href="%{excelurl}" onclick="document.getElementById   ('exportLink').href=(document.getElementById('exportLink').href + 'startDate=' + document.getElementById('startDate').value + '&endDate=' + document.getElementById('endDate').value ;alert(document.getElementById('exportLink').href);">Export Current View to Excel</s:a>

OTHER TIPS

href="%{excelurl}" onclick="document.forms['form'].submit(); 

According to me href will get called once u click on the anchor tag. Can you remove the href and provide the action name in the form tag, so that clicking will call the js submit

This might help :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top