23
votes

i notice that this controller has now been deprecated in the latest spring and was wondering what the alternative controller is?

3
Bear in mind that wile the deprecated stuff still works in Spring 3, the Spring guys are much more aggressive in removing deprecated APIs than the JavaSE people, i.e they actually will remove them.skaffman
Yup SimpleFormController is no longer supported (tried with 4.0.4.RELEASE).Aniket Thakur

3 Answers

28
votes

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:

  • /book - POST: to create a book
  • /book/form - GET: to get the form for creation

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
    }
}
6
votes

Annotated POJOs can act as controllers; see @Controller.

2
votes

In Spring 3.0, your Controllers should no longer inherit from a base class. The standard way is to use annotated controllers.