Spring SimpleFormController in 3.0

后端 未结 3 792
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 10:51

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

相关标签:
3条回答
  • 2020-12-10 11:16

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-10 11:16

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

    0 讨论(0)
  • 2020-12-10 11:27

    Annotated POJOs can act as controllers; see @Controller.

    0 讨论(0)
提交回复
热议问题