0

In my .jsp page I have a button that calls a function in the controller. Clicking this button will initiate a download.

<input type=button onClick="location.href='reporting/GroupbasedPreset?rows=24'" value='Last 24 Hours'>

When this is clicked, the controller makes some database calls and comes back with a result. What I want to do is if the result is empty, have a javascript popup appear rather than starting a download. Is there a way to call javascript from within a spring controller?

Basic idea of what I want the controller to do.

int rows = makethedbcall();
if (rows == 0) {
   javascript popup;
   return;
} else {
   continue with download;
}
4
  • That's not how it works. Javascript executes on the client while your java executes on the server. Look into AJAX. Commented Mar 30, 2013 at 0:51
  • Right, I was wondering if there was a way for the controller to tell the client to execute some javascript? Commented Mar 30, 2013 at 1:02
  • 2
    What you would need is two controller methods. You make an ajax request to the first asking if it's alright to donwload, and then make a second request (either ajax or regular) to actually download the file. Commented Mar 30, 2013 at 1:12
  • If you are using ajax, follow @SotiriosDelimanolis's solution. If not, when you are about to render your "Last 24 Hours" button, do the db lookup and if there are records in the "Last 24 Hours", render the button otherwise render something like "nothing in Last 24 Hours" (as may be a disabled button). Commented Mar 30, 2013 at 2:11

1 Answer 1

1

One way I can think of is, return a JSP from controller when the result is empty.

Controller code would then have:

if (rows == 0) {
// Assuming result-empty would resolve to result-empty.jsp
   return "result-empty";
} else {
   continue with download;
}

JSP (result-empty.jsp) could just have a script tag something like this:

<script>
    window.onload = function() {
       alert("Result is empty");
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

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.