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.
source share