admin管理员组文章数量:1326278
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 Struts 2 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" >
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 Struts 2 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" >
Share
Improve this question
edited Nov 28, 2024 at 14:54
BalusC
1.1m376 gold badges3.6k silver badges3.6k bronze badges
asked Mar 3, 2016 at 17:07
ponder275ponder275
9352 gold badges14 silver badges37 bronze badges
3
- Do you know that such submit doesn't work by default? – Roman C Commented Mar 3, 2016 at 17:10
- @Roman Are you referring to the javascript submit or the struts submit? – ponder275 Commented Mar 3, 2016 at 18:15
- If you want javascript act like the struts submit, you should be aware that struts submit shouldn't have method attribute, see my answer below. You should elaborate what do you mean by act like. – Roman C Commented Mar 3, 2016 at 18:19
3 Answers
Reset to default 3There 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:
public class NameAction {
@Action("myAction")
public String execute(){ ... }
@Action("myActionFindName")
public String findName(){ ... }
}
or with XML:
<action name="myAction" class="project.location.NameAction">
<result name="success" type="tiles">myAction</result>
</action>
<action name="myActionFindName" class="project.location.NameAction" method="findName">
<result name="success" type="tiles">myAction</result>
</action>
Then in javascript:
var url = "project/location/myActionFindName.action?name="+ lname ;
You can use the same action class to map different methods using method
attribute
<action name="myAction" class="project.location.NameAction" method="findName">
By default the method
attribute if omitted uses execute
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 the s:submit
tag. Currently the method:
parameter name is blocked by the params
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 pleteness here is how I implemented the advice from Andrea and Roman.
When the user enters data in both the firstName
and lastName
fields we show them a list of names to choose from to fill in the rest of the form. The jsp is
<div class="row">
<div class=" col-sm-2 col-xs-12 no-padding-right text-right"><span class="required">*</span><label class="pull-right" for="lastNameId"><s:text name="lastName"></s:text>:</label></div>
<div class=" col-sm-2 col-xs-12 no-padding-right ">
<s:textfield name="lastName" id="lastNameId" maxlength="50" onchange ="dirtyFlag();" onblur="selectNameInfo(\'newRequest\');" class="form-control"/>
</div>
<div class=" col-sm-2 col-xs-12 no-padding-right text-right " ><span class="required">*</span><label class="pull-right" for="firstNameId"><s:text name="firstName"></s:text>:</label></div>
<div class=" col-sm-2 col-xs-12 no-padding-right ">
<s:textfield name="firstName" id="firstNameId" maxlength="50" onchange ="dirtyFlag();" onblur="selectNameInfo(\'newRequest\');" class="form-control"/>
</div>
</div>
The javascript is
function selectNameInfo(formId) {
var lastName = document.forms[0].elements["lastNameId"].value;
var firstName = document.forms[0].elements["firstNameId"].value;
if(lastName != "" && firstName != ""){
clearDirtyFlag();
var oldAction = document.getElementById(formId).action;
var actionName = document.getElementById(formId).name;
var url = oldAction.replace(actionName,actionName+"_NameSearch");
document.forms[0].action = url;
document.forms[0].submit();
};
}
The javascript forms a url by adding "_NameSearch" to the action
of the form calling the selectNameInfo()
function. In this case the new action is newRequest_NameSearch
which goes to the following xml which calls the generateNameList()
method of the action class without using DMI which was my original question.
<action name="newRequest_NameSearch" class="gov.mo.dnr.egims.controller.evaluation.NewRequestAction" method = "generateNameList">
<result name="success" type="tiles">newRequest</result>
<result name="nameSearch" type="tiles">selectNameInfo</result>
<result name="error" type="tiles">error</result>
</action>
本文标签: struts2How to call a method in Struts 2 Action class with JavaScriptStack Overflow
版权声明:本文标题:struts2 - How to call a method in Struts 2 Action class with JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742213246a2434103.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论