简体   繁体   中英

How to pass parameter to @RequestParam from Spring form?

Got a Spring MVC app, two Entities - Player and PlayerDetails, would like to add PlayerDetails to existing Player, but to do so I need to have a player id passed through parameter. But I can't pass a paremeter from JSP page, where I'm using Spring form. Still getting "Required String parameter 'playerId' is not present" . I got the value already on JSP page, but the issue is to pass it through form to method. When I remove @RequestParam from method, and hardcode "theInt2" inside method everything is ok.

PlayerDetailsController.class

  @PostMapping("/savePlayerDetails")
    public String savePlayerDetails(@RequestParam("playerId") String theInt,
                    @ModelAttribute("details") PlayerDetails playerDetails){

     int theInt2 = Integer.parseInt(theInt);

     playerDetailsService.savePlayerDetails(theInt2, playerDetails);

     return "redirect:/players/list";
    }

details-form.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<form:form action="savePlayerDetails" modelAttribute="details" method="POST">

    <form:hidden path="id"/>
    Date of birth: <form:input path="dateOfBirth"/>
    Email: <form:input path="email"/>
    Preferred Foot: <form:input path="preferredFoot"/>
    Short info: <form:input path="shortInfo"/>
    <br><br>
    ${player.playerId}  <!-- just for test, player id is shown here -->

<input type="submit" value="Save" class="save" />
</form:form>

you are not passing playerId from your form, add another input to you form like below

<input type="hidden" name="playerId" value="${player.playerId}"/>

or

<form:hidden path="playerId"/> 

should also work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM