2017-07-03 52 views
0

當選項是字符串時,我在'select'中選擇正確的值時遇到問題。我無法在論壇中找到解決方案。選擇從控制器傳遞的選項

我在控制器中傳遞'kind',我可以看到值很好,但只有整數字段在'select'中被正確選擇。用String的那些總是顯示第一個值,而不是'kind'中的一個傳遞。

我添加了代碼我認爲它可以幫助 任何人都可以幫忙嗎?

我的HTML代碼。該表格包含了許多「選擇」,但我留下了兩個,第一個作品,但第二個始終顯示的第一個選項:

<form role="form" th:action="@{/kind/update}" th:object="${kind}" method="post"> 
    <div class="form-group col-md-4"> 
     <label for="replicates">No. of Replicates</label> 
     <select id="replicates" class="form-control" style="width: 70%;" th:field="${kind.replicates}"> 
      <option th:each="rep: ${replicatesnumber}" th:value="${rep}" th:text="${rep}"> </option> 
     </select> 
    </div> 
    <div class="form-group col-md-3"> 
     <label for="substrate">Substrate</label> 
     <select id="substrate" class="form-control" th:field="${kind.substrate}"> 
      <option th:each="substrate: ${substrates}" th:value="${substrate}" th:text="${substrate}"> </option> 
     </select> 
    </div> 
    <div class="box-footer"> 
    <button type="submit" class="btn btn-primary">Save</button> 
    <a class="btn btn-primary" th:href="@{/division/edit/}+${kind.division.id}" role="button">Cancel</a>          
    </div> 
</form>  

控制器是這樣的:

@Controller 
@RequestMapping("/kind") 
public class KindController { 

    @Autowired 
    private KindService kindService; 

    @ModelAttribute("replicatesnumber") 
    public int[] getReplicates() { 
     int[] reps = new int[3]; 
     reps[0] = 2; 
     reps[1] = 4; 
     reps[2] = 8; 
     return reps; 
    } 

    @ModelAttribute("substrates") 
    public List<String> getSubstrates() { 
     return Arrays.asList("BP", "PP", "TP", "OGM", "Sand"); 
    } 

    @GetMapping(value= "/edit/{kindId}") 
    public String viewDivision(@PathVariable Integer kindId, Model model){ 
     Kind kind= kindService.findById(kindId); 
     model.addAttribute("kind",kind); 
     return "kind_edit"; 
    }  

和實體:

@Entity 
@Table(name = "kind", schema = "ostscourses") 
public class Kind implements java.io.Serializable { 

private Integer id; 
private Division division; 
private String name; 
private Integer germinationDays; 
private Integer firstCount; 
private Integer replicates; 
private Boolean dark; 
private Integer chill; 
private String temperature; 
private String substrate; 
private Integer noSeeds; 
private List<Sample> samples; 

public Kind() { 
} 

public Kind(Integer id, Division division) { 
    this.id = id; 
    this.division = division; 
} 

public Kind(Integer id, Division division, String name, Integer germinationDays, Integer firstCount, Integer replicates, Boolean dark, Integer chill, String temperature, String substrate, Integer noSeeds, List<Sample> samples) { 
    this.id = id; 
    this.division = division; 
    this.name = name; 
    this.germinationDays = germinationDays; 
    this.firstCount = firstCount; 
    this.replicates = replicates; 
    this.dark = dark; 
    this.chill = chill; 
    this.temperature = temperature; 
    this.substrate = substrate; 
    this.noSeeds = noSeeds; 
    this.samples = samples; 
} 

@Id 
@Column(name = "id", unique = true, nullable = false) 
public Integer getId() { 
    return this.id; 
} 

public void setId(Integer id) { 
    this.id = id; 
} 

@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "division_id", nullable = false) 
@JsonIgnore 
public Division getDivision() { 
    return this.division; 
} 

public void setDivision(Division division) { 
    this.division = division; 
} 

@Column(name = "name", length = 25) 
public String getName() { 
    return this.name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

@Column(name = "germination_days") 
public Integer getGerminationDays() { 
    return this.germinationDays; 
} 

public void setGerminationDays(Integer germinationDays) { 
    this.germinationDays = germinationDays; 
} 

@Column(name = "first_count") 
public Integer getFirstCount() { 
    return this.firstCount; 
} 

public void setFirstCount(Integer firstCount) { 
    this.firstCount = firstCount; 
} 

@Column(name = "replicates") 
public Integer getReplicates() { 
    return this.replicates; 
} 

public void setReplicates(Integer replicates) { 
    this.replicates = replicates; 
} 

@Column(name = "dark") 
public Boolean getDark() { 
    return this.dark; 
} 

public void setDark(Boolean dark) { 
    this.dark = dark; 
} 

@Column(name = "chill") 
public Integer getChill() { 
    return this.chill; 
} 

public void setChill(Integer chill) { 
    this.chill = chill; 
} 

@Column(name = "temperature", length = 10) 
public String getTemperature() { 
    return this.temperature; 
} 

public void setTemperature(String temperature) { 
    this.temperature = temperature; 
} 

@Column(name = "substrate", length = 5) 
public String getSubstrate() { 
    return this.substrate; 
} 

public void setSubstrate(String substrate) { 
    this.substrate = substrate; 
} 

@Column(name = "no_seeds") 
public Integer getNoSeeds() { 
    return this.noSeeds; 
} 

public void setNoSeeds(Integer noSeeds) { 
    this.noSeeds = noSeeds; 
} 

@OneToMany(fetch = FetchType.LAZY, mappedBy = "kind") 
@JsonIgnore 
public List<Sample> getSamples() { 
    return this.samples; 
} 

public void setSamples(List<Sample> samples) { 
    this.samples = samples; 
} 

@Override 
public int hashCode() { 
    int hash = 3; 
    hash = 47 * hash + Objects.hashCode(this.id); 
    return hash; 
} 

@Override 
public boolean equals(Object obj) { 
    if (this == obj) { 
     return true; 
    } 
    if (obj == null) { 
     return false; 
    } 
    if (getClass() != obj.getClass()) { 
     return false; 
    } 
    final Kind other = (Kind) obj; 
    if (!Objects.equals(this.id, other.id)) { 
     return false; 
    } 
    return true; 
} 

@Override 
public String toString() { 
    return "Kind{" + "id=" + id + ", name=" + name + ", germinationDays=" + germinationDays + ", firstCount=" + firstCount + ", replicates=" + replicates + ", dark=" + dark + ", chill=" + chill + ", temperature=" + temperature + ", substrate=" + substrate + ", noSeeds=" + noSeeds + '}'; 
} 

}

+0

大家好,我仍在爲此而苦苦掙扎。我已檢查控制器是否發送了正確的值,但在'select'中選擇的選項仍然是第一個選項,而不是通過購買控制器的選項。這是基本的還是我的問題難以理解? – Nils

回答

0

好吧,我只是找到了解決辦法與我需要在創建值枚舉選擇

public enum SubstrateType{ 
    BP, 
    PP, 
    TP, 
    OGM, 
    Sand; 
}  

在我的控制器:

@ModelAttribute("substrates") 
public SubstrateType[] getSubstrates() { 
    return SubstrateType.values(); 
}  

我知道它應該不枚舉工作,我已經見過這個。無論如何,我認爲這是一個有enum的好習慣。

相關問題