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的」可以是比賽,參加者多。通常我可以重複這段代碼很多次,但我認爲有更好的解決方案。
無法理解你的問題....你能解釋一下更多? – Akshay 2013-04-27 19:41:16