繁体   English   中英

如何在我的 javascript 文件中使用来自 message.properties 的 Thymeleaf 消息?

[英]How to use a Thymeleaf message from message.properties inside of my javascript file?

很直接的问题。 我想知道是否有任何功能允许我从 message.properties 文件中获取消息并将其插入到我的 javascript 文件中,就像使用 html 文件一样。

例如,我的主页上有一个标题,列为:

<h1 th:text="#{home.screen.title}"></h1>

home.screen.title = 主页

在我的 javascript 文件中,我有一个 function 接受两个字符串,我希望将它们作为这些 thymeleaf 消息。 所以,它有点像:

statusCode: {
       404: function() {
           PISAlert(th:text="#{encounter.error.notFound}", th:text="#{encounter.error.notFound.content}");
       }
   }

其中遇到.error.notFound.title = 遇到404错误。 并且未找到遇到.error.notFound.content = Object

我可以看到有两种方法可以实现这一目标 - 但它们都对您问题的更广泛背景做出了一些假设。 如果这些假设是错误的,那么这些方法可能不起作用。

选项 1 的 2

将 Thymeleaf 提供的参数从 Thymeleaf 模板传递给您的 function(位于单独的 JS 文件中)。

这简化了解决方案。 假设(与您的问题不同)是您仅在 Thymeleaf 模板中调用此 function - 因此您不需要将消息字符串直接呈现到 JS 代码中(在其单独的 JS 文件中)。

例子:

我使用以下消息文件 - jsdemo.properties:

demo.error1=Error message one
demo.error2=Error message two

这是我的示例中的 JS 文件 - js_demo.js:

function getErrorMessagesA(msg1, msg2) {
  console.log('parameter A1 = ' + msg1);
  console.log('parameter A2 = ' + msg2);
}

这是调用getErrorMessagesA的 Thymeleaf 模板:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>JS Demo</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
        <script src="js/js_demo.js"></script>
    </head>
    <body>
        <h2 id="derf">JS Demo</h2>
    </body>

    <!-- option 1: call the function in an external script with parameters: -->
    <script th:inline="javascript">
        $(document).ready(function() {
            getErrorMessagesA( [[#{welcome.error1}]], [[#{welcome.error2}]] );
        });
    </script>

</html>

Thymeleaf 模板使用[[#{...}]]语法将 Thymeleaf 变量嵌入到 JavaScript 中。 请参阅表达式内联

渲染 web 页面时,控制台显示如下两条消息:

parameter A1 = Error message one
parameter A2 = Error message two

选项 2 的 2

这使用了一种不同的方法,其中 JS 作为片段添加到 HTML 模板中(意味着它可以在不同的模板中重复使用。在这种情况下,调用 function 时不带参数。

片段使用这个嵌入到主模板中(它替换了上面代码中的“选项 1”部分):

    <!-- option 2: process the function as a Thymeleaf fragment: -->
    <th:block th:replace="jsdemo_jscode.html :: js_fragment_1" ></th:block>
    <script th:inline="javascript">
        $(document).ready(function() {
            getErrorMessagesB();
        });
    </script>

Thymeleaf 片段文件:

<th:block th:fragment="js_fragment_1">

    <script th:inline="javascript">
        function getErrorMessagesB() {
            console.log('parameter B1 = ' + /*[[#{demo.error1}]]*/ '[msg not found]');
            console.log('parameter B2 = ' + /*[[#{demo.error2}]]*/ '[msg not found]');
        }
    </script>

</th:block>

这使用了 Thymeleaf: /*[[#{demo.error1}]]*/自然模板语法,以确保 JavaScript 有效。 还要注意th:inline="javascript"指令。

渲染 web 页面时,控制台显示如下两条消息:

parameter B1 = Error message one
parameter B2 = Error message two

这里的主要区别在于片段中对 JS 的调用没有参数 - 它只是getErrorMessagesB(); .

选项 3 的 2

理论上还有第三种选择——但我从来没有这样做过。 我认为这会很复杂。

您可以在 Thymeleaf 模板中使用无参数调用getErrorMessagesB(); - 但不是使用选项 2 中的 JS 片段,而是使用选项 1 中的外部 JS 文件。

在这里,JS 将如下所示:

function getErrorMessagesB() {
    console.log('parameter B1 = ' + /*[[#{demo.error1}]]*/ '[msg not found]');
    console.log('parameter B2 = ' + /*[[#{demo.error2}]]*/ '[msg not found]');
}

这样做的复杂性在于,除了 HTML 文件之外,您还需要处理该文件,但要单独处理该文件,并使其可用于 HTML 文件。 我使用过文本模板,但从来没有依赖于相关的 HTML 模板。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM