如何在Struts2中调用一个方法使用javascript的Action类方法 [英] How to call a method in Struts2 Action Class method with javascript
问题描述
我们目前使用以下javascript来在其中一个字段值更改时提交表单。
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();
调用以下Struts2操作
which calls the following Struts2 action
<action name="myAction" class="project.location.NameAction">
<result name="success" type="tiles">myAction</result>
</action>
然后转到 execute()
Action类 NameAction
的方法,其中我必须检查表单是否从javascript提交。
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.
我想在 NameAction $ c>中调用
findName()
$ c>直接从javascript。换句话说,我希望javascript的行为像下面的jsp代码。
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" >
任何帮助将不胜感激。
推荐答案
有不同的方法来实现你想要的,但可能更简单的是将不同的动作映射到同一个动作类文件的不同方法,例如。有注释:
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:
public class NameAction {
@Action("myAction")
public String execute(){ ... }
@Action("myActionFindName")
public String findName(){ ... }
}
或使用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>
然后在javascript:
Then in javascript:
var url = "project/location/myActionFindName.action?name="+ lname ;
这篇关于如何在Struts2中调用一个方法使用javascript的Action类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!