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