Reputation: 407
Is it possible to call a method in Java by button onclick in JSP?
For example:
Java:
@UrlBinding("/Student/import.action")
public class StudentAction implements ActionBean {
public void testMe() {
//do something
}
}
JSP
<stripes:form beanclass="com.example.action.StudentAction">
<input type="button" value="Test" onClick="call testMe"/>
</stripes:form>
I read some posts on Internet that it could be done by Ajax/jQuery, but I couldn't understand how they do it. I just know itshould be something like this:
$.ajax({
type:"POST"
url:
})
or
$(document).ready(function() {
$("#button").click(function(){
})
})
is there any other way to do this, and if no, I would appreciate a simple explanation of how to do it with Ajax/JQuery.
Note: I don't want to use submit input or stripes:submit!
Upvotes: 0
Views: 6461
Reputation: 16051
If you have an ActionBean
(which is a servlet deep down), you can bind an URL to it with @UrlBinding
(as you already do). You just need to make a HTTP request to that URL using Ajax in the 'click'
event handler of the button.
So you could have this markup:
<stripes:form beanclass="com.example.action.TestMeActionBean">
<input type="button" value="Test" onClick="testMe()"/>
</stripes:form>
<script>
function testMe () {
$.ajax({
type: 'post',
url: 'action/testMeAction',
...
});
}
</script>
On the server side, you just need to create the handling action:
@UrlBinding("/action/testMeAction")
public TestMeActionBean implements ActionBean {
After this, you only need to write a handler method. For example, using your method declaration:
@DefaultHandler
public void testMe() {
//do something
}
}
Upvotes: 0
Reputation: 5023
It will not call Java method, you have to write Servlet
then call the doGet()
or doPost()
method of Servlet
on form submission or using ajax
call. refer this example
Upvotes: 1