问题
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?
回答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
回答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.
来源:https://stackoverflow.com/questions/14697143/spring-form-without-commandname