如何使用javascript调用Struts2 Action Class方法中的方法

我们目前使用以下 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>

然后转到 Action 类 NameActionexecute() 方法,我必须检查表单是否是从 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.

我更愿意直接从 javascript 调用 NameAction 中的 findName() 方法.换句话说,我希望 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 ;

相关文章