tags:

views:

1502

answers:

2

I'm using Spring's SimpleFormController for form processing. In my case, the edit view (JSP) can vary depending on what's being edited. SimpleFormController's formView is a field (class variable) which means that it's shared by all threads that use the instance of it. Thus it's not safe (nor appropriate) to set the formView (via setFormView) in formBackingObject. My question is, is it possible to use SimpleFormController with different edit views based on some context?

Follow up: After looking at the source it appears that I can override showForm(HttpServletRequest req, HttpServletResponse resp, BindException errors) and call showForm(HttpServletRequest req, BindException errors, String viewName) with whatever view I want.

A: 

I don't think it's doable with SimpleFormController.

You could try to stop using the controller hierarchy classes which is about to be deprecated anyway and take a look at annotation based Controllers introduced with Spring 2.5. They allow a much more flexible form handling. Don't be put off if you don't like Auto-injection. Although none of the examples really make it obvious, you can use annotation driven controllers and define all beans in an XML content.

fforw
+1  A: 

I believe SimpleFormController provides two "showForm" protected methods. These can be overridden and the BindException used to retrieve the "target" which is your form object, you can then return a ModelAndView based on any bound form object property. You should also take a look at processFormSubmission, it dictates which methods are called really. Another method is "isFormChangeRequest" which determines whether or not your form should change - you can use this to set this to true and it will then call showForm with the request, response, etc. and you can then re-examine the request.

The only way you'll learn how to use this hierarchy is by examining it. It is not very good - it provides many things, but not very well. Spring MVC typically has to be extended in order for it to be very useful.

MetroidFan2002
I'd be interested to hear more about what you think the packaged Controllers in Spring MVC don't do "very well". Personally I find them pretty useful as a starting point
matt b
They're great as a starting point, but they provide too many protected methods that you have to then trace back to the source of where it is called. Also, if you want to use their controller in a different manner it is difficult to find the side effect. For instance, I had a form controller that I had to store the form object so that it could redirect to another controller which used the object from the session to display the data - but unbeknonst to me, the form controllers removed the object
MetroidFan2002
...Now, it does say this on the Javadoc for some methods, but there are so many protected methods it is difficult to find which ones actually need to be used. The hierarchy is simply too complex - that a class with "Simple" in its name extends six classes (ignoring the Object extension), and has probably around 50 methods is rediculous and is a key indicator of bad design. Too much extension, not enough use.
MetroidFan2002