Kashiii
Kashiii

Reputation: 47

How to call java function using onclick event in <a> tag from jsp page

I want to call a java function named setrowNumber(1), that sets the row number to a static int variable. And I want this function to be called when the link is clicked but that does'nt seem to happen.

The problem is in that part only because when i try to set the value manually it works fine.

<a   href="single.jsp" onclick= "<%conc.setrowNumber(1);%>" > <i class="glyphicon glyphicon-menu-right icon"  ></i></a>

I am making java web application on struts framework, using net beans and wamp server.

If I use javascript function and use it with onclick event then how can I pass value to java function.e.g

<a   href="single.jsp" onclick= "myFunc(1)" > <i class="glyphicon glyphicon-menu-right icon"  ></i></a>


<script type="text/javascript">
function myFunc (int a) {
<% conc.setrowNumber(a); %> //how to write java function and pass value here because a is not known var here.         
}
</script>

Upvotes: 1

Views: 12414

Answers (4)

Mayur Jain
Mayur Jain

Reputation: 9

Include the class which has got this function in your jsp like this

<%@page import="package.subPackage.TheClassName"%>

In the scriptlet tag you can invke the method.

<% TheClassName theClassInstance = new TheClassName(); theClassInstance.doSomething(); %>

If its a static method you don't need to create instance .

Upvotes: -1

Rajeev Ranjan
Rajeev Ranjan

Reputation: 549

Try this. <s:url id="yourId" action="yourActionName"></s:url> <s:a href="%{#yourId}"><img src="<s:url value="/images/submit.png" /> </s:a> Make a link on jsp and through that url you can call the Action.

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109557

That is normally done with an AJAX call in JavaScript, in asynchrone mode: you send to the server, and possibly register a JavaScript function to call back. A web search will give examples. In your case. a link, you could assign to the href location an url with the parameter.

this.href.url = "single.jsp?a=" + a;

Upvotes: 0

Vickyexpert
Vickyexpert

Reputation: 3167

try below code

        <a   href="#" onclick= "<% conc.setrowNumber(1); %> return false;" > <i class="glyphicon glyphicon-menu-right icon"  ></i></a>

Upvotes: 0

Related Questions