在支持bean中使用HashMaps

时间:2019-06-13 16:42:15

标签: java spring-mvc thymeleaf

我正在为我的工作搜索构建工作搜索跟踪器,并且尝试使用HashMap将申请人的技能集存储在备用Bean中,并且在将HashMap应用于表单时遇到问题。

这是支持豆

package com.nexus.jobsearchtracker.domain;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name="Applicants")
public class Applicant {

    @Id
    @GeneratedValue
    private Long id;

    @NotNull
    @Size(max = 40)
    private String firstName;

    @NotNull
    @Size(max = 40)
    private String lastName;

    private Integer yearsOfExperience;

    @Embedded
    private Address address;

    private HashMap<String, Integer> skills = new HashMap<>();

    public Applicant() {}

    public Applicant(Long id, @NotNull @Size(max = 40) String firstName, @NotNull @Size(max = 40) String lastName,
            Integer yearsOfExperience, Address address, HashMap<String, Integer> skills) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.yearsOfExperience = yearsOfExperience;
        this.address = address;
        this.skills = skills;
    }

    public HashMap<String, Integer> getSkills() {
        return skills;
    }

    public void setSkills(HashMap<String, Integer> skills) {
        this.skills = skills;
    }
        // Other accessors ommited for brevity
}

这是我正在使用的表格

<table>
    <thead>
        <tr>
            <td>Skill</td>
            <td>Years Experience</td>
            <td><input id="addSkill" type="button" value="Add Skill"></td>
        </tr>
    </thead>
    <tbody id="skillset">
        <tr>
            <td><input id="skill" type="text" th:field="*{skills[key]}"></td>
            <td><input id="yearsExp" type="number" th:field="*{skills[value]}"></td>
        </tr>
    </tbody>
</table>

调试代码时,我最终看到

{value=11}

代替

{java=11}

我在这里做错什么了吗?

编辑:经过更多研究,我通过Thymeleaf的预处理程序找到了答案。

<tbody id="skillset" th:each="skill,skillStat: *{skills}">
    <tr>
        <td><input id="skill" type="text" th:field="*{skills[__${skillStat.index}__].skill}"></td>
        <td><input id="yearsExp" type="number" th:field="*{skills[__${skillStat.index}__].yearsOfExperience}"></td>
    </tr>
</tbody>

感谢您的建议!

0 个答案:

没有答案