在控制器中获取选定的值 - jsp / spring

时间:2017-02-23 00:42:45

标签: spring jsp

我正在尝试将值从jsp传递给POST控制器。

我有控制器得到:

@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";
}

然后在jsp:

 <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>

我想在POST控制器中获取所选值:

@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}/";
}

但总是空着。 如果你可以指导我如何继续,那将是伟大的。 谢谢!

1 个答案:

答案 0 :(得分:1)

创建一个模型类League.class

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;
    }
}

更改你的获取控制器:

    @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";
    }

更改你的jsp:

<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>

最后是你的帖子控制器:

@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}/";
}