Spring SimpleFormController 3.0 [英] Spring SimpleFormController in 3.0
问题描述
我注意到这个控制器现在已经在最近的春天被弃用,并且想知道替代控制器是什么?
i notice that this controller has now been deprecated in the latest spring and was wondering what the alternative controller is?
推荐答案
在Spring 3.0中,您应该使用由 @Controller
注释的简单类。这种控制器可以处理多个请求。每个请求都由自己的方法处理。这些方法由 @RequestMapping
注释。
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
.
你需要重新思考的一件事是,这是一所旧学校 SimpleFormController
处理很多不同的请求(至少:一个获取表单,第二个提交表单)。你必须手工处理这个问题。但请相信我这更容易。
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.
例如,REST风格的这个控制器将处理两个请求:
For example this Controller in REST Style, will handle two requests:
- / book - POST:创建一本书
- / book / form - 获取:获取创建表单
Java代码:
@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
}
}
这篇关于Spring SimpleFormController 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!