Spring Boot + Thymeleaf:提交表单时,如何在列表<string>中添加th:field?

时间:2018-10-20 13:02:21

标签: java spring-boot thymeleaf

我确实有一个 Model 类,如下所示:

@Document(collection="compliancerequests")
public class ComplianceRequest {
    @Id
    String id;
    String overarchingTopicId;
    Date requestDate;
    String title;
    String description;
    String htmlFormView;
    String htmlViewName;
    List<String> formContent;


    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getOverarchingTopicId() {
        return overarchingTopicId;
    }
    public void setOverarchingTopicId(String overarchingTopicId) {
        this.overarchingTopicId = overarchingTopicId;
    }

    public Date getRequestDate() {
        return requestDate;
    }

    public void setRequestDate(Date requestDate) {
        this.requestDate = requestDate;
    }

    public void setCurrentDate() {
        this.requestDate = new Date();
    }

    public String getCurrentDate() {
        Date dnow = new Date();
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd ' / ' HH:mm:ss");
        return ft.format(dnow);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getHtmlFormView() {
        return htmlFormView;
    }

    public void setHtmlFormView(String htmlFormView) {
        this.htmlFormView = htmlFormView;
    }

    public String getHtmlViewName() {
        return this.title.toLowerCase().replaceAll("\\s+","");
    }

    public void setHtmlViewName() {
        this.htmlViewName = this.title.toLowerCase().replaceAll("\\s+","");
    }

    public List<String> getFormContent() {
        return formContent;
    }

    public void setFormContent(List<String> formContent) {
        this.formContent = formContent;
    }

    public void addToList(String s) {
        this.formContent.add(s);
    }


}

List类型的“ formContent” 变量应包含HTML表单中的每个String条目,其外观如下:

<form action="#" th:action="@{'/compliancerequests/' + ${htmlViewName} + '/submit'}" th:object="${request}" enctype="multipart/form-data" method="POST">
<div class="form-row">
      <div class="form-group col-md-12">
          <label for="employeeName">Employee Name</label>
          <input type="text" class="form-control" id="employeeName" placeholder="Employee Name" th:field="*{formContent[0]}"></input>
      </div>
      <div class="form-group col-md-12">
        <label for="securityName">Security Name</label>
        <textarea class="form-control" id="securityName" rows="3" placeholder="Security Name" th:field="*{formContent[1]}"></textarea>
      </div>
      <div class="form-group col-md-12">
        <label for="ticker-isin">Ticker / ISIN</label>
        <input type="text" class="form-control" id="ticker-isin" placeholder="Ticker / ISIN" th:field="*{formContent[2]}"></input>
      </div>
</div>

  <br/>

  <button type="submit" class="btn btn-primary">Submit Request</button>
</form>

但是,在提交HTML表单时,我还没有设法将所有表单字段条目添加到“ formContent”列表中。

我的 Controller 类用于处理HTML表单提交的发布请求,如下所示:

@PostMapping("/{htmlViewName}/submit")
public ModelAndView submitRequest(@PathVariable("htmlViewName") String htmlViewName, @ModelAttribute("request") ComplianceRequest compliancerequest, BindingResult bindingResult) {
    ModelAndView mv = new ModelAndView();

    if(bindingResult.hasErrors()) {
        System.out.println("There was a error " + bindingResult);
        mv.setViewName("request_" + htmlViewName + ".html");

        return mv;
    }
    compliancerequest.setTitle(compliancerequest.getHtmlViewName() + "//" + compliancerequest.getCurrentDate());
    compliancerequest.setCurrentDate();
    requestRepository.insert(compliancerequest);
    mv.setViewName("redirect:/compliancetopics/");

    return mv;
}

能否请您帮我,让我知道我做错了什么?在编写HTML / Thymeleaf表单时,我确实尝试了多个标记版本,但是没有一个起作用。

非常感谢您的反馈!

非常感谢您!

0 个答案:

没有答案