i notice that this controller has now been deprecated in the latest spring and was wondering what the alternative controller is?
Forms in Spring are typically modelled by extending the org.springframework.web.servlet.mvc.SimpleFormController class, but using Spring annotations, they can also be simplified and defined by the @Controller annotation.Without annotations, a SimpleFormController would be defined as below as in both a Java class and as a bean in XML.
to handle forms in spring you need to extend your controller class from simpleformcontroller class. here we will create a user registration form to understand how this works. the simpleformcontroller is deprecated as of spring 3.0 so if you are using spring 3.0 or above use the annotate controllers instead.
Yup SimpleFormController is no longer supported (tried with 4.0.4.RELEASE ). Show activity on this post. In Spring 3.0 you should use simple classes annotated by @Controller. Such controller can handle more than one request. Each request is handled by its own method. These methods are annotated by @RequestMapping.
@Deprecated public class SimpleFormController extends AbstractFormController Concrete FormController implementation that provides configurable form and success views, and an onSubmit chain for convenient overriding. Automatically resubmits to the form view in case of validation errors, and renders the success view in case of a valid submission.
Annotated POJOs can act as controllers; see @Controller
.
In Spring 3.0 you should use simple classes annotated by @Controller
. Such controller can handle more than one request. Each request is handled by its own method. These methods are annotated by @RequestMapping
.
One thing you need to rethink is the fact, that a old school SimpleFormController
handle a lot of different requests (at least: one to get the form and a second to submit the form). You have to handle this now by hand. But believe me it is easier.
For example this Controller in REST Style, will handle two requests:
Java Code:
@RequestMapping("/book/**") @Controller public class BookController { @RequestMapping(value = "/book", method = RequestMethod.POST) public String create( @ModelAttribute("bookCommand") final BookCommand bookCommand) { Book book = createBookFromBookCommand(bookCommand); return "redirect:/book/" + book.getId(); } @RequestMapping(value = "/book/form", method = RequestMethod.GET) public String createForm(final ModelMap modelMap) { modelMap.addAttribute("all", "what you need"); return "book/create"; //book/create.jsp } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With