Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将数据作为哈希映射从thymeleaf表单传输到控制器_Java_Spring Mvc_Thymeleaf - Fatal编程技术网

Java 如何将数据作为哈希映射从thymeleaf表单传输到控制器

Java 如何将数据作为哈希映射从thymeleaf表单传输到控制器,java,spring-mvc,thymeleaf,Java,Spring Mvc,Thymeleaf,请帮助我解决这个问题:我不知道如何将数据从thymeleaf视图传输到控制器,该数据作为哈希映射吗?所以让我更详细地解释一下 我有下一个pojo,它用作hashMap数据的包装器。看起来是这样的: public class DTDays { private Map<Driver, String> driversDays = new HashMap<>(); public Map<Driver, String> getDriversDays() {

请帮助我解决这个问题:我不知道如何将数据从thymeleaf视图传输到控制器,该数据作为哈希映射吗?所以让我更详细地解释一下

我有下一个pojo,它用作hashMap数据的包装器。看起来是这样的:

public class DTDays {
  private Map<Driver, String> driversDays = new HashMap<>();

  public Map<Driver, String> getDriversDays() {
      return driversDays;
  }

  public void setDriversDays(Map<Driver, String> driversDays) {
      this.driversDays = driversDays;
  }
}
在这里我调试过,并且
dtDays
不为空,但map属性为空,而且tourId按预期工作,我可以得到正确的值

现在要解决问题,请查看:

<body>
<div style="margin-left: 20px;">
    <h1 th:text="${tour.tittle}"></h1>
    <p>Add tour interval for drivers</p>
    <form id="driverIntervals" th:action="@{/advanced/go}" th:object="${driversDays}" method="post">
        <table>
            <tr>
                <td>Drivers:</td>
                <td>Date:</td>
            </tr>
            <tr th:each="d: ${attachedDrivers}">
                <td th:text="${d.id+' '+d.fullName}" >
                    <input type="hidden" th:value="${d.id}" >
                </td>
                <td>
                    <input type="text" placeholder="Pick days" th:name="days"/>
                </td>
            </tr>
        </table>
        <input type="hidden" th:name="tourId" th:value="${tour.id}"/>
        <button type="submit">Confirm</button>
    </form>
</div>
</body>

为驾驶员添加巡更间隔

司机: 日期: 证实
视图如下所示:

public class DTDays {
  private Map<Driver, String> driversDays = new HashMap<>();

  public Map<Driver, String> getDriversDays() {
      return driversDays;
  }

  public void setDriversDays(Map<Driver, String> driversDays) {
      this.driversDays = driversDays;
  }
}

为了提交数据,我应该写些什么?在我的例子中,驱动程序是地图的一个键,用户在相关输入字段中输入的数据将是地图的一个值

我已经知道如何使用视图中的选择选项提交列表:

<select th:name="drivers2attach" multiple="multiple" id="attachDrivers">
    <!--/*@thymesVar id="drivers" type="java.util.List<stanislav.tun.novinomad.picasso.persistance.pojos.Driver>"*/-->
    <option th:each="d : ${drivers}" th:value="${d.id}"
            th:text="${d.getId()+' '+d.fullName}">
    </option>
</select>

和控制器中的@RequestParam列表:

@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView addTourAction(@ModelAttribute("tour") Tour tour,
@RequestParam(required = false, name = "drivers2attach") List<Long> drivers2attach) 
@RequestMapping(value=“/save”,method=RequestMethod.POST)
公共模型和视图addTourAction(@ModelAttribute(“tour”)TourTour,
@RequestParam(required=false,name=“drivers2attach”)列表驱动程序2attach)
但如何处理map?

如果使用列表,则自动填充数据。在map中,只预填充键,这是驱动程序的计数,现在我希望每个驱动程序的用户输入作为键值

在研究答案时,我已经阅读了以下来源:

等等


但是没有帮助。在这个链接的某个地方,我发现我应该使用一些包装器来完成它,但同样不知道为什么它不工作,或者我应该做些什么来让它工作。也许我通常会犯错误的逻辑,为了以hashmap的形式提交数据,我应该首先将数据转换为列表,然后在控制器中以某种方式从列表中获取映射?

很抱歉,我创建了重复的问题,最后,我按照下面的答案找到了解决方案

我写了一个测试它的小项目,它工作了。所以我带着包装器进入控制器是正确的。我只错过了视图地图表示和语法

最后,视图将如下所示:

以下是查看源代码:

<form th:action="@{/save}" th:object="${form}" method="post">
<h1>Welcome</h1>
<div th:each="property : ${form.properties.entrySet()}">
    <div class="form-group">
        <label th:for="*{properties['__${property.key}__']}" th:text="${property.key}">Property</label>
        <input type="text" class="form-control" th:field="*{properties['__${property.key}__']}" />
    </div>
</div>
<button type="submit">send to controller</button>
输出为:

另外,我在这里提问的原因是我在这个问题上坚持了大约两周,我感到绝望,但在提出问题后找到了解决办法

 @PostMapping("/save")
public ModelAndView save(@ModelAttribute(name = "form") MapWrapper form){
    var mav = new ModelAndView();
    mav.setViewName("index");
    mav.addObject("mapWrapper", form);
    var map = form.getProperties();

    System.out.println("Size of map = "+map.size());
    for (Long id : map.keySet()) {
        System.out.println("Key: "+id+"; Value: "+map.get(id));
    }

    return mav;
}