【问题标题】:Using user input in HTML form in realtime in HTML embedded Java code在 HTML 嵌入式 Java 代码中实时使用 HTML 表单中的用户输入
【发布时间】:2020-06-23 18:39:43
【问题描述】:

我正在使用 Maven 和 Spring 开发一个 java severlet。

我需要创建一个具有动态行数的表。行应使用变量“amountRows”创建。到目前为止,我最好的猜测是使用嵌入式 Java 代码创建静态数量的行,然后使用 CSS / JavaScript 动态隐藏它们。

知道如何摆脱 for 循环中的“15”并将其替换为变量“amountRows”吗?

或者有什么更平滑的解决方案的想法吗?

 <form action="/myServlet/.../Page.html" method="post">
    <table>
    <caption><center><b>Table Title</b></center></caption>
        <tr><td><input type="number" min="00000" max="30" name="amountRows"/></td></tr>
        <tr>
            <td>column-title</td><td>column-title</td><td>column-title</td><td>column-title</td>
        </tr>
        <%  
            int i = 1;
            for(; i <= 15; i++) {
                out.print("<tr><td>Cell1</td><td>Cell2</td><td>Cell3</td><td>Cell4</td");
            }
        %>          
    </table>
    <input type="submit"/>
</form>

【问题讨论】:

    标签: javascript java html css servlets


    【解决方案1】:

    您可以使用 Thymeleaf 来执行此操作。这是我的一个项目中的一个示例:

    <!doctype html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" 
          xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
        <head>
            <title>Home</title>
            <meta charset="UTF-8"/>
            <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
            <!-- Bootstrap CSS -->
            <link rel="stylesheet" 
                href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" 
                integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" 
                crossorigin="anonymous">
            <link rel="stylesheet" href="/css/main.css"/>
        </head>
        <body>
    
            <div class="row">
    
                <div class="container col-9">
                    <table class="table table-striped">
                        <thead>
                            <th class="standard">Song Title and Artist</th>
                            <th class="year" style="font-weight:900">Year</th>
                        </thead>
                        <tbody>
                            <tr th:each="song : ${songs}">
                                <td>
                                    <a href="#" 
                                       th:href="@{/playsong(id=${song.songId})}"
                                       th:text="${song.title + ' - ' + song.artist}">
                                        Song title and artist
                                    </a>
                                </td>
                                <td class="year" th:text="${song.year}">
                                    Song year
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            <footer>
                &copy; Copyright 2020 Lindsay Nickalo. All rights reserved.
            </footer>
    
            <!-- Optional JavaScript -->
            <!-- jQuery first, then Popper.js, then Bootstrap JS -->
            <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" 
                integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" 
                crossorigin="anonymous"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" 
                integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" 
                crossorigin="anonymous"></script>
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" 
                integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" 
                crossorigin="anonymous"></script>
        </body>
    </html>
    

    在您的映射中,您只需使用model.addAttribute("amountRows", amountRows); 将变量添加到模型中,然后就可以在带有Thymeleaf 的html 中使用它。在我的示例中,我传入了完整的 Song 对象列表,然后使用 th:each="song : ${songs}" 为每首歌曲创建了一个表格行。在 Thymeleaf 中,您使用 ${variable} 引用变量

    在你的 POM 中,包含

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 2022-01-23
      • 2020-02-20
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多