0

I would like to run method over my Java object in Freemaker template but in Javascript. Could be also directly in Freemarker but I need to run it on click.

I have issue that following method doesnt run:

actual_id.setActual_id(variable) ;

And I have following Java code:

Setting actual_id= new Setting("-");

Map<String, Object> data = new HashMap<>();
data.put("items", items);
data.put("actual_id", actual_id);

public Setting(String actual_id) {
    this.actual_id = actual_id;
}

public String getActual_id() {
    return actual_id;
}

public void setActual_id(String actual_id) {
    this.actual_id = actual_id;
}

This is my Freemaker template:

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>function myFunction(variable) {
alert(variable);
actual_id.setActual_id(variable) ;
location.reload();}
</script>

<#list items as item>
<p>${item.name}: ${item.id} <button type="button" id="${item.id}" onclick=myFunction("${item.id}") >Details</button>
    </#list> 

EDIT1:

I am also trying something like this:

onclick="${actual_id.setActual_id(item.id)}"

but cannot trigger activity from onclick.

EDIT2:

I already almost found solution. Following code executes Java method, I just need to figure out how to quote "variable" to load javasript value into it.

function myFunction(variable) {
    alert(variable);
    var idd ="${actual_id.setActual_id(variable)}";//here variable value needed
    alert(idd);
    location.reload();
}
jokuja
  • 69
  • 13
  • Can you tell me what is the issue you are facing? – soorapadman Oct 15 '18 at 12:55
  • I made some progress, put it into EDIT2 and now I just need to get value of the javascript variable within freemaker expression. – jokuja Oct 15 '18 at 13:02
  • Look like what you have done is correct .Did you get values in the map? – soorapadman Oct 15 '18 at 13:09
  • Yes everything works, just I need to get String value from variable "variable" in the freemaker expression: "${actual_id.setActual_id(variable)}" -> here "variable" is recognized as freemaker variable but I need it as javascript variable. – jokuja Oct 15 '18 at 13:16
  • You can't do `variable` because there is no such thing . – soorapadman Oct 15 '18 at 13:23
  • So how could I load javascript variable into freemaker variable? Something like this: – jokuja Oct 15 '18 at 13:30
  • Your problem is items is in the list so you have to pass dynamically . In js actualid pass like this `${actual_id}.setActual_id(variable)` and `onclick=myFunction('${item.id}')` hope this is sufficient – soorapadman Oct 15 '18 at 13:34
  • Thanks, now it generates following html but doesnt execute js function at all: var idd = urlpackage$Settinga@2c6e22c6.setActual_id(variable); – jokuja Oct 15 '18 at 14:19
  • It's because you have not having string. Perhaps you can check what setting objects contain – soorapadman Oct 15 '18 at 14:36
  • Whats not String? my "variable" is a string. Also "setActual_id sets" sets variable "object.actual_id" which is a string and it also returns string. :D – jokuja Oct 15 '18 at 14:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181930/discussion-between-jokuja-and-manfromnowhere). – jokuja Oct 16 '18 at 07:47

1 Answers1

1

Here is a quote from the Apache FreeMarker Project front-page: (https://freemarker.apache.org/)

What is Apache FreeMarker™?

Apache FreeMarker™ is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data. Templates are written in the FreeMarker Template Language (FTL), which is a simple, specialized language (not a full-blown programming language like PHP). Usually, a general-purpose programming language (like Java) is used to prepare the data (issue database queries, do business calculations). Then, Apache FreeMarker displays that prepared data using templates. In the template you are focusing on how to present the data, and outside the template you are focusing on what data to present.

Figure [Photo/Image Not Posted]

This approach is often referred to as the MVC (Model View Controller) pattern, and is particularly popular for dynamic web pages. It helps in separating web page designers (HTML authors) from developers (Java programmers usually). Designers won't face complicated logic in templates, and can change the appearance of a page without programmers having to change or recompile code.

While FreeMarker was originally created for generating HTML pages in MVC web application frameworks, ** it isn't bound to servlets or HTML or anything web-related.** It's used in non-web application environments as well.

https://freemarker.apache.org/

I, myself, program Java & JavaScript web-servers on Google Cloud Server all day long. The only way to make a JavaScript function talk to a Java Function is through an HTTP GET / POST call to a Java-Servlet or, also, an old-school JSP Page. Though it says (explicity) right on the top-level domain page of the website that "Freemarker is not bound to Servlets" - that actually means the software classes / package does not have to run inside of a web-environment at all - perhaps on your desktop computer without a web-browser.

What I do know with an extremely high degree of certainty is that communication between the client (on a web-browser) and a server (a web-server) is always done through HTTP GET / POST requests. JSON, AJAX are common for higher communicating large amounts of data. If you expect a JavaScript method to make a call to a Java Class on the back-end, you will need to include a Servlet or JSP class - and the whole 9 yards to boot.

NOTE: I have not used Apache FreeMarker, but I program Java/JavaScript all day long. Judge accordingly! According to the Apache web-site, FreeMarker is of assistance in "programatically or automatically generating the HTML for pages" (which is what C# is good at) - which is actually something I do for my web-site, often, but (alas!) I don't use Apache's product. What that means is FreeMarker can help the generating of HTML more efficiently using Java Classes on the back-end server side ...

But the rules of how Java and Java-Script communicate have not changed...

Long story short - you must include JavaScript calls such as: