@RequestMapping注释忽略了 POST,春天



My Controller:

@Controller
@RequestMapping("/registration")
public class RegisterController {
@RequestMapping("")
public String register() {
    return "register";
}
@RequestMapping(value = "", method = RequestMethod.POST)
public ModelAndView registerUser(@RequestParam("emailsignup") String email,
                           @RequestParam("firstname") String firstName,
                           @RequestParam("lastname") String lastName,
                           @RequestParam("passwordsignup") String password) throws SQLException, ClassNotFoundException {
    ModelAndView modelAndView = new ModelAndView();
    .... SQL code that inserts if it can
}

我的寄存器.jsp只是一个常规形式,我之前有这个代码工作,但它映射到"/"并且弄乱了我的其他映射。当单击POST表单(如果成功(时,我希望它将用户返回到/profile,如果不成功,则留在/registration...我该怎么做?

你可以

像这样使用HttpRedirectAttributes:

@RequestMapping(value = "", method = RequestMethod.POST)
public ModelAndView registerUser(@RequestParam("emailsignup") String email,
                           @RequestParam("firstname") String firstName,
                           @RequestParam("lastname") String lastName,
                           @RequestParam("passwordsignup") String password,
                           RedirectAttributes redirect) throws SQLException, ClassNotFoundException {
               redirect.addFlashAttribute("emailsignup", emailsignup);
               redirect.addFlashAttribute("firstname", emailsignup);
               // Next 
               // Or redirect.addFlashAttribute("profile", profileObject)
               return modelAndView.setViewName("redirect:/profile");
    }

然后在您的 jsp 中恢复如下属性:

<label>${emailsignup} </label>

<label>${profileObject.emailsignup}</label>

最新更新