Thymeleaf内联JavaScript完全被忽略。

huangapple go评论72阅读模式
英文:

Thymeleaf inline JavaScript is completely ignored

问题

以下是您要翻译的代码部分:

<!doctype html>
<html th:replace="~{fragments/layout :: layout(~{::title}, ~{}, ~{}, ~{::main})}">
    <head>
        <title>Reports Page</title>
    </head>     
    <body>
        <main role="main" class="flex-shrink-0">
            <!-- some HTML code here -->
            <select class="form-select" id="year" aria-label="Year dropdown" th:onchange="reloadPage()">
                <option 
                    th:each="yearRow : ${years}" 
                    th:value="${yearRow}" 
                    th:selected="(${yearRow} == ${year})"
                    th:text="${yearRow}">
                </option>
            </select>
            <!-- other HTML code here -->
        </main>

        <script th:inline="javascript" type="text/javascript">
            function reloadPage() {
                alert("You selected a year");
            }
        </script>
    </body>
</html>

希望这能帮助您。如果您有其他翻译需求,请随时提出。

英文:

I'm trying to include a basic vanilla JS script into a Thymeleaf view but it seems to be completely ignored - I don't even see it when I inspect the page. I assume the issue is related with the fact that the view is part of a layout template but I cannot figure out where the problem is.

The View:

&lt;!doctype html&gt;
&lt;html th:replace=&quot;~{fragments/layout :: layout(~{::title}, ~{}, ~{}, ~{::main})}&quot;&gt;
    &lt;head&gt;
        &lt;title&gt;Reports Page&lt;/title&gt;
    &lt;/head&gt;     
    &lt;body&gt;
        &lt;main role=&quot;main&quot; class=&quot;flex-shrink-0&quot;&gt;
            &lt;!-- some HTML code here --&gt;
            &lt;select class=&quot;form-select&quot; id=&quot;year&quot; aria-label=&quot;Year dropdown&quot; th:onchange=&quot;reloadPage()&quot;&gt;
                &lt;option 
                    th:each=&quot;yearRow : ${years}&quot; 
                    th:value=&quot;${yearRow}&quot; 
                    th:selected=&quot;(${yearRow} == ${year})&quot;
                    th:text=&quot;${yearRow}&quot;&gt;
                &lt;/option&gt;
            &lt;/select&gt;
            &lt;!-- other HTML code here --&gt;
        &lt;/main&gt;

        &lt;script th:inline=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;
            function reloadPage() {
                alert(&quot;You selected a year&quot;);
            }
        &lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;

The Layout fragment:

&lt;!DOCTYPE html&gt;
&lt;html class=&quot;h-100&quot; th:fragment=&quot;layout (title, css_assets, js_assets, content)&quot; xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head&gt;
    &lt;!-- Usual head stuff - meta tags, links to Bootstrap CSS &amp; fonts libraries etc --&gt;
    &lt;!-- Common CSS Styles --&gt;
    &lt;link rel=&quot;stylesheet&quot; th:href=&quot;@{/css/styles.css}&quot;&gt;
    
    &lt;!-- Per-page placeholder for additional links --&gt;
    &lt;th:block th:replace=&quot;${css_assets}&quot; /&gt;
        
    &lt;title th:replace=&quot;${title}&quot;&gt;Layout Title&lt;/title&gt;
&lt;/head&gt;
&lt;body class=&quot;d-flex flex-column h-100&quot;&gt;
    &lt;header th:replace=&quot;~{fragments/header :: header}&quot;&gt;&lt;/header&gt;
   
    &lt;div th:replace=&quot;${content}&quot;&gt;
        &lt;p&gt;Layout content&lt;/p&gt;
    &lt;/div&gt;
       
    &lt;footer th:replace=&quot;~{fragments/footer :: footer}&quot;&gt;&lt;/footer&gt;
    
    &lt;!-- Common scripts --&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    
    &lt;!-- Per-page placeholder for additional scripts --&gt;
    &lt;th:block th:replace=&quot;${js_assets}&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;

Inspecting the code, I don't see the inline script:

Thymeleaf内联JavaScript完全被忽略。

.. and in the console the Javascript function is not found:
Thymeleaf内联JavaScript完全被忽略。

Of course, probably I can use the ${js_assets} to inject my JS function from an external JS file but it bothers me why is it not working with the inline syntax. Am I missing something?

I'm pretty new to Spring Boot and Thymeleaf so please be understanding. Thanks!

答案1

得分: 0

以下是代码的中文翻译部分:

感谢 andrewJames,我意识到从我的视图注入到最终HTML的唯一HTML代码是在 main 标签之间包含的部分 - 其他一切不是布局中的片段都会被替换。最简单的解决办法是将内联脚本包含在 main 标签内。

但由于我想要在关闭 body 标签之前拥有它,我最终利用了我已经内建到我的页面的用于每个页面JS的占位符。

以下是视图的新代码(请注意,布局中发送的 js_assets 参数现在指向页面中的片段):

<!doctype html>
<html th:replace="~{fragments/layout :: layout(~{::title}, ~{}, ~{::js_assets}, ~{::main})}">
    <head>
        <title>Reports Page</title>
    </head>     
    <body>
        <main role="main" class="flex-shrink-0">
            <!-- 这里有一些HTML代码 -->
            <select class="form-select" id="year" aria-label="Year dropdown" th:onchange="reloadPage()">
                <option 
                    th:each="yearRow : ${years}" 
                    th:value="${yearRow}" 
                    th:selected="(${yearRow} == ${year})"
                    th:text="${yearRow}">
                </option>
            </select>
            <!-- 这里还有其他HTML代码 -->
        </main>

        <th:block th:fragment="js_assets">
            <script th:inline="javascript" type="text/javascript">
                function reloadPage() {
                    alert("您选择了一年");
                }
            </script>
        </th:block>
    </body>
</html>
英文:

Thanks to andrewJames I realized that the only HTML code that gets injected from my view into the final HTML is the one included between the main tags - everything else that is not a fragment in the layout is replaced. The easiest fix would have been to include my inline script inside main.

But since I want to have it right before the closing body tag I ended up leveraging the placeholder I already built into my layout for page-by-page JS.

Here's the new code for view (notice that the parameter for js_assets sent in the layout is pointing now to the fragment in the page)

&lt;!doctype html&gt;
&lt;html th:replace=&quot;~{fragments/layout :: layout(~{::title}, ~{}, ~{::js_assets}, ~{::main})}&quot;&gt;
    &lt;head&gt;
        &lt;title&gt;Reports Page&lt;/title&gt;
    &lt;/head&gt;     
    &lt;body&gt;
        &lt;main role=&quot;main&quot; class=&quot;flex-shrink-0&quot;&gt;
            &lt;!-- some HTML code here --&gt;
            &lt;select class=&quot;form-select&quot; id=&quot;year&quot; aria-label=&quot;Year dropdown&quot; th:onchange=&quot;reloadPage()&quot;&gt;
                &lt;option 
                    th:each=&quot;yearRow : ${years}&quot; 
                    th:value=&quot;${yearRow}&quot; 
                    th:selected=&quot;(${yearRow} == ${year})&quot;
                    th:text=&quot;${yearRow}&quot;&gt;
                &lt;/option&gt;
            &lt;/select&gt;
            &lt;!-- other HTML code here --&gt;
        &lt;/main&gt;

        &lt;th:block th:fragment=&quot;js_assets&quot;&gt;
            &lt;script th:inline=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;
                function reloadPage() {
                    alert(&quot;You selected a year&quot;);
                }
            &lt;/script&gt;
        &lt;/th:block&gt;
    &lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年3月20日 23:53:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定