如何使用javascript调用Struts2 Action Class方法中的方法 [英] How to call a method in Struts2 Action Class method with javascript

查看:32
本文介绍了如何使用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 ;

这篇关于如何使用javascript调用Struts2 Action Class方法中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆