1

SpringとSpringMVCは初めてです。このフレームワークの機能をテストするために基本的なWebアプリケーションを実装しようとしていますが、いくつかの問題があります。

バージョン3から、アノテーションが多くの利点をもたらすことを発見しました。そのため、コントローラークラスは抽象クラス(つまりSimpleFormController)を実装する必要はありませんが、これは実装する必須のメソッドがないことを意味します。

だから私の質問は:コントローラークラスに実装する必要がある一般的な便利なメソッドはどれですか?

4

3 に答える 3

2

さまざまなWebページからのアクションに対応させたいメソッドを実装するだけで済みます。たとえば、フォームの入力を受け入れる場合などです。

あなたはどのような特定の困難を抱えていますか?

于 2012-04-17T10:27:48.813 に答える
0

メソッドには、特定のURLに対して呼び出す必要があることを示す関連するアノテーションがあります。たとえば、次のコード(公式ドキュメントから取得)では、

@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 に答える