@RequestMapping注释忽略POST,Spring

时间:2018-03-02 15:23:39

标签: java spring jsp spring-mvc

我的控制器:

@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
}

我的register.jsp只是一个常规表单,我之前有这个代码工作但它映射到" /"并搞砸了我的其他地图。单击POST表单(如果成功)我希望它将用户返回到/ profile,如果不成功继续/注册...我该怎么做?

1 个答案:

答案 0 :(得分:0)

你可以像这样使用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>