dafodil dafodil - 1 year ago 65
Java Question

calling controller onclick in jsp

I am using spring web application,in jsp file i have to call controller in java file using ajax function,how could i call controller in java file from jsp file.

<p class="bottom-slide-corners">
<a class="billing" href="#Billing"><spring:message code="billing_area" /></a>

$('.billing').on('click', function(event) {
var $this = $(this);
var $linkToFind = $($this.attr("href") + "_billing");
var $slidesToFind = $("." + $this.attr("href").replace("#", "") + "_slide");

if($this.parent().parent().siblings('.current-arrow').find('img').is(":visible")) {
$('.Background').slideUp(function() {
return false;

if($window.width() <= 767) {
$('#dashboard-mobile-banner, #header-bg, #footer-container, .container-slider').slideUp();

var categoryClass = $linkToFind.attr('id').replace("learnMore", "slide");
$('.courseDashboard').removeClass().addClass("courseDashboard Background " + categoryClass);
$('body, html').animate({ scrollTop: 0 }, "fast");

if($('.learn_more').is(":visible")) {
$('.Background').slideUp(function() {
} else {
$('.Background').slideDown(function() {
if ($window.width() <= 767) {
var slider = $("#" + $linkToFind.attr('id') + " .thumbview").bxSlider({
slideWidth: 300,
pager: false
$('.close-panel:visible').on('click', function(e) {

$('.current-arrow img:visible').slideUp();
$slidesToFind.find('.current-arrow img').slideDown();
return false;

here on click i want to call a controller in .java file,how could i call this below controller in jsp file code,on click

@RequestMapping(value = "/billing", method = RequestMethod.POST)
public String Billing(@RequestParam Long caseId,
@RequestParam Long noteID, HttpServletRequest request) throws Exception {

logger.debug("success ");
return "success";
} catch (Exception e) {
throw e;

Answer Source

You can do it like this ...it will 100% work :) .

    $.ajax({url:"/billing", success: function(result){


Return your success string in map given by JSONObject , keep your key as "result" and "success" as your String .Something like this you write in your controller

`JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("result", "success");
String myResult= jsonObject1.toString();
return myResult;

As you can see i have added resp.getWriter() . It sends response back to your controller. So also please add a response Object HttpServletResponse resp in your method public String Billing method like your have added HttpServletRequest request.This will 100% work . Let me know if that helps !! :)