【问题标题】:Populating dynamically a list in thymeleaf在百里香中动态填充列表
【发布时间】:2020-01-28 21:52:02
【问题描述】:

在 Spring MVC 项目的控制器中,我确实有一个列表,我将其放入模型映射中,如下所示。

@GetMapping("/teacher/{userId}/add_thiesis")
  public String dashboard(ModelMap model, @PathVariable Long userId) {

        List<Course> courses = courseRepo.findAll();
        model.put("courses", courses);

        String uId = String.valueOf(userId);

        ArrayList<String> questions = new ArrayList<>();
        model.put("id", uId);
        model.put("thiesis", new Thiesis());
        model.put("questions",questions);
        return "add_thiesis"; 
  }

现在我想在百里香视图中使用 post 方法动态填充它。为此,我正在使用 JavaScript 脚本。这不是所有的 html 代码,但为简单起见,我只添加了 JS 部分。

    window.addEventListener("load", start);
    var i='0';




    var question = '<div class="form-group row">'+
                        '<label for="name" class="col-12 col-sm-4 col-form-label">Question:</label>'+
                        '<div class=" col-12 col-sm-8">'+
                            '<input type="text"'+ ' class="form-control" placeholder="Enter question here..." th:field="{questions['+i+']"}  required/><br/>'+
                        '</div>'+
                    '</div>';

    function start(){
        var plus = document.getElementById("plus");
        var minus = document.getElementById("minus");


        plus.addEventListener("click", add);
        minus.addEventListener("click", remove);
    }


    function add(){
        var addQuestion = document.getElementById("question_body");
        addQuestion.innerHTML += question;
        i++;
        console.log(i);
    }

    function remove(){
        var removeQuestion = document.getElementById("question_body");
        if(i>0){
            console.log(removeQuestion.lastChild.nodeName);
            removeQuestion.removeChild(removeQuestion.lastChild);
            i--;
            console.log(i);
        }

    }

这是经过测试可以正常工作的 post 方法。

    @PostMapping("/teacher/{userId}/add_thiesis")
public String addThiesis(Thiesis thiesis, ArrayList<String> questions)
{
    for (String question : questions) {
        Question q = new Question();
        q.setQuestion(question);
        questionService.save(q);
    }
    System.out.println("addThiesis called");
    String tmpCourse = thiesis.getTmpCourse();
    thiesisService.save(thiesis, tmpCourse);
    System.out.println(thiesis.getId());
    return "" ;
}

它向我显示错误: 解析模板 [] 时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问,这肯定是因为:th:field="{questions['+i+']"。 有没有办法动态填充数组,因为添加的问题数量是一个变量。有没有办法处理它?

【问题讨论】:

    标签: java spring model-view-controller thymeleaf


    【解决方案1】:

    在你的 GET 方法中试试这个:

    return "add_thiesis.html";
    

    然后像这样使用@Controller 注解:

    @Controller
    public class YourClass {
    //
    }
    

    编辑:

    对于 th:field 也有一个小错误

    th:field="'*{questions['+i+']}'"
    

    【讨论】:

    • @Controller 注解已经放好了,.html 没有任何区别。正如我清楚地推测的那样,问题出在视图方面而不是在控制器中。
    • 根据错误消息,程序甚至没有找到您的模板,请确保它位于资源下的“模板”文件夹中。
    • 我解决了部分问题。在 post 方法中,我将用户重定向到一个新页面,它可以工作。但我还是有问题。提交表单时未填充问题数组,因此模型返回一个空数组列表。有什么我做错了吗。
    猜你喜欢
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2014-11-14
    • 2020-11-20
    • 2016-04-02
    • 1970-01-01
    • 2014-11-21
    相关资源
    最近更新 更多