dafodil dafodil - 5 months ago 16
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>
</p>


$('.billing').on('click', function(event) {
clearSliderInterval();
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")) {
$this.parent().parent().siblings('.current-arrow').find('img:visible').slideUp();
$('.Background').slideUp(function() {
$(".learn_more").hide();
}).removeClass("open");
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() {
$('.learn_more').hide();
$linkToFind.show();
$('.Background').slideDown();
});
} else {
$linkToFind.show();
$('.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) {
slider.destroySlider();
});
}
}).addClass("open");
}

$('.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 {
try{

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

Answer

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

(".billing").on('click',function(){
    $.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;
response.getWriter().write(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 !! :)