0

Hi have an problem with the data submit. I need more than one form for the input fields because of Spring. Nested forms does not work, I get fist part input null or second debugging log says:

User [id=null, name=null, email=null]true Bookingnumber [bookingnumber=123.456,123.456]

is there an better solution for submitting data from multiple inputs?

<c:choose>
    <c:when test="${userForm['new']}">
        <h1>New Booking</h1>
    </c:when>
    <c:otherwise>
        <h1>Update Traveller</h1>
    </c:otherwise>
</c:choose>
<br />

<spring:url value="/users" var="userActionUrl" />
<form:form class="form-horizontal" method="post">

    <form:form class="form-horizontal" method="post"
        modelAttribute="userForm" action="${userActionUrl}">

        <form:hidden path="id" />

        <spring:bind path="name">
            <div class="form-group ${status.error ? 'has-error' : ''}">
                <label class="col-sm-2 control-label">Name</label>
                <div class="col-sm-10">
                    <form:input path="name" type="text" class="form-control "
                        id="name" placeholder="Name" />
                    <form:errors path="name" class="control-label" />
                </div>
            </div>
        </spring:bind>

        <spring:bind path="email">
            <div class="form-group ${status.error ? 'has-error' : ''}">
                <label class="col-sm-2 control-label">Email</label>
                <div class="col-sm-10">
                    <form:input path="email" class="form-control" id="email"
                        placeholder="Email" />
                    <form:errors path="email" class="control-label" />
                </div>
            </div>
        </spring:bind>


    </form:form>
    <!-- TODO bookingnumber in new form bookingForm ... buttons?-->
    <form:form class="form-horizontal" method="post"
        modelAttribute="bookingnumberForm" action="${userActionUrl}">
        <form:hidden path="bookingnumber" />
        <spring:bind path="bookingnumber">
            <div class="form-group ${status.error ? 'has-error' : ''}">
                <label class="col-sm-2 control-label">Bookingnumber</label>
                <div class="col-sm-10">
                    <form:input path="bookingnumber" class="form-control"
                        id="bookingnumber" placeholder="Bookingnumber" />
                    <form:errors path="bookingnumber" class="control-label" />
                </div>
            </div>
        </spring:bind>
        <div class="form-group">
            <!-- TODO move buttons -->
            <div class="col-sm-offset-2 col-sm-10">
                <c:choose>
                    <c:when test="${userForm['new']}">
                        <button type="submit" class="btn-lg btn-primary pull-right">Add</button>
                    </c:when>
                    <c:otherwise>
                        <button type="submit" class="btn-lg btn-primary pull-right">Update</button>
                    </c:otherwise>
                </c:choose>
            </div>
        </div>

    </form:form>
    <!--  -->
</form:form>

edit:

@RequestMapping(value = "/users", method = RequestMethod.POST)
  public String saveOrUpdateUser(@ModelAttribute("userForm") @Validated   User user, @ModelAttribute("bookingnumberForm") @Validated   Bookingnumber bookingnumber,
        BindingResult result, Model model, final RedirectAttributes redirectAttributes) {

    logger.debug("saveOrUpdateUser() : {}", user);

    if (result.hasErrors()) {
        populateDefaultModel(model);
        return "users/userform";
    } else {

        redirectAttributes.addFlashAttribute("css", "success");
        if(user.isNew()){
            redirectAttributes.addFlashAttribute("msg", "User added successfully!");
        }else{
            redirectAttributes.addFlashAttribute("msg", "User updated successfully!");
        }

        userService.saveOrUpdate(user);
        bookingnumberService.save(bookingnumber);

        // POST/REDIRECT/GET
        return "redirect:/users/" + user.getId();

        // POST/FORWARD/GET
        // return "user/list";

    }

}

see also: values in multiple tables spring validation prob

Community
  • 1
  • 1
w3Charlie
  • 85
  • 1
  • 3
  • 17
  • It sounds to me like you are confused about how to map values to form fields without the modelAttribute. there should be no reason to have multiple forms, or embedded forms in one post. Can you explain why you need multiple forms? – ChrisThompson Feb 15 '17 at 14:22
  • It's a good question, if i really need it. Yesterday I had an issue with the validator and "solved" it maybe in not the best way. edit:pasted more code on top .. – w3Charlie Feb 15 '17 at 15:00
  • I would suggest restructuring your JSP. I found a similar answer here: http://stackoverflow.com/questions/13242394/spring-mvc-multiple-modelattribute-on-the-same-form – ChrisThompson Feb 15 '17 at 15:14

0 Answers0