javascript 如何使用struts2框架在javascript中调用动作类中的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12203415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:35:04  来源:igfitidea点击:

how to call method in action class in javascript using struts2 framework?

javascriptstruts2

提问by Soham

I am trying to call a method in action class in javascript function on button click event which is in jsp.I am using struts 2.could you tell me how to do this? There is a function in ListAlgorithmAction Class which I want to call when the user clicks submit button.

我试图在jsp中的按钮单击事件上调用javascript函数中动作类中的方法。我正在使用struts 2.你能告诉我怎么做吗?ListAlgorithmAction 类中有一个函数,我想在用户单击提交按钮时调用它。

function alertselected (){
    var x = document.getElementById ( "select_name" ).selectedIndex;
    var y = document.getElementById ( "select_name" ).options;
    var id = y [x].index;
    redirect(id);
}

function redirect(x){
    document.getElementById ( "param_ID" ).value = x;
    document.forms ["./ListAlgorithmAction"].submit ();
}

回答by Umesh Awasthi

I do not think this is something related to Struts2 ,since struts2 is independent of the way you call action class be it by java-script form submit,ajax or by simple form submit.

我认为这与 Struts2 无关,因为 struts2 独立于您调用操作类的方式,无论是通过 java 脚本表单提交、ajax 还是通过简单的表单提交。

i believe you have some form in your jsp like

我相信你的jsp中有某种形式

<s:form action="myAction" name="myForm">
  some data

</s:form>

on click you can do something like

点击你可以做类似的事情

function redirect(x){
    document.getElementById("param_ID").value=x;
    document.myForm.action="ListAlgorithmAction";
     document.myForm.submit();
    }

This is just solution based on your inputs and there can be many more if you able to define your problem more

这只是基于您的输入的解决方案,如果您能够更多地定义您的问题,那么可能还有更多

回答by KD.

For similar implementations we used to use http://directwebremoting.org/dwr/index.html(DWR). You can try that.. if you have big implementation i would suggest you to integrate this framework

对于类似的实现,我们曾经使用http://directwebremoting.org/dwr/index.html(DWR)。你可以试试..如果你有很大的实现,我建议你集成这个框架