0
jspの値をPOSTコントローラに渡そうとしています。コントローラ - jsp/springで選択された値を取得
私は、コントローラGET持って:私はPOSTコントローラで選択した値を取得しようとしています
<form:form modelAttribute="leaguesMap" action="drop" class="dropdown-leagues" method="POST">
<form:select path="${leaguesMap.value}" id="league-selection" onchange="this.form.submit()" class="form-control select-filter select2-hidden-accessible" aria-hidden="true">
<form:options items="${leaguesMap}" />
</form:select>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form:form>
:
@RequestMapping(value = "/matches/{pageNumber}/drop", method = RequestMethod.POST)
public String games(@ModelAttribute("leaguesMap") String leaguesMap, @PathVariable Integer pageNumber, BindingResult result) {
String fff = leaguesMap;
System.out.println("asadasdadsa"+fff);
return "redirect:/matches/{pageNumber}/";
}
しかし、取得、JSPで
@RequestMapping(value = "/matches/{pageNumber}/", method = RequestMethod.GET)
public String games(@PathVariable Integer pageNumber, Model model) {
....
//build map for dropdown compatitions
HashMap<Integer, String> leaguesMap = new HashMap<Integer, String>();
leaguesMap.put(1, "Top Leagues");
leaguesMap.put(2, "All");
for (Competition competition : competitions) {
if(!leaguesMap.containsKey(competition.getApiId())){
leaguesMap.put(competition.getApiId(), competition.getName());
}
}
model.addAttribute("leaguesMap", leaguesMap);
.....
return "upcomingMatches";
}
そしてを常にnullです。 進め方を教えていただければ幸いです。 ありがとう!あなたのJSP
@RequestMapping(value = "/matches/{pageNumber}/", method = RequestMethod.GET)
public String games(@PathVariable Integer pageNumber, Model model) {
....
//build map for dropdown compatitions
HashMap<Integer, String> leaguesMap = new HashMap<Integer, String>();
leaguesMap.put(1, "Top Leagues");
leaguesMap.put(2, "All");
for (Competition competition : competitions) {
if(!leaguesMap.containsKey(competition.getApiId())){
leaguesMap.put(competition.getApiId(), competition.getName());
}
}
model.addAttribute("leaguesMap", leaguesMap);
model.addAttribute("league", new League());
.....
return "upcomingMatches";
}
を変更します:
<form:form modelAttribute="league" action="drop" class="dropdown-leagues" method="POST">
<form:select path="id" id="league-selection" onchange="this.form.submit()" class="form-control select-filter select2-hidden-accessible" aria-hidden="true">
<form:options items="${leaguesMap}" />
</form:select>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form:form>
そして最後に、あなたのポストコントローラ:
を
すべてのドロップダウンにモデルクラスを設定することは理にかなっていますか? – ygrunin
ドロップダウンごとにモデルを用意する必要はなく、フォームの内容に従ってモデルを作成します。 – mhshimul
フォームなしでjspから値を取得する必要がある場合はどうすればよいですか?出来ますか? – ygrunin