Spring form without commandName

10,273

Solution 1

If you don't need a command object at all, then avoid the Spring form and simply use an HTML form.

So, change

<form:form action="getReportFile.html" method="post">
     .
     .
     .
</form:form>

to

<form action="getReportFile.html" method="post">
     .
     .
     .
</form>

The command object is indeed not mandatory. It is mandatory only if you use the Spring's form like <form:form></form:form> using the following library.

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

You have to receive the request parameters using the request.getParameter("paramName") method, if you use an HTML form.


if you don't have a form backing bean, you can't use the Spring tag since it does require one! Your "path" attribute on that tag is supposed to specify the path to the model bean's property for data binding.

http://forum.springsource.org/showthread.php?83532-how-to-have-form-without-command-object&p=279807#post279807

Solution 2

I don't know if it's possible to make a form with no backing bean in Spring MVC, but I think you won't get much out of Spring MVC without using a bean. Those form tags you are using (like form:input) get bound to properties of the backing bean, and this lets you attach things like validation, error messages, type conversion, etc. Is your intent simply to post the form to a Spring controller, and handle all the form fields on the server with code like "request.getParameter('field1')"? Then I would suggest just using a plain HTML form.

Share:
10,273
Christian Vielma
Author by

Christian Vielma

I'm a person who like to do the right thing every day. Aristotle one day said "We are what we repeatedly do.Excellence, then, is not an act, but a habit.". I truly believe in this. I'm mainly what's considered a back-end developer. I've worked on some projects, with more emphasis lately on distributed applications and enterprise systems. Lately I've been working also on systems orchestration, streaming and microservices development. I also like to talk about computer science topics on my Youtube channel "A Dev's Story"

Updated on June 14, 2022

Comments

  • Christian Vielma
    Christian Vielma almost 2 years

    I'm new to Spring, and I have a problem. I have a form which is used to send information to the controller. I don't need or want to have a bean backing up the form so I left the commandName attribute in the form blank like this:

    <form:form action="getReportFile.html" method="post">
                <table>
                    <tr>
                        <td><form:label path="field1">Field1:</form:label></td>
                    </tr>
                    <tr>
                        <td><form:select path="field1" items="${FieldMap}" />                        
                        </td>
                    </tr>
                   <tr>
                       <td><form:label path="field2">Field2:</form:label></td>
                   </tr>
                   <tr>
                       <td><form:input path="field2"/></td>
                   </tr>
                   <tr>
                       <td><input type="submit" value="Submit" /></td>
                   </tr>
               </table>
           </form:form>
    

    I'm getting the following error:

    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
    

    I could see here that when you don't give a value to commandName it uses the default 'command', but then, Do I have to configure anything else? should I put a 'command' bean in the dispatcher-servlet.xml? How would that bean?

    I just want a form to send the information to the controller. Do I really have to create a bean to back it?

  • Lion
    Lion over 11 years
    You're welcome. If you need server side validation using something like HibernateValidator, then you must have a backing bean as described by @jfrank in his answer. Once upon a time, it was also my requirement to avoid a backing bean (a backing bean was indeed not required because everything on a JSP page was handled only by the Spring JSON).
  • Don Cheadle
    Don Cheadle over 9 years
    so simple yet apparently so uncommon. Every tutorial uses form:form, which brings a lot of binding baggage that isn't often explained. I don't think it's uncommon to want a form that doesn't relate to a model (a search form, a contact form for example) yet almost no tutorial covers it.
  • Sajib Acharya
    Sajib Acharya about 8 years
    @Lion, Hi, I am using the normal HTML Form and using an input tag like <input type="text" name="someInput" />. But my request.getParameter("someInput") always returns null. Any idea?