How to call a method in Struts2 Action Class method with javascript(如何使用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 类 NameAction
的 execute()
方法,我必须检查表单是否是从 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方法中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用javascript调用Struts2 Action Class方法中的方法


基础教程推荐
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01