0

私は春の新人です。次のコントローラーがあります。

1) パーティーをデータベースに追加する単純なコントローラー。

@Controller
@RequestMapping("/party")
@SessionAttributes()
public class PartyController {

    @RequestMapping(value = "/new", method=RequestMethod.GET)
    public ModelAndView newPartyGet(@ModelAttribute("party")
        Party party, BindingResult result) throws IOException{
        ModelAndView mav = new ModelAndView("/views/party/party_add.jsp", "party", party);
        return mav;
    }
    @RequestMapping(value = "/new", method=RequestMethod.POST)
    public String newPartyPost(@ModelAttribute("party") Party party,
        HttpServletRequest req) throws IOException{
        if (party.validate()) {
            //here is called servlet, which add party to google app engine database
            return "forward:/partyadd";
        } else {
            party.fromRequest(req);
            return "/views/party/party_add.jsp";
        }
    }

2) 同じですが、コンテストがあります。

@Controller
@RequestMapping("/party/{id}/contest")
@SessionAttributes()
public class ContestController {
    @RequestMapping(value = "/new", method=RequestMethod.GET)
    public ModelAndView newContestGet(@ModelAttribute("contest")
        Contest contest, BindingResult result) throws IOException{
        ModelAndView mav = new ModelAndView("/views/contest/contest_add.jsp", "contest", contest);
        return mav;
    }
    @RequestMapping(value = "/new", method=RequestMethod.POST)
    public String newContestPost(@ModelAttribute("contest") Contest contest,
        HttpServletRequest req) throws IOException{
        if (contest.validate()) {
            //here is called servlet, which add contest to google app engine database
            return "forward:/contestadd";
        } else {
            party.fromRequest(req);
            return "/views/contest/contest_add.jsp";
        }
    }

さらに、プローブまたは参加者を追加します。

これらのコントローラーの違いは目立たないので、このフォームを提供できるようにする 2 つのメソッドを持つ 1 つのコントローラーを作成したいと思います。

出来ますか?

わかりました、私が言いたいことをあまりうまく説明していませんでした。

私はたくさんのコントローラーを持っています、すべてこのように見えます:

@Controller
@SessionAttributes()
public class MyObjectController {
    @RequestMapping(value = "/new", method=RequestMethod.GET)
    public ModelAndView MyObjectGet(@ModelAttribute("myObject")
        MyObject myObject, BindingResult result) throws IOException{
        ModelAndView mav = new ModelAndView("/views/myObject_add.jsp", "myObject", myObject);
        return mav;
    }
    @RequestMapping(value = "/new", method=RequestMethod.POST)
    public String MyObjectPost(@ModelAttribute("myObject") MyObject myObject,
        HttpServletRequest req) throws IOException{
        if (myObject.validate()) {
            //here is called servlet, which add myObject to google app engine database
            return "forward:/myObjectadd";
        } else {
            myObject.fromRequest(req);
            return "/views/myObject_add.jsp";
        }
    }

「myObject」には、コンテスト、参加者などを指定できます。通常、このコードを何度も繰り返すことができますが、より良い解決策があると思います。

4

1 に答える 1