Form fields entered in multiple beans with the same property names

So the problem should be very simple. I have form control methods, GET and POST and one bean support form.

Relevant part of the form:

<form:form modelAttribute="newUser" method="post" action="${signUpUrl}">
    <table>

        <tbody>
        <tr>
        <tr>
            <td>Username: </td>
            <td><c:out value="${userForEditBean.username}"/></td>
            <td></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><form:password path="password" size="20"/>
            </td>
            <td><form:errors path="password" cssClass="error"/></td>
        </tr>
        <tr><input id="submitUser" name="Submit" value="Submit" type="submit" />  
        </tbody>
     </table>
</form:form>

GET Method

...
model.addAttribute("newUser", new SignUpBean());
model.addAttribute("test", new InjectTest());
...

Post method

@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String registerUser(@ModelAttribute("test") InjectTest injTest, @ModelAttribute("newUser") SignUpBean signUpBean, BindingResult bindingResult, Model model)
...

So, when I click submit ALL of the beans that I extract from the model using the @ModelAttributePOST method that has the String property password(which are signUpBeanand injTest) have the same contents in the password field, which is the password entered into the form.

I do not want this, I want only the bean marked with the modelAttribute attribute newUser.

I am using Spring 3.2.6

+4
source share
1

InitBinder, InjectTest bean.

@InitBinder("test")
public void initBinder(WebDataBinder binder) {
    binder.setDisallowedFields("password");
}
+2

Source: https://habr.com/ru/post/1527911/


All Articles