[英]simple way to call struts2 action
I need to test certain struts2 action classes in struts and I want to use just a regular old button. 我需要在struts中测试某些struts2动作类,我想只使用一个普通的旧按钮。 not necessarily a submit button, just a button. 不一定是提交按钮,只是一个按钮。 What would be the simplest way for me to tie an action class to a click event? 将动作类绑定到click事件的最简单方法是什么?
say I have a jsp page 说我有一个jsp页面
<html>
<head>
</head>
<body>
<form>
<button id="buttonId">imAButton /button>
</form>
</body>
</html>
and an action class 和一个动作类
public class ActionClass1 {
public String execute(){
System.out.println("yes this test is juvenile but it works!");
return "iWork";
}}
You need <form>...</form>
in JSP page, give your action name in action="..."
parameter in form tag element. 您需要在JSP页面中使用<form>...</form>
,在表单标记元素中的action="..."
参数中给出您的操作名称。
Write onclick function for button and submit your form in that function. 为按钮编写onclick函数并在该函数中提交表单。
<form action="yourActionName" id="testForm">
<!--other form elements-->
<button name="btn" id="btn" onclick="myFunction()"></button>
</form>
<script>
//Your JavaScript function.
function myFunction()
{
// submit your form here by JavaScript or JQuery.
//by JavaScript
document.getElementById("testForm").submit();
//by JQuery
$("#testForm").submit();
}
</script>
You need to define a form in your JSP and on clik, call javascript to submit it as shown below: 您需要在JSP和clik中定义表单,调用javascript以提交它,如下所示:
document.getElementById("myForm").submit()
You can also give it any action using script. 您也可以使用脚本为其提供任何操作。 That action should be mapped in struts configuration file. 该操作应该在struts配置文件中映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.