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
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…