Having multiple forms on Spring MVC

I am working with Spring MVC 2.5.

Often I use only one form in all my JSPs. Now I need to add another form to the same JSP.

My question is: Will Spring MVC support the same lifecycle method, whatever form I submit?

I mean the same data bindings, validation, error handling, form controller, etc.

<div> <form:form method="post" commandName="station"> </form> </div> <div> <form:form method="post" commandName="fields"> </form> </div> 
+4
source share
2 answers

The old controllers you use can only support one command object and, therefore, only one form at a time. @ Arthur's answer shows that you cannot put both forms on the same page, because any given controller will supply only one form object, you will never receive both assets at the same time.

If you want to do this, you will have to stop using the old controllers (which are deprecated in Spring 2.5 and deprecated in Spring 3), and also use annotated controllers, which are much more flexible. You can mix your shapes pretty much as you would like.

+3
source

Spring SimpleFormController supports only one Command object. Therefore, if you want to use two different Spring forms in the same JSP, you need to create two SimpleFormController. And inside your JSP, do the following to avoid some exception

 <c:if test="${not empty station}"> <div> <form:form method="post" commandName="station"> </form> </div> </c:if> <c:if test="${not empty fields}"> <div> <form:form method="post" commandName="fields"> </form> </div> </c:if> 
+3
source

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


All Articles