Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.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 SpringBoot/Thymeleaf-can';t删除hashmap键_Java_Spring Boot_Thymeleaf - Fatal编程技术网

Java SpringBoot/Thymeleaf-can';t删除hashmap键

Java SpringBoot/Thymeleaf-can';t删除hashmap键,java,spring-boot,thymeleaf,Java,Spring Boot,Thymeleaf,我想为hashmap中的每个值创建一个删除按钮,但它不起作用…:-( 我已在de控制器方法中使用记录器: ->>HomeController:正在从用户删除权重空值:-( 弹簧控制器 @RequestMapping(value = "/deleteValue", method = RequestMethod.POST) public String deleteValue(Principal principal, Long key, ModelMap modelMap) {

我想为hashmap中的每个值创建一个删除按钮,但它不起作用…:-( 我已在de控制器方法中使用记录器: ->>HomeController:正在从用户删除权重空值:-(

弹簧控制器

@RequestMapping(value = "/deleteValue", method = RequestMethod.POST)
public String deleteValue(Principal principal, Long key, ModelMap modelMap) {
    User user = authService.extractUserFromAuthInfo(principal);

    user.deleteValueByKey(key);

    userDAO.save(user);

    return "redirect:/";
}
用户模型

@ElementCollection
@CollectionTable(name = "user_timeline", joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")})
@MapKeyColumn(name = "date")
@Column(name = "weight")
private Map<Long, Double> weights;

...

public void deleteValueByKey (Long key) {
    weights.remove(key);
}
@ElementCollection
@CollectionTable(name=“user\u timeline”,joinColumns={@JoinColumn(name=“user\u id”,referencedColumnName=“id”)})
@MapKeyColumn(name=“date”)
@列(name=“weight”)
私有地图权重;
...
public void deleteValueByKey(长键){
重量。拆卸(键);
}
Thymeleaf(通常情况下,问题就在这里)


X
我发现了问题: 输入行中缺少name=“key”

<form method="POST" action="/deleteValue">
    <tr th:each="weight : ${user.weights}">
        <input type="hidden" name="key" th:value="${weight.key}"/>
        <button type="submit">X</button>
    </tr>
</form>

X

你能回答你的问题来澄清你所说的“它不工作”是什么意思吗?你看到了哪些具体的问题或错误?如果你还没有这样做,请看一看。我不知道是否更好…你可以检查浏览器的HTML(由Thymeleaf生成)并查看Thymeleaf变量是否已被预期的数据替换。如果需要,您甚至可以在Thymeleaf模板中编辑HTML,以便在可见的
s中显示(或无法显示)数据。您如何将Java
用户
数据传递到Thymeleaf用于填充
的上下文中${user.weights}
?我在de home控制器方法中使用了记录器:home控制器:从用户中删除权重null:-(
<form method="POST" action="/deleteValue">
    <tr th:each="weight : ${user.weights}">
        <input type="hidden" name="key" th:value="${weight.key}"/>
        <button type="submit">X</button>
    </tr>
</form>