英文:
Have multiple method=RequestMethod.POST in controller and how to call each of them?
问题
在我的Spring控制器中,我有三个函数:
@Controller
public class Controll {
@RequestMapping(value="/signup",method=RequestMethod.GET)
public String signup(@ModelAttribute("signuper") Signuper signuper, ModelMap model){
if(signuper.getSignupFName()!=null){
//做某事
return "emailForm";
}
//做某事
return "signupForm";
}
@RequestMapping(value="/signup",method=RequestMethod.POST)
public String signup(@Valid Signuper signuper,BindingResult result, ModelMap model,
RedirectAttributes attributes)
{
//做某事
attributes.addFlashAttribute("signuper",signuper);
return "redirect:signup";
}
@RequestMapping(value="/signup",method=RequestMethod.POST, params = "email")
public String signupE(@Valid EmailVerify emailVerify,BindingResult result,ModelMap model) {
//做某事
return "signupSuccess";
}
}
用户在Web浏览器中访问/signup。
调用方法(具有method = GET的signup)。
用户填写“signupForm”,信息传递给方法(具有method = POST的signup)。
方法(具有method = POST的signup)执行验证,如果一切正常,将重定向回具有Signuper信息的方法(具有method = GET的signup)。因为现在signuper.getSignupFName()不会为空,用户必须填写“emailForm”。
如何进入方法(具有method = POST的signupE)?
要回答的主要问题(其他内容是背景信息,请阅读):
-
如何在方法中创建一个参数,以便在它可以调用哪个method = POST方法时进行选择?
-
如何在控制器中有多个method = RequestMethod.POST,并如何调用它们中的每一个?
英文:
In my spring controller, I have three functions
@Controller
public class Controll {
@RequestMapping(value="/signup",method=RequestMethod.GET)
public String signup(@ModelAttribute("signuper") Signuper signuper, ModelMap model){
if(signuper.getSignupFName()!=null){
//dosomething
return "emailForm";
}
//dosomething
return "signupForm";
}
@RequestMapping(value="/signup",method=RequestMethod.POST)
public String signup(@Valid Signuper signuper,BindingResult result, ModelMap model,
RedirectAttributes attributes)
{
//dosomething
attributes.addFlashAttribute("signuper",signuper);
return "redirect:signup";
}
@RequestMapping(value="/signup",method=RequestMethod.POST, params = "email")
public String signupE(@Valid EmailVerify emailVerify,BindingResult result,ModelMap model) {
//dosomething
return "signupSuccess";
}
}
The user goes to /signup in the web browser.
Method(signup with method = GET) gets called.
User fills out "signupForm" and the info goes to Method(signup with method = POST).
Method(signup with method = POST) does validation and if everything is good, it redirects back to Method(signup with method = GET) with Signuper info. Becuase signuper.getSignupFName() will not be null now, user will have to fill "emailForm".
How do I go to Method(signupE with method = POST)?
MAIN QUESTIONS TO ANSWER (everything else was background info, but pls read it) :
-
How do you create a param in a method so that when it can pick which method = POST Method it can call?
-
How to have multiple method=RequestMethod.POST in controller and how to call each of them?
答案1
得分: 0
1. 在方法中如何创建一个参数,以便在可以调用POST方法的时候选择调用哪个方法?
我猜您想在用户填写“emailForm”后调用方法signupE。因此,在“emailForm”中,表单的“action”URL必须附加一个请求参数。如何做到这一点取决于您用于生成HTML页面的服务器端技术。可以是JSP/Thymeleaf等。如果您需要帮助,请指定您正在使用的技术。
我可以看到您在RequestMapping中指定了params = "email",但是您没有在方法参数中指定任何@RequestParam("email")。应该像这样指定:
@RequestMapping(value="/signup", method=RequestMethod.POST, params = "email")
public String signupE(@RequestParam("email") String email, @Valid EmailVerify emailVerify, BindingResult result, ModelMap model) {
// 做一些操作
return "signupSuccess";
}
2. 如何在控制器中使用多个method=RequestMethod.POST,并如何调用它们?
其中一种方法是通过根据请求参数进行区分,使用“params”属性。由于您已经使用了这个属性,我不认为我需要解释它是如何工作的。
英文:
1. How do you create a param in a method so that when it can pick which method = POST Method it can call?
I suppose you want to call the method signupE once the user fills the "emailForm". So in the "emailForm", the form "action" URL would have to be attached with a request parameter. How you do that depends on what server side tech you have used for generating the HTML pages. It can be JSP/Thymeleaf etc. If you need help with that, specify which one you're using.
I can see that you've specified params = "email" in the RequestMapping, but you haven't specified any @RequestParam("email") in the method arguments. It should be specified like:
@RequestMapping(value="/signup",method=RequestMethod.POST, params = "email")
public String signupE(@RequestParam("email") String email, @Valid EmailVerify emailVerify,BindingResult result,ModelMap model) {
//dosomething
return "signupSuccess";
}
2. How to have multiple method=RequestMethod.POST in controller and how to call each of them?
One of the ways to do that is by differentiating them on the basis of request parameters, using the "params" attribute. Since you've already used that, I don't think I should explain how that works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论