1

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>
2
  • 1
    An onclick event handler is executed in the browser. Normally this would be JavaScript. JSP scriptlets are executed on the server before the page is sent to the browser. You could have a scriptlet that generates some JavaScript and embeds it in the page as an onclick handler, or you could have an onclick handler that sends an XmlHttpRequest to a servlet or JSP to execute some function on the server. But your current approach doesn't make sense. Commented Jun 20, 2016 at 4:30
  • I edited the question please check again. Commented Jun 20, 2016 at 4:44

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

0

try below code

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

2 Comments

Returning false won't load the page.
yep if you need to call java function then it should not reload the page till code execute then you can call js method to reload the page
-1

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 .

2 Comments

I have done this, but my problem is that I want to call the function on Onclick event and pass parameter but I can't understand how.The function is not being invoked.
why do people write garbage answers like this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.