javascript - 我如何使用 thymeleaf 在 javascript 中使用 for 循环?

标签 javascript thymeleaf

我正在使用 thymeleaf,所以当我运行这个应用程序时,它给我一个错误 (for(int i=0;i<10;i++) 这意味着我必须尊重 thymeleaf 的语法。我的问题是如何才能我使用 thymeleaf 编写了这个脚本。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handing Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
        <script type="text/javascript" src="jquery-1.11.3.js"></script>

</head>
<body>
    <h1>Result</h1>
    <p th:text="'columns_number: ' + ${db.columns_number}" />
    <h3>Création de la base de données</h3>
    <p>Table_name: <input type="text" th:field="${db.table_Name}" /></p>
<table id='tablona' border='1px'>
        <tr>
        <th>field</th>
                <th>Size</th>
                <th>Type</th>
                <th>null</th>
                </tr>


</table>

<script th:inline="javascript">
    /*<![CDATA[*/
    $( document ).ready(function() {

        for(int i=0;i<[[${T(Integer).parseInt(db.columns_number)}]];i++)
      {
        $('<tr>'+
        '<td><input id="field" type="text" name="field'+i+'"  maxlength="255"  required="required"/></td>'+
        '<td><input id="Size" type="text"  name="Size'+i+'"  maxlength="255" required="required"/></td>'+
        '<td><SELECT id="Type" name="Type'+i+'">'+
'<OPTION VALUE="varchar">varchar</OPTION>'+
'<OPTION VALUE="int">int</OPTION>'+
'<OPTION VALUE="text">long</OPTION>'+
'<OPTION VALUE="float">float</OPTION>'+
'<OPTION VALUE="double">double</OPTION>'+
'<OPTION VALUE="Date">Date</OPTION>'+
'<OPTION VALUE="Time">Time</OPTION>'+
'<OPTION VALUE="Year">Year</OPTION>'+
'<OPTION VALUE="Real">Real</OPTION>'+
'<OPTION VALUE="Boolean">Boolean</OPTION>'+
'<OPTION VALUE="longText">longText</OPTION>'+
'<OPTION VALUE="Binary">Binary</OPTION>'+
'</SELECT></td>'+
'<td><SELECT id="null" name="nullabilite'+i+'">'+
'<OPTION VALUE="null">null</OPTION>'+
'<OPTION VALUE="not_null">not_null</OPTION>'+
'</SELECT></td>'+
    '</tr>').appendTo($("#tablona")).html()

    }
    });
    /*]]>*/
</script>
      <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>    

</body>
</html> 

最佳答案

你应该用这个结构包装你的脚本:

<script th:inline="javascript">
    /*<![CDATA[*/
    $( document ).ready(function() {
        for(i=0;i<10;i++) {
            ...
        }
    });
    /*]]>*/
</script>

编辑:

不要忘记将 javascript 和其他静态文件存储在 spring-boot 项目的 /src/main/webapp 文件夹中

编辑2:

你可以直接用 thymeleaf 做你的脚本:

<tr th:each="i : ${#numbers.sequence( 1, db.columns_number)}">
    <td><input id="field" th:name="${'field'+i}" maxlength="255"
        required="required" type="text" /></td>
    <td><input id="Size" th:name="${'Size'+i}" maxlength="255"
        required="required" type="text" /></td>
    <td><select id="Type" th:name="${'Type'+i}">
             ...
        </select></td>
    <td><select id="null" th:name="${'nullabilite'+i}">
            <option value="null">null</option>
            <option value="not_null">not_null</option>
    </select></td>
</tr>

关于javascript - 我如何使用 thymeleaf 在 javascript 中使用 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194185/

相关文章:

javascript - 如何将json数据放入html中

javascript - React 中的 useState() 不更新数据 - 这是 sort() 问题吗?

java - 如何将 html 输入保存到列表中并在每次提交时显示?

javascript - appendTo 添加多个对象

javascript - 是否可以在 nodejs 中倒带文件描述符游标?

javascript - 平滑地动画封面大小的背景

spring - 将对象传递到 Thymeleaf 片段中

javascript - 如何获得西方数字模式(用逗号分隔)

带有 Gradle 的 Spring Boot 缺少 Thymeleaf 的依赖项

eclipse - Spring JBoss Eclipse 中的周转时间