2017-04-09 211 views
0

在Spring Boot網絡應用程序中,User想重置他的密碼,因此他輸入Reset password頁面。現在我想讓他鍵入他的電子郵件地址,按Reset,我想重定向到myapp/resetPassword?email=HIS_EMAIL以處理密碼重置。請求參數與thymeleaf

我的代碼:

@RequestMapping(value = "/resetPassword", method = RequestMethod.GET) 
public String resetPasswordForm(Model model){ 
    model.addAttribute("email", new String()); 
    return "resetPassword"; 
} 

@RequestMapping(value = "/resetPassword", method = RequestMethod.POST) 
public String resetPassword(@RequestParam("email") String email) { 
    User user = userService.findUserByEmail(email); 
    //playing with logic 
    return "redirect:/"; 
} 

我怎樣才能實現呢我thymeleaf網頁上?我試過這樣的:

<form th:action="@{/resetPassword(email=${email})}" method="post"> 
    <input type="email" th:field="${email}" th:placeholder="Email" /> 
     <div class="clearfix"> 
      <button type="submit">Reset</button> 
     </div> 
</form> 

不幸的是我的email總是空。有人可以幫忙嗎?

回答

4

「th:field」僅適用於Entity-Beans。這應該工作:

@GetMapping(value = "/resetPassword") 
public String resetPassword(@RequestParam(value="email",required=false) String email) { 
    if(email==null) 
     return "resetPassword"; 
    User user = userService.findUserByEmail(email); 
    //playing with logic 
    return "redirect:/";  
} 

<form th:action="@{/resetPassword}" method="get"> 
    <input type="email" th:name="email" th:placeholder="Email" /> 
    <div class="clearfix"> 
     <button type="submit">Reset</button> 
    </div> 
</form> 

並且不要忘記:Thymeleaf不是Javascript。它在服務器上呈現。認爲像@{/resetPassword(email=${email})}會輸出例如/resetPassword?email=anValueYouAddedToModelInController

+0

'錯誤400':'必需的字符串參數'電子郵件'不存在' – crooked

+0

抱歉,我的錯。我編輯了@RequestParam。現在它應該工作。 – benkuly

+0

Yeaa,現在我明白了:)感謝您的幫助!順便說一句。我的代碼實際上很糟糕:) – crooked