当前位置: 首页 > 知识库问答 >
问题:

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

李捷
2023-03-14

请帮助我解决这个问题:我不知道如何将数据从thymeleaf视图传送到controller,那个预期的数据作为哈希映射?所以让我更详细地解释一下。

我有下一个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;
  }
}

Controller具有参数model属性的方法和另一个POJO:

@RequestMapping(value = "/go", method = RequestMethod.POST)
public String advanced(@ModelAttribute DTDays dtDays,
                       @RequestParam(name = "tourId") Long tourId, Model model){
    // make some business logic with provided data 
    return "redirect:/tours/advanced";
}
<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>

视图如下所示:

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

我已经知道如何通过在视图中使用select-option提交列表:

<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>
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView addTourAction(@ModelAttribute("tour") Tour tour,
@RequestParam(required = false, name = "drivers2attach") List<Long> drivers2attach) 

如果列表数据是自动填充的。在map only keys是预填充的,这是驱动程序的计数,现在我希望每个驱动程序的用户输入都是一个键值。

在研究答案时,我已经阅读了这些资料:如何绑定一个对象列表与thymeleaf?

将列表对象从thymeleaf发送到controller

http://forum.thymeleaf.org/how-to-use-map-lt-string-string-gt-with-spring-and-thymeleaf-forms-td4028666.html

http://forum.thymeleaf.org/how-to-use-method-postwith-a-complex-hashmap-lt-object-list-lt-object-gt-gt-td4031257.html

Tymeleaf映射形式结合

使用HashMap作为表单,支持Bean、Spring、MVC+ThymeLeaf等。

但无济于事。在这个链接的某个地方,我发现我应该使用一些包装器来完成它,但再次不知道为什么它不工作,或者我应该做什么额外的使它工作。也许我通常会做错误的逻辑,为了将数据提交为hashmap我应该先将数据转换为list,然后在Controller中以某种方式从中获取map?

共有1个答案

鲁文昌
2023-03-14

很抱歉创建了一个重复的问题,最后我找到了解决方法,遵循这个关于stackoverflow的答案

我写了一个小项目来测试它,它工作了。所以我用包装器在控制器上是正确的。我只错过了视图表示和语法;

最后,视图将如下所示:视图

以下是视图源代码:

<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;
}

输出为:输出

 类似资料:
  • 我有这个代码,但它不起作用(我认为它是th:action=“@{/employee}”,因为它想从我这里输入html href)对不起我的英语)) 如果你知道,帮帮我,pleace))) 4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.2.发布.... 1.8 在此输入图像说明

  • 我有一个定义如下的散列表: 此映射由作为键的字段名称和作为课程值的字段值组成。我正在尝试制作一个将 HashMap 和表名作为参数的方法。我的查询必须具有以下格式,因为我不会插入到表中的所有列: 列的数量当然取决于散列表的大小。我如何通过遍历散列表来实现这一点呢?以下是我迄今为止使用不同方法得出的结论,但我不认为它会正常工作:

  • 我有一个包含许多重复条目的文件,如下所示: 那里的每个实体也与一个ID相关联,也许是一个,也许是更多, 我想单独获取每个实体,但由于消除歧义,对于类似于,每个实体都可以与多个ID关联,因此在下,可能会有一个用于河流的ID,另一个用于海湾、城镇等。 我想最好的方法是使用哈希映射,其中名称是关键,对吗? 有没有办法输出JSON格式的hashmap或其他高度可移动的数据表示?

  • 假设我在jsp中有这个表单 和Person类 编辑:控制器实现 我没有问题调用其他方法,但测试。

  • 问题内容: 我必须将数据从html页面(带有很少输入文本字段的简单形式)发送到页面控制器,然后再发送到数据库。我正在使用3.0版的百里香2.0.17。我搜索并检查了一些解决方案,但是没有用。也许有人遇到了同样的问题,并找到了一些好的解决方案。请帮忙。谢谢 问题答案: 如本教程所建议,你需要使用,并在中创建一个表单。 看起来像这样: 控制器: HTML: Foo.java: 希望这可以帮助。

  • 问题内容: 我目前正在尝试制作一个将动词与西班牙语共轭的程序。我创建了一个哈希表,其中包含一个键和对象Ve​​rb的实例化。键是具有动词不定式形式的字符串(例如“ hablar”)。这是到目前为止我对哈希映射的代码: HashMap中每个动词的键都基于动词的不定式形式。例如,字符串“ hablar”是西班牙语动词的键。Verb类具有一个名为getInfinitive()的方法,该方法返回一个字符串