2013-04-27 98 views
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"; 
     } 
    } 

还有其他几个,添加探测器或参与者。

这些控制器之间的区别并不明显,所以我想用两种方法编写一个控制器,它们允许我为这些表单提供服务。

可能吗?

好的,我看到我没有解释得太好,我想说什么。

我有很多的控制器,全部是这样的:

@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的”可以是比赛,参加者多。通常我可以重复这段代码很多次,但我认为有更好的解决方案。

+0

无法理解你的问题....你能解释一下更多? – Akshay 2013-04-27 19:41:16

回答

0

当然,你只需要使用控制器上的A类普通@RequestMapping("/party")然后从ContestController你的方法必须要为

@RequestMapping(value = "/{id}/contest/new", method=RequestMethod.GET) 

@RequestMapping(value = "/{id}/contest/new", method=RequestMethod.POST) 
相关问题