Java 弹簧控制器的主要方法

Java 弹簧控制器的主要方法,java,spring,spring-mvc,controller,Java,Spring,Spring Mvc,Controller,我是Spring和Spring MVC的新手。我正在尝试实现一个基本的web应用程序来测试这个框架的功能,我发现了一些困难 我发现,从版本3开始,注释带来了许多优势,因此控制器类不必实现抽象类(即,SimpleFormController),但这意味着没有强制实现的方法 因此,我的问题是:在控制器类中应该实现哪些常用的有用方法?您只需实现希望与各种网页中的操作相对应的方法-例如,接受表单的输入 您遇到了哪些具体困难?这些方法将具有相关的注释,以指示应为特定URL调用它们。例如,在以下代码中(取自

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

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


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

您只需实现希望与各种网页中的操作相对应的方法-例如,接受表单的输入


您遇到了哪些具体困难?

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


helloWorld(Model-Model)
只是任意类中的任意方法。它的特殊之处在于注释
@RequestMapping
,它告诉我们,当请求URL为
/helloWorld

时,应该调用此方法,类似于Santosh,我建议您查看官方文档和Javadoc:

基本上,您不会覆盖方法,而是在方法上使用注释,并且基于参数注释和方法参数,会发生不同的事情。上面的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);
    }
}

没错,但例如,在构建表单时(经过几次麻烦之后),我发现要将自定义对象集合绑定到select,我必须实现
PropertyEditorSupport
。提前知道使用哪些类或方法来获得一个全功能控制器类可能会有所帮助……我理解,但您对全功能类的看法可能与其他类不同——Spring不会采用Struts等框架的一刀切方法(随机选取一个示例)。我理解这使得在一开始就很难理解,但在以后的代码中会更清晰、更轻巧。我建议看一看他们提供的一些Spring示例应用程序,它们非常有启发性。对于其他问题,这里的人们很乐意提供帮助。
@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);
    }
}