We currently use the following javascript to submit the form when one of the field values change.
var url = "project/location/myAction.action?name="+ lname ;
document.forms[0].action = url;
document.forms[0].submit();
which calls the following Struts2 action
<action name="myAction" class="project.location.NameAction">
<result name="success" type="tiles">myAction</result>
</action>
which then goes to the execute()
method of the Action class NameAction
where I have to check to see if the form was submitted from the javascript.
I would prefer to call the findName()
method in NameAction
directly from the javascript. In other words I want the javascript to act like the following jsp code.
<s:submit method="findName" key="button.clear" cssClass="submit" >
Any help would be appreciated!
There are different ways to achieve what you want, but probably the simpler is to map different actions to different methods of the same action class file, eg. with annotations:
or with XML:
Then in javascript:
You can use the same action class to map different methods using
method
attributeBy default the
method
attribute if omitted usesexecute
method.This approach requires changing the action name and hence URL to map the action. If you want to keep the same URL for different actions, then you should pass a method name as parameter to the action. Then in execute method parse this parameter for the method name and call the corresponding method.
When DMI was enabled in the previous versions to call the method you could use a
method
attribute of thes:submit
tag. Currently themethod:
parameter name is blocked by theparams
interceptor, even if it gets to the action mapper.You also read other possibilities from the How to exclude the submit action from a list of parameters in struts2.
For completeness here is how I implemented the advice from Andrea and Roman.
When the user enters data in both the
firstName
andlastName
fields we show them a list of names to choose from to fill in the rest of the form. The jsp isThe javascript is
The javascript forms a url by adding "_NameSearch" to the
action
of the form calling theselectNameInfo()
function. In this case the new action isnewRequest_NameSearch
which goes to the following xml which calls thegenerateNameList()
method of the action class without using DMI which was my original question.