問題描述
我們目前使用以下 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();
調(diào)用以下 Struts2 動作
which calls the following Struts2 action
<action name="myAction" class="project.location.NameAction">
<result name="success" type="tiles">myAction</result>
</action>
然后轉(zhuǎn)到 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 調(diào)用 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" >
推薦答案
實現(xiàn)你想要的有不同的方法,但可能更簡單的是將不同的動作映射到同一個動作類文件的不同方法,例如.帶注釋:
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 ;
這篇關(guān)于如何使用javascript調(diào)用Struts2 Action Class方法中的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!