javascript - 在 Freemarker 模板中的 Javascript 中运行 Java 方法

标签 javascript java html freemarker

我想在 Freemaker 模板中但在 Javascript 中对我的 Java 对象运行方法。也可以直接在 Freemarker 中,但我需要点击运行它。

我遇到以下方法无法运行的问题:

actual_id.setActual_id(variable) ;

我有以下 Java 代码:

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;
}

这是我的 Freemaker 模板:

<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> 

编辑1:

我也在尝试这样的事情:

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

但无法通过 onclick 触发 Activity 。

编辑2:

我已经几乎找到解决方案了。下面的代码执行Java方法,我只需要弄清楚如何引用“变量”来将javasript值加载到其中。

function myFunction(variable) {
    alert(variable);
    var idd ="${actual_id.setActual_id(variable)}";//here variable value needed
    alert(idd);
    location.reload();
}

最佳答案

以下是来自 Apache FreeMarker 项目首页的引用: (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/

我自己整天在 Google Cloud Server 上编写 Java 和 JavaScript Web 服务器。让 JavaScript 函数与 Java 函数对话的唯一方法是通过 HTTP GET/POST 调用 Java-Servlet 或老式 JSP 页面。尽管它在网站的顶级域页面上(明确)说“Freemarker 不绑定(bind)到 Servlet”——这实际上意味着软件类/包根本不必在 Web 环境中运行——也许在没有网络浏览器的台式计算机上。

我非常确定地知道客户端(网络浏览器上)和服务器(网络服务器)之间的通信始终通过 HTTP GET/POST 请求。 JSON、AJAX 常用于高级通信大量数据。如果您希望 JavaScript 方法调用后端的 Java 类,则需要包含 Servlet 或 JSP 类 - 以及整个 9 码来启动。

注意:我没有使用过 Apache FreeMarker,但我整天都在编写 Java/JavaScript 代码。据此判断!根据 Apache 网站,FreeMarker 有助于“以编程方式或自动生成页面的 HTML”(这是 C# 所擅长的)——这实际上是我经常为我的网站所做的事情,但是(唉) !)我不使用 Apache 的产品。这意味着 FreeMarker 可以帮助在后端服务器端使用 Java 类更有效地生成 HTML ...

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

长话短说 - 您必须包含 JavaScript 调用,例如:

关于javascript - 在 Freemarker 模板中的 Javascript 中运行 Java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52812470/

相关文章:

java - 即使兼容类型可用,方法重载是否隐式类型转换为更大的类型

javascript - 使用 Mongoose promise /异步等待,返回空数组

javascript - 在此示例中,为什么 window.getSelection() on focus 在 Chrome 中不起作用?

javascript - Puppeteer 中的异步/等待等待页面加载

java - Google App Engine 中常驻后端实例的可用性

java - 没有域关联的 java 包名称的约定是什么?

jquery - 通过 DOM 层传递点击事件

javascript - 单击按钮更改复选框颜色,一次从上到下淡入淡出

javascript - 限制点击按钮的次数

javascript - 基于条件的数组操作JS