我是Spring和Spring MVC的新手。我正在尝试实现一个基本的Web应用程序来测试这个框架的功能,我发现了一些困难。
我发现从版本3开始,注释带来了许多优点,因此控制器类不必实现抽象类(即SimpleFormController
),但这意味着没有强制方法可以实现。
所以我的问题是:应该在控制器类中实现哪些常用的有用方法?
答案 0 :(得分:2)
您只需要实施您希望与各种网页中的操作相对应的方法 - 例如接受表格的输入。
你有什么具体的困难?
答案 1 :(得分: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
,它告诉我们在请求网址为/helloWorld
答案 2 :(得分: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);
}
}