0
votes

I have a freemarker template with javascript in it, and I'm using spring mvc to pass in a java object "emailer".

Somehow, in this freemarker template, I want to call the emailer object's "sendEmail(params, ..)" method from javascript within the freemarker template. I know how to call java methods from freemarker (the regular way - for example: How do I call java methods on an object from a FreeMarker template?) , but I don't know how to do it from within the javascript.

Is this even possible? If so, how? And if it's not, what are some alternatives?

The overall goal was to get a value from a dropdown list (using javascript), and then using that value in the java method that gets called when a button is pressed.

Thanks in advance! If more info is needed, I'd be happy to provide it.

1

1 Answers

1
votes

It's not the only possible combination of how these technologies would work together, but in the usual flow of things, what you're looking forward wouldn't be possible:

  • A Java call (mediated with Spring) renders the FreeMarker, allowing calls back into Java code as it processes.
  • This rendered string (which might happen to contain some Javascript) is shipped over HTTP to the client browser. At this point the Java execution has completed.
  • In the browser, the generated text is parsed, and the Javascript is run. Here there is no direct knowledge of the server, and there is no way to call back that completed thread of control.

So unless you're doing something more unusual, no you can't do what you're suggesting.

There are tools to allow client-side Javascript to call back to the server and to interact with Java there.; so you can rig something up. But you will not simply call the Java directly without more work.