2017-07-03 8 views
0

オプションが文字列の場合、「選択」で正しい値の選択に問題があります。私はフォーラムで解決策を見つけることができません。コントローラから渡されたオプションの選択

私はコントローラーで 'kind'を渡して、値が正常であることがわかりますが、 'select'でIntegerフィールドだけが正しく選択されています。 Stringを持つものは、常に最初の値を表示し、 'kind'の1回のパスは表示しません。

私はそれが助けることができると思ったコードを追加しました 誰でも助けることができますか?

自分のHTMLコードです。フォームは、多くの「選択」含まれますが、私は2つ、最初の作品を残したが、二番目のは常に最初のオプションを示しています。

<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

こんにちは皆、私はまだこれで苦労しています。コントローラーが適切な値を送信していることを確認しましたが、「選択」で選択したオプションは引き続き最初のオプションであり、コントローラーを購入するオプションではありません。これは基本的なものなのか、それとも私の質問は理解しづらいのですか? – Nils

答えて

0

まあ、私はちょうど私が必要な値を持つ列挙型を作成する解決策を見つけました私のコントローラで選択

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

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

私は前にこれを見てきたように、それが列挙せずに動作するはずです知っています。とにかく私はenumを持つ良い練習だと思います。

関連する問題