Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...