如何从GET到POST方法获取modelattribute

时间:2018-06-14 11:08:30

标签: java spring spring-mvc

我正在制作一个Spring MVC项目,我希望注册新用户。在我的第一页用户汇总他的详细信息,如全名,地址,移动号码等,当用户输入详细信息时,我将在他的手机和商店OTP上发送OTP在CacheLoader中。然后我重定向otp页面并使用flashAttribute在OTPController GET方法中使用。我想当用户输入OTP时,完整数据进入POST方法,我将验证OTP然后我将完整数据保存在DB中。

UserDetailsController.java

@RequestMapping(method = RequestMethod.POST)
public String addUser(@Valid @ModelAttribute("addUser") UserPojo userPojo, BindingResult bindingResult,
        Model modelMap, final RedirectAttributes redirectAttributes) {

    otpService.clearOTP(userPojo.getPhoneNumber());
    boolean validate = adminUserService.uniqueUserName(userPojo.getUserName());
    if (validate == false) {
        int otp = otpService.generateOTP(userPojo.getPhoneNumber());
        String response = smsService.sendOtp(otp, userPojo.getPhoneNumber());
        if(response.equals("OK")) {
        redirectAttributes.addFlashAttribute("addUser", userPojo);
        logger.debug(addUserPojo.toString());
        return "redirect:/userotp";
        }
        logger.debug("response is-OTP Failed");
        redirectAttributes.addFlashAttribute("error", "Problem is Send OTP");
        return "redirect:/admin/adduser";
    }
    redirectAttributes.addFlashAttribute("error", "UserName Must Be Unique");
    return "redirect:/admin/adduser";
}

OtpController.java

@RequestMapping(value = "/userotp", method = RequestMethod.GET)
public String showAddUser(@ModelAttribute("addUser")  UserPojo userPojo,
        final BindingResult mapping1BindingResult, final Model model, ModelMap map,
        final RedirectAttributes redirectAttributes) {
    logger.debug("Enter in the Method" + this.getClass().getSimpleName());
    logger.debug("Data is" + addUserPojo.toString());
    return "userotp";
}

@RequestMapping(value = "/userotp", method = RequestMethod.POST)
public String validateUser(
        final BindingResult mapping1BindingResult, Model model, final RedirectAttributes redirectAttributes,
        HttpServletRequest request) {
    logger.debug("Enter in the Method" + this.getClass().getSimpleName());
    return "userotp";
}

userotp.html

<form:form  method="post">
        <div class="" role="document" style="padding: 0px 30%;">

            <div class=" box">
                <h5 class="">User Moblie No. Verify</h5>
                <div class="">
                    <label>Moblie Number For Otp</label> <form:input type="number"
                        class="form-control" name="otp" placeholder="Enter OTP"/>
                    <br> <br>
                    <div class="float-right">
                        <button type="reset" class="btn btn-secondary">Reset</button>
                        <button type="submit" class="btn btn-primary">Verify</button>
                    </div>
                </div>
            </div>
        </div>
    </form:form>

我想在OtpController POST方法中获取数据UserDetails。任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

我认为你应该使用@SessionAttributes.To使用@SessionAttributes我们可以在Session中存储modelAttribute,当没有使用modelAttribute时,我们可以删除它 我们可以像这样删除

@RequestMapping(value = "/userotp", method = RequestMethod.POST)
public String validate(@ModelAttribute User user, WebRequest request, SessionStatus status) {
 // your bussiness logic 
status.setComplete();
request.removeAttribute("user", WebRequest.SCOPE_SESSION);
return "userotp";

}