wjhplano
wjhplano

Reputation: 591

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. 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?

say I have a jsp page

<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";  
}}

Upvotes: 1

Views: 3451

Answers (2)

Yagnesh Agola
Yagnesh Agola

Reputation: 4639

You need <form>...</form> in JSP page, give your action name in action="..." parameter in form tag element.

Write onclick function for button and submit your form in that function.

<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>

Upvotes: 3

Darshan Mehta
Darshan Mehta

Reputation: 30809

You need to define a form in your JSP and on clik, call javascript to submit it as shown below:

document.getElementById("myForm").submit()

You can also give it any action using script. That action should be mapped in struts configuration file.

Upvotes: 1

Related Questions