views:

50

answers:

2

Hi All,

I have this question about Spring MVC 2.5.

Oftentimes, I am only using one form in all my JSP. Now I need to add another form into the same JSP.

My question is, will spring mvc still support the same lifecycle method whichever form I submit?

I mean, the same databinding, validation, errorhandling, form controller etc?

<div>
    <form:form method="post" commandName="station">

    </form>
</div>

<div>
    <form:form method="post" commandName="fields">

    </form>
</div>

Please advise. Thanks.

A: 

Spring SimpleFormController just support one kind of Command object. So if you want to use two distinct Spring form into The same JSP, you have to create two SimpleFormController. And inside your JSP, do as follows 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>
Arthur Ronald F D Garcia
Hi Arthur,Just being curious, why is there a need for an if test on the command objects? Thanks.
Mark Estrada
@Mark Estrada Because Spring form Taglib **will evaluate The command provided by yourself** If The command can not be evaluated, Spring will Throw some exception (No command name underblah, blah, blah...). It explains why you should use <c:if test
Arthur Ronald F D Garcia
A: 

The old-style controllers you're using can only support one command object, and therefore only one form, at a time. @Arthur's answer shows that you can't really put both forms on one page, because any given controller will only supply one form object, you'll never get both active at once.

If you want to do this, you're going to have to stop using the old-style controllers (which are obsolescent in Spring 2.5, and deprecated in Spring 3), and use annotated controllers, which are much more flexible. You can mix up your forms pretty much as complex you want.

skaffman