2017-02-23 14 views
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> 

そして最後に、あなたのポストコントローラ:

答えて

1

public class League { 
    private Integer id; 
    private String name; 

    public Integer getId() { 
     return id; 
    } 

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

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 
があなたのgetコントローラを変更

モデルクラスLeague.class

を作成します。
@RequestMapping(value = "/matches/{pageNumber}/drop", method = RequestMethod.POST) 
public String games(@ModelAttribute("league") League league, @PathVariable Integer pageNumber, BindingResult result) { 

    System.out.println("asadasdadsa" + league.getId()); 

    return "redirect:/matches/{pageNumber}/"; 
} 
+0

すべてのドロップダウンにモデルクラスを設定することは理にかなっていますか? – ygrunin

+0

ドロップダウンごとにモデルを用意する必要はなく、フォームの内容に従ってモデルを作成します。 – mhshimul

+0

フォームなしでjspから値を取得する必要がある場合はどうすればよいですか?出来ますか? – ygrunin

関連する問題