Spring 3 MVC: is it possible to have a Spring form without a 'commandName' binding?

My question is: do I need to have a 'commandName' binding for forms and an input path binding?

I have a workflow that requires users to select data from a series of ajaxy selection boxes. Thus, the shape does not exactly match any model directly. It seems pretty pointless to add another model to accommodate this form, while everything on this form will be controlled by ajax. Now I know that I can always write a classic form and select tags, but I would like to use spring tags here, for example:

<form:options items="${list}" itemLabel="name" itemValue="id"/> 

which makes it easy to fill out form elements. And plus, I would like to maintain consistency in the project and use spring tags.

I would like to know thoughts and opinions on this topic.

Thanks!

PS: I come from rubies against the background of rails and am used to syntactic sugar: P forgive me if the question sounds silly or answers obvious.

+3
source share
2 answers

I don't know if this is useful, but here it is:

If you do not set 'commandName' on the form, the default value for this property will be set to 'command'. So, if you do not install it, the associated data will have the name "command".

If you want, you can set it with the name of the associated data.

==================================================== ==========================

A solution without using data binding would be:

I would add the HttpServletRequest request parameter to the controller method and get a parameter similar to servlets.

 @Controller public class SomeController { @RequestMapping(value = "/formAction", method = RequestMethod.POST) public String controllerMethod(HttpServletRequest request){ // this way you get value of the input you want String pathValue1 = request.getParameter("path1"); String pathValue2 = request.getParameter("path2"); return "successfulView"; } } 

PS: "path1" and "path2" are the names of the paths that you set on the inputs. I know that it doesn't seem to use Spring correctly, but it is a hack that Spring allows us to use.

The form will be this way:

 <form:form method="post" action="/formAction"> <form:input path="path1" /> <form:input path="path2" /> <input type="submit" value="Submit"/> </form:form> 

Hope this is helpful.

+6
source

If we do not add the formName tag to the spring form tags, the exception will be raised as: IllegalStateException: neither the BindingResult nor the regular target command object are available as a request parameter ...

+7
source

All Articles