1

我是 Spring 和 Spring MVC 的新手。我正在尝试实现一个基本的 Web 应用程序来测试这个框架的功能,但我发现了一些困难。

我发现从版本 3 开始,注解带来了很多好处,因此控制器类不必实现抽象类(即SimpleFormController),但这意味着没有强制实现方法。

所以我的问题是:应该在控制器类中实现哪些常用的有用方法?

4

3 回答 3

2

You only have to implement the methods you wish to correspond to the actions from your various webpages - e.g. to accept the input of a form.

What specific difficulties are you having?

于 2012-04-17T10:27:48.813 回答
0

这些方法将具有相关的注释,以表示它们应该为特定的 URL 调用。例如在以下代码中(取自Official Doc.),

@Controller
public class HelloWorldController {

    @RequestMapping("/helloWorld")
    public String helloWorld(Model model) {
        model.addAttribute("message", "Hello World!");
        return "helloWorld";
    }
} 

helloWorld(Model model)只是任何类中的任何方法。它的特别之处在于注释@RequestMapping告诉该方法应该在请求 URL 时调用/helloWorld

于 2012-04-17T10:54:08.573 回答
0

与 Santosh 类似,我建议您查看官方文档和 Javadoc:http ://static.springsource.org/spring/docs/3.0.x/api/org/springframework/web/bind/annotation/RequestMapping.html

基本上,您将在方法上使用注释而不是覆盖方法,并且基于参数注释和方法参数会发生不同的事情。上面的 Javadoc 说明了要使用的等效注释,而不是覆盖 simpleform 方法。

这是我使用 Roo 生成的 CRUD 控制器的完整示例:

@Controller
@RequestMapping("/timers")
public class MyTimer {
    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid Timer timer, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, timer);
            return "timers/create";
        }
        uiModel.asMap().clear();
        timer.persist();
        return "redirect:/timers/" + encodeUrlPathSegment(timer.getId().toString(), httpServletRequest);
    }

    @RequestMapping(params = "form", produces = "text/html")
    public String createForm(Model uiModel) {
        populateEditForm(uiModel, new Timer());
        return "timers/create";
    }

    @RequestMapping(value = "/{id}", produces = "text/html")
    public String show(@PathVariable("id") Long id, Model uiModel) {
        uiModel.addAttribute("timer", Timer.findTimer(id));
        uiModel.addAttribute("itemId", id);
        return "timers/show";
    }

    @RequestMapping(produces = "text/html")
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            uiModel.addAttribute("timers", Timer.findTimerEntries(firstResult, sizeNo));
            float nrOfPages = (float) Timer.countTimers() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            uiModel.addAttribute("timers", Timer.findAllTimers());
        }
        return "timers/list";
    }

    @RequestMapping(method = RequestMethod.PUT, produces = "text/html")
    public String update(@Valid Timer timer, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, timer);
            return "timers/update";
        }
        uiModel.asMap().clear();
        timer.merge();
        return "redirect:/timers/" + encodeUrlPathSegment(timer.getId().toString(), httpServletRequest);
    }

    @RequestMapping(value = "/{id}", params = "form", produces = "text/html")
    public String updateForm(@PathVariable("id") Long id, Model uiModel) {
        populateEditForm(uiModel, Timer.findTimer(id));
        return "timers/update";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
    public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
        Timer timer = Timer.findTimer(id);
        timer.remove();
        uiModel.asMap().clear();
        uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
        uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
        return "redirect:/timers";
    }

    void populateEditForm(Model uiModel, Timer timer) {
        uiModel.addAttribute("timer", timer);
    }
}
于 2012-04-17T11:17:35.537 回答