Spring to reload the same page when submitting the form

I had a problem creating a Spring controller that will reload the same page when filling out a form. What I want to achieve is a page for changing passwords. He should ask for the old password, new password and new password. When the user fills in everything and submits the form, the action should be the same page, and the controller should be able to show something like "passwords do not match" or "password was successfully changed." I know how to achieve this using multiple controllers, but I think it would be better to use one controller.

Below is my controller:

package controllers; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import models.Password; @Controller public class ChangePassword { @RequestMapping(value = "/changepassword", method = RequestMethod.GET) public ModelAndView changePassword(@ModelAttribute("changepassword")Password password, BindingResult result) { if(password.isEmpty()) { return new ModelAndView("changepassword", "command", new Password()); } else if(password.isValid()) { return new ModelAndView("changepassword", "message", "The password was successfully changed!"); } else{ return new ModelAndView("changepassword", "message", "The passwords did not match and/or the password was not 8 at least 8 characters long."); } } 

The first time the page loads, it displays the form correctly, but if you leave the password blank and then send it, then the following will appear on the glass fish:

"org.springframework.web.util.NestedServletException: request processing failed; the nested exception is java.lang.IllegalStateException: neither the BindingResult nor the simple target for the bean name 'command' is available as a request attribute

I don’t understand why this is happening, I thought that it should show the form, as for the first time.

Actually, I need to get the Password model for the viewer, as well as the text (error message or successful message). I saw people returning cards to the viewer, but I did not succeed.

+4
source share
1 answer

You should send the request as GET and POST. GET to display the page and its messages and POST to send.

You should redirect to avoid the problem of duplicate sending.

and you must use the FlashScope implementation to display the message in the model and view.

See this post for details: Spring MVC custom scope bean

You can do it as follows:

 @Controller public class ChangePassword { @RequestMapping(value = "/changepassword", method = RequestMethod.GET) public ModelAndView renderCPassPage(@ModelAttribute("changepassword")Password password, BindingResult result) { ModelAndView mv = new ModelAndView("changePassword"); mv.addObject("password" password); return mv; } @RequestMapping(value = "/changepassword", method = RequestMethod.POST) public ModelAndView renderCPassPage(@ModelAttribute("changepassword")Password password, BindingResult result) { ModelAndView mv = new ModelAndView("redirect:changepassword"); if(password.isEmpty()) { return new ModelAndView("message", "password Empty"); } else if(password.isValid()) { return new ModelAndView("message", "The password was successfully changed!"); } else{ return new ModelAndView("message", "The passwords did not match and/or the password was not 8 at least 8 characters long."); } } 
+3
source

All Articles