Search code examples
javajspspring-mvchidden-field

Hidden value is in network call but not reaching to spring mvc controller


Passing a hidden field (start and end) from JSP to Spring MVC controller but its not reaching, with my debug point, the value checked is null but after completing the call, these values can be observed in the network call clearly. Why not passing the values of hidden fields with 'rental' object

JSP code,

<body>
    <center>
        <table border="1">
            <tr>
                <th><h2>Name</h2></th>
            </tr>
            <c:forEach items="${vehicles}" var="e">
                <tr>
                    <td>${e.vehicleName}</td>
                </tr>
            </c:forEach>
        </table>

        <br> <br>

        <form:form action="bookVehicle.web" method="post"
            modelAttribute="rental">

            <form:select path="vehicle.vehicleId">
                <form:option value="" label="-- Select" />
                <form:options items="${vehicles}" itemValue="vehicleId"
                    itemLabel="vehicleName" />
            </form:select>
            <br>
            <br>

            <b>Enter userId </b>
            <br>
            <br>
            <form:input path="user.email" />
            <br>
            <br>

            <form:hidden path="start" value="${start}" />
            <form:hidden path="end" value="${end}" />

            <input type="submit" value="book" name="bookVehicleBtn" />
        </form:form>

        <br> <br> <a href="index.web">Return To Home Page</a>
    </center>
</body>

@RequestMapping(value = "/bookVehicle.web", method = RequestMethod.POST, params = "bookVehicleBtn")
    public String bookVehicle(Model model, @ModelAttribute("rental") Rental rental, BindingResult error) {
        System.out.println(" inside controller ");
        model.addAttribute("message", rentalService.proceedBooking(rental));    
        return "message";       
    }

Solution

  • Please make sure you model class fields (start and end) are of type string.