Executing a function in Struts using javascript: A Step-by-Step Guide

I am trying to trigger a method in the action file from a java script and here is the code that I currently have:

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

Here is the corresponding javascript part:

<script language="javascript">

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

</script>

This allows me to call the execute method. However, I need to be able to call different methods. For instance, instead of execute(), I want to call test(). But I am unsure how to achieve this. One way I can handle calling different methods is by using buttons like this:

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

How can I call the test() method through the onclick event of the image?

Answer №1

When discussing the action of "calling a method" and your intentions with the results, there are various approaches to consider.

If you intend to utilize an image as a hyperlink, leveraging action mappings with the "method" attribute set to the appropriate method is one option.

By enabling dynamic method invocation, appending the method name to the URL using "!test" becomes feasible. This functionality can also be managed by the <s:url> tag through its "method" attribute.

In cases where page refresh is undesirable, incorporating Ajax is necessary. This can be achieved through the utilization of jQuery plugin, the deprecated Dojo plugin, or via a JavaScript framework in its raw form.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sharing Variables with $(document).ready

I need help understanding why this code is not functioning and how I can fix it. Despite my efforts to use namespaces and IIFEs, I am still unable to make it work. $(document).ready(function() { alert (hi); }); $(document).ready(function() { var hi = ...

Updating the background of a div in HTML through the power of JavaScript

I am having trouble changing the background of a DIV. I believe my code is correct, but it doesn't seem to be working. I suspect the error lies with the URL parameter, as the function works without it. Here is my code: <script language="JavaS ...

Navigating Angular: Discovering Route Challenges in Less Than an Hour

Can someone take a look at my code and help me out? I'm trying to learn Angular.js by following the popular Angular.js in 60 minutes video tutorial, but it seems like things have been updated since then. I'm having trouble getting my routes to wo ...

What is the best way to incorporate the information from my CSV file onto my website?

I am currently working on a project where I need to extract data from a CSV file and display it on my website. The idea is to use two values as x and y coordinates, similar to a graph, and retrieve the data at that specific location. Here is the HTML code ...

Creating a time-based scatter plot using chart.js

As I attempt to visualize a scatter plot using chart.js with (x,y) data where x represents a date string, I have noticed that most tutorials and examples available online utilize a function to generate a timestamp for demonstration purposes. However, I am ...

Reveal the functions of one module to another module

I have a node application that needs to retrieve the path to a folder and read all the files within it. For example: moduleA -server.js -controller --load.js Within load.js, there is a method (loadFolderFiles) which takes a file path as input and ...

Issue with jQuery hide() function in Internet Explorer 9

I need help with creating a hidden div that becomes visible when a user clicks a link. The code I have works in FF / IE7 / IE8 but not in IE9, where the div is always visible without any content. Any advice is appreciated! <script> $(document).r ...

Issue with the login functionality on Express.js/Node.js endpoint needs to be resolved

I have developed a basic CRUD application where users can register and login. The registration functionality in general.js is functioning properly, but I am facing an issue with the login process. Upon attempting to login, the response message received is: ...

The Ajax div refresh is displaying outdated information even after being refreshed

Hey there! I'm currently dealing with an issue related to ajax refresh and I could really use some troubleshooting tips. The problem I'm facing is that when the div initially loads, it displays the latest information correctly. However, upon refr ...

When utilizing Nettuts' CSS navigation resource, the bottom shadow will disappear if the parent element contains child elements

I'm currently working on customizing a solution I stumbled upon on Nettuts to fit my specific requirements. Everything seems to be functioning well, except for a peculiar issue I'm encountering while hovering over a top-level navigation element t ...

The most effective method for initializing the "db" variable in NodeJS using MongoDB

I've been diving into the world of NodeJS / Express / MongoDB and have come across various tutorials on the subject. One tutorial that caught my eye was by Christopher Buecheler, in which steps 5 and 6 were particularly interesting: Step 5 in app.js ...

scrolling element resembling an iPad

Currently in search of a css/js component that resembles iscroll, but one that is compatible with non-webkit browsers as well. To elaborate, I am in need of the following features: Sleek scrollbar Scrollbar that only appears on drag and/or hover Abilit ...

Vue 3 project experiencing issue with Bootstrap 5 tabs failing to update upon tab click

Currently, I'm in the process of developing a vue3 application with bootstrap 5. As part of this project, I am implementing tabs. Although I can successfully click on the tabs, I have encountered an issue where the tab-content remains fixed on the ini ...

I aim to display interconnected information from various APIs in a cohesive manner

I am working with two APIs: component.ts ngOnInit(): void { this.getQueryCountriesList().subscribe(arg => { this.countryDatas = arg; }); this.getQueryNights().subscribe(obj => { this.nightDatas = obj; }); ...

Steps to have index.html display when running the build command in a Svelte project:

Greetings everyone, I'm brand new to using Svelte and have a question that's been on my mind. I recently developed a small app in Svelte that works perfectly fine during development. However, when I run npm run build for production, the output ...

The live() function is causing issues with my ajax request

Within my webpage, I have a link with an onclick() event that should display a div containing a date input text named "datepicker0", followed by another div with the id of "bContent". I've included the script below to implement a date filter on the d ...

Is there a way for me to determine the number of occurrences of span elements with the same ID on my page

Can someone help me with this HTML code challenge? <span id="post"></span> <span id="post"></span> <span id="post"></span> <span id="post"></span> <span id="post"></span> <span id="post">&l ...

Tips for eliminating the empty element from a JavaScript array

Here is the code implementation I am working with: Array.prototype.abc = function(condition, t){ var arr = []; for( var i = 0; i < this.length; i++){ arr.push(condition(this[i],t)); } return arr; }; var a = [1,2,3,4]; ...

How can I extract the parameters stored in a JSON object inside a function() call using RegExp.$?

I am attempting to extract a set of parameters for RegExp.$ (also known as JavaScript f-strings) from a JSON file. const schema = require('../schema.json').Flights; class flightRepo{ keys = Object.keys(schema); create(db) { r ...

Is there anyone who can provide insight on replicating the hover effect showcased on this webpage?

Is there a way to replicate the hover effect seen on the images of this page - ? When you hover over an image, a larger picture appears within a bordered window. Any suggestions on how to achieve this effect would be greatly appreciated! ...