首页 > 解决方案 > 在表单的选择框部分显示数组列表

问题描述

我正在使用 Java Spring 开发一个简单的锻炼跟踪器。我正在尝试创建我的锻炼表格,该表格允许用户输入锻炼名称/重量/代表并选择他们希望添加的(先前创建的)锻炼。我查看了许多讨论类似问题的线程,但没有任何东西对我有用。

这是我的 WorkoutController 中的方法:

@PostMapping("/workout/createExercise")
public String createExercise(@ModelAttribute Exercise exercise,
                             Model model) {
    Collection<Workout> workouts = workoutService.getWorkouts();
    model.addAttribute("workouts", workouts);
    workoutService.saveExercise(exercise);
    return "createExercise";
}

这是表格:

<options action="#" th:action="@{/exercise/createExercise}" th:object="${exercise}" method="post">
            Exercise name:<br>
            <input type="text" class="formInput" name="exercise-name" size="50"
                   th:field="*{exerciseName}"><br>
            <div class="divider"></div>
            Weight:<br>
            <input type="text" class="formInput" name="weight" size="50"
                   th:field="*{weight}"><br>
            <div class="divider"></div>
            Reps:<br>
            <input type="text" class="formInput" name="reps" size="50"
                   th:field="*{reps}"><br>
            <div class="divider"></div>
            Workout:<br>
            <select name="workout" class="exerciseForm" name="workout">
                <option>--</option>
                <option items="${workouts}" itemLabel="workoutName"
                       itemValue="workoutID"></option>
            </select>
            <br><br>
            <input type="submit" class="input-box">
        </form>

我尝试了很多变化。有时它会渲染页面但当我单击下拉框时不显示任何内容,有时它会显示以下错误:

18-11-07 12:32:47 ERROR org.thymeleaf.TemplateEngine - 
[THYMELEAF][http-nio-8080-exec-10] Exception processing 
 template "createExercise": An error happened during 
 template parsing (template: "class path resource 
 [templates/createExercise.html]" - line 57, col 61)
 org.thymeleaf.exceptions.TemplateInputException: An error 
 happened during template parsing (template: "class path 
 resource [templates/createExercise.html]" - line 57, col 61)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:239)
at 

任何建议都非常感谢!谢谢。

标签: javaspring

解决方案


推荐阅读