Khashayar
Khashayar

Reputation: 2034

How To Call a Method in Struts with javascript?

I want to call a method in the action file from a java script so this is the code that i have

<img src="image/travel.jpg" alt="test" onclick="javascript:doABC();"/>

and the javascript part is :

<script language="javascript">

function doABC(){
    var form = document.forms["Welcome"];
    form.action = "Welcome.action";
    form.submit();
}

</script>

like this I can call the execute method! but I have different methods that I want to call them for example I want to call test() instead of execute!! but I dont know how to include this! I can handel calling different methods with buttons like this :

<s:submit method="execute" value="Enter The Site" align="center"/>  
<s:submit method="test" value="Enter The Site" align="center"/> 

what should I do if I want to call test() through the onclick of the image?!

Upvotes: 2

Views: 4658

Answers (1)

Dave Newton
Dave Newton

Reputation: 160181

It depends on what you mean by "call a method", and what you want to do with the results of that call.

If you want to use an image as a link, you can use action mappings with their "method" attribute set to the appropriate method.

If you enable dynamic method invocation, you can append the method name to the URL by using "!test". This is also handled by the <s:url> tag via its "method" attribute.

If you don't want to reresh the page, then you need to use Ajax, e.g., the jQuery plugin, the Dojo plugin (long-deprecated), or by using a JavaScript framework "natively", i.e., by writing plain old JavaScript.

Upvotes: 3

Related Questions