参数未传递给控制器​​ - Spring

时间:2012-10-19 06:14:29

标签: spring-mvc controller

我在这里结束了。这似乎是一件非常微不足道的事情,但它不起作用。我有一个jsp页面,它将一个Long(tournamentId)和一个Objects列表传回服务器。 当我发布表单时,列表正确传递,但Long成员返回null,即使我看到它已被发送。

jsp:

<form:form method="post" action="addBets" modelAttribute="gwbCollection">
<c:choose>
<c:when test="${gwbCollection.tournamentState == 'CLOSED_FOR_BETS'}">
        <br>
    </c:when>
</c:choose>
<input name="tournamentId" value="${gwbCollection.tournamentId}" type="hidden"/>
    <table>
        <tr>
            <td>Side A:</td>
            <td>Score A:</td>
            <td>Side B:</td>
            <td>Score B:</td>
        </tr>
        <c:forEach var="gwb" items="${gwbCollection.games}" varStatus="status">
            <tr>
                <td><input name="games[${status.index}].game.gameId" value="${gwb.game.gameId}" type="hidden"/>
                    <input name="games[${status.index}].userId" value="${gwb.userId}" type="hidden"/>
                    <input name="games[${status.index}].game.tournamentId" value="${gwb.game.tournamentId}" type="hidden"/>
                    <input name="games[${status.index}].bet.betId" value="${gwb.bet.betId}" type="hidden"/>
                    ${gwb.game.sideA}</td> 
                <td><input name="games[${status.index}].bet.scoreA" value="${gwb.bet.scoreA}"/></td>
                <td>${gwb.game.sideB}</td> 
                <td><input name="games[${status.index}].bet.scoreB" value="${gwb.bet.scoreB}"/></td>
            </tr>
        </c:forEach>
    </table>
    <c:choose>
        <c:when test="${gwbCollection.tournamentState == 'OPEN_FOR_BETS'}">
            <input type="submit" />
        </c:when>
    </c:choose>
</form:form>

控制器:

@Controller
@SessionAttributes
public class BetController {
...
@RequestMapping(value = "/addBets", method = RequestMethod.POST)
public String addBet(@ModelAttribute("gwbCollection") GamesWithBetsCollection gwbCollection) {
    List<Bet> bets = gwbUtil.getBets(gwbCollection);
...
}

最后,GamesWithBetsCollection:

public class GamesWithBetsCollection {
private TournamentState tournamentState;
private Long tournamentId;
private List<GameWithBet> games;

public GamesWithBetsCollection() {

}

public List<GameWithBet> getGames() {
    return games;
}

public void setGames(List<GameWithBet> games) {
    this.games = games;
}

public TournamentState getTournamentState() {
    return tournamentState;
}

public void setTournamentState(TournamentState tournamentState) {
    this.tournamentState = tournamentState;
}

public Long getTournamentId() {
    return tournamentId;
}

public void setTournamentId(long tournamentId) {
    this.tournamentId = tournamentId;
}

}

1 个答案:

答案 0 :(得分:0)

尼克多斯 - 哇!这就是答案!美丽的捕获!

回顾一下 - 字段“tournamentId”被定义为Long(对象),但是setter有long(primitive)作为参数。将它改为Long(对象)就可以了。

再次感谢Nickdos!