简体   繁体   中英

How can I have a model attribute persist between redirects?

I am trying to write a password reset function for a website. I am running into an issue that I am using a couple of redirects to transition from postmappings to getmappings and they dont seem to be carrying the attributes they need to with them, namely the user object that I am trying to reset the password form, here is an example of one of my mappings:

@PostMapping("/user/forgot")
    public String emailCheck (@RequestParam String email, Model model){
        User user = userDao.findByEmail(email);
        if (user==null){
            model.addAttribute("wrongEmail", true);
            return "redirect:/user/forgot";
        }
        else {
            model.addAttribute("user", user);
            return "redirect:/verifysecurity";
        }
    }

And here is the template where I then call the user attribute:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head th:replace="fragments/header :: header('Security', '')"></head>
<body>
    <div th:replace="fragments/navbar :: navbar"></div>
    <h1 th:if="${wrongAnswer}">Answer does not match record on file, please try again</h1>
    <h1>Please answer your security question: WHat is your best friends name?</h1>
    <form id="passwordForm" th:action="@{/verifysecurity}" th:method="post">
        <label for="answer">Answer</label>
        <br/>
        <input type="text" id="answer" name="answer"/>
        <input type="hidden" name="user" id="user" th:value="${user}"/>
        <input type="submit" class="btn btn-block btn-primary" value="Request"/>
    </form>
</body>
</html>

Then on the next mapping afterwards I get a null pointer exception for the user:

@PostMapping("/verifysecurity")
    public String verify (Model model, @RequestParam User user, @RequestParam String answer){
        String security = user.getSecurity_question();
        if (answer.equals(security)){
            model.addAttribute("user", user);
            return "redirect:/reset/password";
        } else {
            model.addAttribute("wrongAnswer", true);
            model.addAttribute("user", user);
            return "redirect:/verifysecurity";
        }
    }

How can I fix this, and if model attributes won't work what should I be doing instead?

Use spring RedirectAttributes.addFlashAttribute() , as name suggested it's stored in flashmap which internally uses user session to pass on this data to next redirect, and removes ones data is used.

Example from spring doc:

@RequestMapping(value = "/accounts", method = RequestMethod.POST)
 public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) {
   // Save account ...
   redirectAttrs.addFlashAttribute("message", "Account created!");
   return "redirect:/accounts/{id}";
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM