【问题标题】:Whats the best way to populate an Array List and make it stick when page reloads什么是填充数组列表并在页面重新加载时保持不变的最佳方法
【发布时间】:2019-01-23 23:02:39
【问题描述】:

我一直在尝试自学 Java,但我似乎找不到我正在寻找的关于数组列表的答案。

我的项目有一个表单,当页面加载时,它会加载我创建的几个列表,例如,复选框中的业务代理类型或使用列表显示下拉选项的业务类。

我一直在编写以下代码来填充不同的字段部分。

    @GetMapping("/directBind")
    public String getDirectBind(Model model){
        DirectBind directBind = new DirectBind();
        List<String> businessAgencies = new ArrayList<String>();
        businessAgencies.add("Personal");
        businessAgencies.add("Commercial");
        businessAgencies.add("Life");
        businessAgencies.add("Benefits");
        businessAgencies.add("Health");
        businessAgencies.add("Non P and C");
        model.addAttribute("businessAgencies", businessAgencies);
        List<String> businessAgencyList = new ArrayList<String>();
        directBind.setBusinessAgencyList(businessAgencyList);

        List<Ams360Policy> ams360Policies = new ArrayList();
        Ams360Policy ams360Policy = new Ams360Policy();
        ams360Policies.add(ams360Policy);
        model.addAttribute("ams360Policies", ams360Policies);

        List<String> billTypeList = new ArrayList<String>();
        billTypeList.add("Direct Bill");
        billTypeList.add("Agency Bill");
        model.addAttribute("billTypeList", billTypeList);
        ams360Policy.setBillTypeOptions(billTypeList);

        directBind.setDirectBox(true);
        directBind.setServiceLevel(true);

        List<String> businessClasses = new ArrayList<>();
        businessClasses.add("Animal Services");
        businessClasses.add("Bonds");
        businessClasses.add("Contractor");
        model.addAttribute("businessClasses", businessClasses);
        directBind.setBusinessClasses(businessClasses);

        model.addAttribute("directBind", directBind);

        return "directBind";
    }

我的问题是,这真的是创建数组列表的最佳方式,还是应该去其他地方?因为它只存在于“检索/加载表单的函数”中,所以对表单的任何更改,比如让用户添加新的行/输入字段,都会导致这段代码必须在 addRow 中重写功能。它看起来真的很多余,我想知道是否有更好的地方来添加它。像 java 类文件中的单独控制器函数或构造函数一样?

我还注意到,用户输入答案的某些输入字段会在添加新行时保留在页面上,而某些用户输入不会保留。也很乐意帮助。不确定这是否是一个相关问题,因为我正在以不同的方式生成字段,或者它是否是它自己的问题。

这是表单的一些 HTML:

<form  class="ui form" th:object="${directBind}" th:action="directBind" method="post"  style="padding:0 10px;">
  <h4>Customer Setup</h4>
  <div class="row">
      <label >Contact Name (First/Last):</label>
      <input type="text" th:field="*{contactName}" required="true" />
  </div>
  <div class="row">
      <label for="formAddress">Address:</label>
      <input type="text" id="formAddress" th:field="*{formAddress}" required="true"/>
  </div>
  <div class="row">
      <label for="phoneNumber">Phone Number:</label>
      <input type="text" id="phoneNumber" th:field="*{phoneNumber}" required="true"/>
  </div>
  <div class="row">
      <label for="email">Email:</label>
      <input type="email" id="email" th:field="*{email}" required="true"/>
  </div>
  <div class="row">
      <label for="website" style="margin-top: 1em;"> Website:</label>
      <input type="text" id="website" th:field="*{website}" required="true"/>
  </div>
  <div class="row">
      <label for="nameInsured">Name Insured:</label>
      <input type="text" id="nameInsured" th:field="*{insuredName}" required="true"/>
  </div>
  <div class="row">
      <label>Business with Agency:</label>
      <div th:each="businessAgency : ${businessAgencies}">
          <input type="checkbox" th:field="*{businessAgencyList}" th:value="${businessAgency}"/>
          <label th:text="${businessAgency}">Business with Agency</label>
      </div>

  </div>
<!-- Business Class Drop Down Field Below: -->
      <div class="row">
          <label>Business Class: </label>
          <div >
              <select class="form-control" th:field="*{businessClasses}" required="true">
                  <option th:each="businessClass : ${businessClasses}" th:value="${businessClass}" th:text="${businessClass}"></option>
              </select>
          </div>

      </div>
      <div class="row">
      <label for="descriptionOfOps">Description of Operations:</label>
      <input type="text" id="descriptionOfOps" th:field="*{descriptionOfOps}" required="true" />
  </div>



  <div class="formFooter">
      <input id="send"  type="submit" value="send" name="send" class="btn btn-success finish" data-loading-text="Sent!" />
  </div>
</form>

【问题讨论】:

    标签: java list spring-boot arraylist thymeleaf


    【解决方案1】:

    每次调用 REST 端点时,您似乎都在生成静态内容。 您可能想要研究的与此相关的第一个原则是separation of concernsMVC 适用于您的案例,它被广泛使用并代表这 3 层的分离:模型-视图-控制器。有些框架已内置支持这种方法。

    模型应该是一个单独的数据库或在您保存数据的内存存储中 - 在您的情况下是 ArrayLists。您将在此处保存所有更改并向每个请求者提供相同的数据。

    现在您将模型数据保留在代码的视图部分中,这很不幸,原因有很多。

    Controller 应该是从 Model 中检索数据并将其提供给 View 的中间层。

    Here is a random simple example 谈如何实施这一有助于您入门的设计原则。在这种情况下,每层应该使用一个类,并且随着服务的增长,您可以构建复杂性。

    【讨论】:

    • 那么把它放在Model中的一个方法中,然后在每个控制器端点中调用那个方法?
    • 是的,如果您只想要一个内存存储,那么您可以为您拥有的每种数据类型创建一个对象。当视图端(您的表单)有请求时,控制器类可以调用模型类,模型类可以将请求的对象返回给控制器,控制器会将其发送到视图。
    猜你喜欢
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2022-08-11
    • 2022-06-10
    • 1970-01-01
    • 2010-11-26
    相关资源
    最近更新 更多