Spring Boot + Thymeleaf: как добавить поле th: в список <String>при отправке формы? - PullRequest
0 голосов
/ 20 октября 2018

У меня есть класс 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);
    }


}

"formContent" Переменная типа List должна содержать каждую из записей Stringсделано в HTML-форме, которая выглядит следующим образом:

<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».

Мой Контроллер Класс, который обрабатывает запрос на публикацию, отправленный 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, но ни одна из них не сработала.

Буду очень признателен за ваш отзыв!

Большое спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...