不显示创建了 JQuery 个 div 的页面

Page with JQuery created divs is not displayed

当我在 chrome 中手动打开它时,以下工作正常。但是,一个简单的 spring 入门应用程序拒绝传送文件。

IndexController class 在没有 jquery 脚本的情况下工作正常

@Controller
public class IndexController {
    @RequestMapping({"/index","/"})
    public String index(Model model,HttpSession session) {
        return "index";
    }
}

如果我只想创建一个 div!

,html 文件已正确传送
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Hello</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            var i;
            for( i = 0 ; i < 5 ; i++ )
                {
                    var elem = '<div>' + '<p>blafoo</p>' + '</div>';
                    $('#container').append(elem);
                }
           });
        </script>

    </head>

    <body>
    <h1>Foo bar!</h1>
        <div id="container">
        </div>
    </body>
</html>

我实际上对 Thymeleaf 有一些经验。但是根据此处的搜索,某些用户确实对 Javascript 和 Thymeleaf 有疑问。

您可以尝试将脚本包含在 CDATA

//<![CDATA[
    your script
//]]>

或者只是将 Javascript 放在外部文件中。

参见:

  1. Thymeleaf and inline scripts SAXParseException
  2. Thymeleaf encoding issues with javascript (using spring mvc)