i notice that this controller has now been deprecated in the latest spring and was wondering what the alternative controller is?
Asked
Active
Viewed 3.3k times
23
-
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 Jan 19 '11 at 11:01
-
Yup `SimpleFormController` is no longer supported (tried with `4.0.4.RELEASE`). – Aniket Thakur Jul 26 '14 at 19:12
3 Answers
28
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
}
}

Ralph
- 118,862
- 56
- 287
- 383
-
cheers. annotaing mapping is all new to me as i was following this book i got http://www.amazon.co.uk/Spring-Action-Craig-Walls/dp/1933988134/ref=sr_1_1?ie=UTF8&qid=1295447914&sr=8-1 – Jono Jan 19 '11 at 14:39
-
quick question. does that mean i no longer have to map my controllers into beans via xml files? – Jono Jan 19 '11 at 15:17
-
@jonney: Yes, there are new Annotations: @Controller, @Service, @Repository, as well as @Autowire and @Inject. But you have to enable something that is called component scan in your xml. - Have a look at the SpringSource blog (around end of 2009) and the new features section of the Spring reference. -- You are also free to ask a new question. – Ralph Jan 19 '11 at 15:30
-
awsome. this way is definitely easier! i'l have a look at the other annotations in spring. – Jono Jan 19 '11 at 15:59
6
Annotated POJOs can act as controllers; see @Controller
.

duffymo
- 305,152
- 44
- 369
- 561
-
Why on earth was this voted down, a year and a half after it was posted? Were the other answers voted down as well? – duffymo May 29 '12 at 21:42
2
In Spring 3.0, your Controllers should no longer inherit from a base class. The standard way is to use annotated controllers.

Sean Patrick Floyd
- 292,901
- 67
- 465
- 588
-
-
@jonney exactly. the XML way to do it is not even documented anymore in the Spring 3 Reference. – Sean Patrick Floyd Jan 19 '11 at 15:33
-
what about for DI and aspects? all can be defined using annotations now? is there any need to write any spring bean anymore lol? – Jono Jan 20 '11 at 09:40
-
@jonney there are ApplicationContext implementations that don't need XML at all, e.g. [AnnotationConfigApplicationContext](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/annotation/AnnotationConfigApplicationContext.html). But usually, some XML is required (and with Springsource Tool Suite, managing the XML is actually fun) – Sean Patrick Floyd Jan 20 '11 at 09:48