I have a page to add a user "/user/userAdd". In GET, i populate a list of Countries. In POST, i validate the User object from the formsubmit. If it has error, i return back to the same page with error msg. My problem is I just do a simple return "/user/userAdd"; the Country list is not populated. If I do a return "redirect:/user/userAdd"; i am loosing the previous user input. How should I handle this?
#RequestMapping(value = "/user/userAdd", method = RequestMethod.GET)
public void getUserAdd(Model aaModel) {
aaModel.addAttribute("user", new User());
List<Country> llistCountry = this.caService.findCountryAll();
aaModel.addAttribute("countrys", llistCountry);
}
#RequestMapping(value = "/user/userAdd", method = RequestMethod.POST)
public String postUserAdd(#ModelAttribute("user") #Valid User user,
BindingResult aaResult, SessionStatus aaStatus) {
if (aaResult.hasErrors()) {
return "/user/userAdd";
} else {
user = this.caService.saveUser(user);
aaStatus.setComplete();
return "redirect:/login";
}
}
I was also facing similar issues in my spring project. I would recommend changing your POST method to following
#RequestMapping(value = "/user/userAdd", method = RequestMethod.POST)
public String postUserAdd(#ModelAttribute("user") #Valid User user,
BindingResult aaResult, Model aaModel, SessionStatus aaStatus) {
if (aaResult.hasErrors()) {
List<Country> llistCountry = this.caService.findCountryAll();
aaModel.addAttribute("countrys", llistCountry);
return "/user/userAdd";
} else {
user = this.caService.saveUser(user);
aaStatus.setComplete();
return "redirect:/login";
}
}
Here, the list is again added to the model and it will also retain the previously selected values(if any) in UI.
Hope this helps
Related
but want to ask one question. First in my app i want to register User in one form and after redirect to another form where User must fill UserDetails form. These are two Entities User and UserDetail with OneToOne Relation. In my second Controller UserDetailController I want to pass UserId property from User, I made research but cant find the answer how. I try with #PathVariable, but its not working.
Here part of my code:
abstract class BaseController {
protected ModelAndView view(String view, ModelAndView modelAndView) {
modelAndView.setViewName(view);
return modelAndView;
}
protected ModelAndView view(String view) {
return this.view(view, new ModelAndView());
}
protected ModelAndView redirect(String route) {
return this.view("redirect:" + route);
}
}
UserController
#GetMapping("/register")
public ModelAndView register() {
return this.view("register");
}
#PostMapping("/register")
public ModelAndView registerConfirm(#ModelAttribute UserRegisterBindingModel userRegisterBindingModel,
HttpSession session, HttpServletRequest request
) {
if (!userRegisterBindingModel.getPassword()
.equals(userRegisterBindingModel.getConfirmPassword())) {
return this.view("register");
}
this.userService.createUser(this.modelMapper.map(userRegisterBindingModel, UserServiceModel.class));
String id = request.getParameter("userId");
return this.redirect("user-profile/" + id);
}
UserDetailController
#GetMapping("/user-profile/{id}")
public ModelAndView add(#PathVariable String id) {
return this.view("user-profile");
}
This code doesn't work, because "id" return NULL. How to pass from register to user-profile/{id} --> id ?
Thаnks for helping me.
In registerConfirmMethod
model.addAttribute("userId", userId);
return new ModelAndView("redirect:/user-profile", model);
In last method you would have to add
ModelAndView modelAndView = new ModelAndView("user-profile");
modelAndView.addObject("id", id);
return modelAndView;
You can use RedirectView something like this
When you add attribute with "id"
variable '{id}' matches with a model attribute, so it will be automatically substituted as pathparam in redirect URL
#PostMapping("/register")
public RedirectView registerConfirm(#ModelAttribute UserRegisterBindingModel userRegisterBindingModel,
HttpSession session, HttpServletRequest request, Model model
) {
this.userService.createUser(this.modelMapper.
map(userRegisterBindingModel, UserServiceModel.class));
String id = request.getParameter("userId");
model.addAttribute("id", id);
RedirectView rv = new RedirectView();
rv.setContextRelative(true);
rv.setUrl("/user-profile/{id}");
return rv;
}
and in your user-profile controller
#GetMapping("/user-profie/{id}")
public ModelAndView add(#PathVariable("id") String id) {
// sout id
return this.view("user-profile");
}
I have a request mapping for a controller let's say it's A, it receives post action and uses its post values as parameter, sometimes the parameters will be very long so that's the reason why it's POST not GET apart from the best practices and security;
RequestMapping(value = "/reports/performance/indicator/{indicatorType}", method = RequestMethod.POST)
public String generatePerformanceReportsIndicator(ModelMap map,HttpServletResponse response, #PathVariable("indicatorType") Long indicatorType,
#RequestParam(value = "siteIds", required = false) List<Long> siteIds,
#RequestParam(value = "timeframeIds", required = false) List<String> timeframeIds,
#RequestParam(value = "showTarget", required = false) String showTarget,Locale locale) {
And then it turned out that in another spring controller I need to forward the request to the first one.
The problem is how I can add post parameters to the request before forwarding it to the first request mapping? is that healthy to say for example?
new FirstController().generatePerformanceReportsIndicator(....);
Given that:
I don't want the first request mapping to use get instead of post of the mentioned reasons.
I don't want to write redundant code by creating another method that extract the parameters as attribute from the model map.
You should not manually call other controllers! What you can do is redirect to them with RedirectAttributes like:
#RequestMapping(value = "/doctor/doEditPatientDetails", method = RequestMethod.POST)
public String editPatientDetails(Model model, #ModelAttribute(value = "user") #Valid User user,
BindingResult result, RedirectAttributes attr, Principal principal) {
if (null != principal) {
if (result.hasErrors()) {
attr.addFlashAttribute("org.springframework.validation.BindingResult.user", result);
attr.addFlashAttribute("user", user);
attr.addAttribute("id", user.getId());
return "redirect:/doctor/editPatient/{id}";
}
}
....
return "redirect:/doctor/patients";
}
#RequestMapping(value = "/doctor/editPatient/{id}", method = RequestMethod.GET)
public String showEditPatient(Model model, #ModelAttribute("id") String id, Principal principal) {
if (null != principal) {
//here you can access the model and do what everything you want with the params.
if (!model.containsAttribute("user")) {
model.addAttribute("user", user);
}
....
}
return "/doctor/editPatient";
}
Note that, to redirect to a link like "redirect:/doctor/editPatient/{id}" you have to add the id in RedirectAttributes. Also not that there are many ways you can achive the same functionality like HttpServletRequest
I'm facing a problem I don't really know how to solve.
I am developing a Bug Tracker (learning purposes only). I have a page to create a new issue and one page to edit an issue. Both, for now, have their own controllers.
EditIssueController.java
#Controller
#RequestMapping(value = "/issues/{issueId}")
#SessionAttributes("issuePackage")
public class EditIssueController {
#Autowired
private IssueService issueService;
[...]
#ModelAttribute("issuePackage")
public IssueTagEnvironment populateIssue (#PathVariable("issueId") Integer issueId) {
IssueTagEnvironment issueTagEnv = new IssueTagEnvironment();
issueTagEnv.setIssue(issueService.getIssueById(issueId));
return issueTagEnv;
}
#InitBinder
public void initBinder (WebDataBinder binder) {
[...]
}
#RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView editIssue (#PathVariable("issueId") Integer issueId,
#ModelAttribute("issuePackage") IssueTagEnvironment issuePackage) {
ModelAndView mav = new ModelAndView("/issues/EditIssue");
[...]
IssueTagEnvironment issueTagEnv = new IssueTagEnvironment();
issueTagEnv.setIssue(issueService.getIssueById(issueId));
[...]
mav.addObject("issuePackage", issueTagEnv);
return mav;
}
#RequestMapping(value = "/edit", method = RequestMethod.POST)
public String updateIssue (#ModelAttribute("issuePackage") IssueTagEnvironment issuePackage,
BindingResult result) {
if (result.hasErrors() == true) {
return "redirect:/issues/{issueId}/edit";
}
issueService.updateIssue(issuePackage.getIssue());
return "redirect:/issues/{issueId}";
}
}
CreateIssueController.java
#Controller
#SessionAttributes("issuePackage")
public class CreateIssueController {
#Autowired
private IssueService issueService;
[...]
#ModelAttribute("issuePackage")
public IssueTagEnvironment populateNewIssue () {
return new IssueTagEnvironment();
}
#InitBinder
public void initBinder (WebDataBinder binder) {
[...]
}
#RequestMapping(value = "/issues/CreateIssue", method = RequestMethod.GET)
public ModelAndView createIssueGet (#ModelAttribute("issuePackage") IssueTagEnvironment issuePackage) {
ModelAndView mav = new ModelAndView("/issues/CreateIssue");
[...]
issuePackage.getIssue().setReporter(SecurityUtils.getCurrentUser());
return mav;
}
#RequestMapping(value = "/issues/CreateIssue", method = RequestMethod.POST)
public String createIssuePost (#ModelAttribute("issuePackage") IssueTagEnvironment issuePackage,
BindingResult result,
SessionStatus status) {
if (result.hasErrors() == true) {
return "redirect:/issues/CreateIssue";
}
[...]
issueService.createIssue(issuePackage.getIssue());
status.setComplete();
return "redirect:/issues/" + issuePackage.getIssue().getId();
}
}
So far everything seems correct (and in indeed works). But here are the dragons:
I am within an "edit" page changing data from an existing issue.
Instead of submitting the changes I decide to press the "Go Back" button from the navigator.
Right after that action (Go Back), I decide to create a new issue and... Here it is! The form to create a new issue isn't empty but filled with the information of the previous "edited-but-not-submitted" issue.
I understand what the problem is: The controller is not completing the session/status by executing status.setComplete().
My question here is, how to solve this problem?
Thanks in advance to the community!
For your current example, it is easy to fix , just change createIssueGet method to :
public ModelAndView createIssueGet () {
ModelAndView mav = new ModelAndView("/issues/CreateIssue");
IssueTagEnvironment issuePackage = new IssueTagEnvironment();
ModelAndView mav = new ModelAndView("/issues/CreateIssue");
mav.addAttribute("issuePackage", issuePackage);
[...]
[...]
}
That way you are sure that you always use a fresh IssueTagEnvironment object in that controller. And Spring will put it in session as you put it in model.
But the problem still remains : if you do not properly call status.setComplete(), you leave in session an object that should not be there, and like you said dragons may be there
I stopped using #SessionAttributes for that reason, and only use a hidden field (for the id) and a Converter from the id to a full object using the service layer, hoping it should be in cache and does not hit the database. Not really nice, but not really worse than that.
I have a page with a form and a table. When i submit the form, i want it to fill the table.
I tried simply returning the view name, but it doesn't go through the "Get" method.
I saw the Post Redirect Get pattern so i tried it, and it effectively refresh the page like it should. But then the validation errors aren't shown in the tags.
I saw elsewhere that you can use RedirectAttributes and flashAttribute the bindingResult, but it's still not working.
I don't know what is the normal way of doing this thing.
Here's my code :
#Controller
#RequestMapping("/settings")
public class SettingsController {
#Autowired
protected SettingsService settingsService;
#RequestMapping(method = RequestMethod.GET)
public void loadSettings(Model model) {
model.addAttribute("settings", new Settings());
model.addAttribute("settingsList", settingsService.getAllSettings();
}
#RequestMapping(value = "/add", method = RequestMethod.POST)
public String saveSettings(#ModelAttribute("settings") #Valid Settings settings, Errors errors, RedirectAttributes redirectAttributes) {
// code
redirectAttributes.addFlashAttribute("settings", settings);
redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.settings", errors);
return "redirect:/settings";
}
}
I made it work, but i have NO idea why :
Instead of this :
#RequestMapping(method = RequestMethod.GET)
public void loadSettings(Model model) {
model.addAttribute("settings", new Settings());
model.addAttribute("settingsList", settingsService.getAllSettings();
}
I have this :
#RequestMapping(method = RequestMethod.GET)
public void loadSettings(Model model) {
if (!model.containsAttribute("settings")) {
model.addAttribute("settings", new Settings());
}
model.addAttribute("settingsList", settingsService.getAllSettings();
}
And it works, but the form keeps the data posted. It's one or the other :/
I have been looking for a way to somehow reduce the amount of code that is duplicated with subtle variance in my Spring MVC controllers, but searching through the SO questions so far has only yielded some questions without any satisfactory answers.
One example of duplication that I want to remove is this, where the user creation page and the role creation page share similarities:
#RequestMapping(value = "user/create", method = RequestMethod.GET)
public String create(#ModelAttribute("user") User user, BindingResult errors) {
LOG.debug("Displaying user creation page.");
return "user/create";
}
#RequestMapping(value = "role/create", method = RequestMethod.GET)
public String create(#ModelAttribute("role") Role role, BindingResult errors) {
LOG.debug("Displaying role creation page.");
return "role/create";
}
A slightly more involved variant of duplication that I would like to remove is the one for posting the create form:
#RequestMapping(value = "user/create", method = RequestMethod.POST)
public String save(#ModelAttribute("user") User user, BindingResult errors) {
LOG.debug("Entering save ({})", user);
validator.validate(user, errors);
validator.validatePassword(user, errors);
validator.validateUsernameAvailable(user, errors);
String encodedPassword = encoder.encode(user.getPassword());
user.setPassword(encodedPassword);
if (errors.hasErrors()) {
return create(user, errors);
} else {
service.save(user);
}
return "redirect:/user/index/1";
}
#RequestMapping(value = "role/create", method = RequestMethod.POST)
public String save(#ModelAttribute("role") Role role, BindingResult errors) {
LOG.debug("Entering save({})", role);
validator.validate(role, errors);
if (errors.hasErrors()) {
return create(role, errors);
} else {
service.save(role);
}
return "redirect:/index";
}
This example includes a validate then save if correct and a redirect to the error page if things don't go as planned.
How to remove this duplication?
Spring uses your handler method parameter types to create class instances from the request parameters or body. As such, there is no way to create a handler (#RequestMapping) method that could take an Object and check if it is either a Role or a User. (Technically you could have both parameters and just check which one isn't null, but that is terrible design).
Consequently, you need a handler method for each. This makes sense since, even through the logic is similar, it is still specific to the exact type of model object you are trying to create. You perform different validation, call a different service method, and return a different view name.
I say your code is fine.
Thought I would provide the solution that I settled on in the hope that it might help someone. My gf suggested that I use the name of the entity as a path variable for the controller, and this has proved to provide a very nice solution for the problem at hand.
The two methods now look like this:
#RequestMapping(value = "{entityName}/create", method = RequestMethod.GET)
public String create(#PathVariable("entityName") String entityName, #ModelAttribute("entity") BaseEntity entity, BindingResult errors) {
LOG.debug("Displaying create page for entity named: [{}]", entityName);
return handlerFactory.getHandler(entityName).getCreateView();
}
#RequestMapping(value = "{entityName}/create", method = RequestMethod.POST)
public String save(#PathVariable("entityName") String entityName, #ModelAttribute("entity") BaseEntity entity, BindingResult errors) {
LOG.debug("Saving entity of type {}", entityName);
CrudHandler handler = handlerFactory.getHandler(entityName);
handler.getCreateValidator().validate(entity, errors);
if (errors.hasErrors()) {
return create(entityName, entity, errors);
}
handler.preSave(entity);
handler.getService().save(entity);
return "redirect:" + DASHBOARD_URL;
}
The CrudHandler interface has implementations for each entity, and provides the controller with the entity specific classes that it needs, such as service and validator. A sample CrudHandler implementation looks like this for me:
#Component
public class RoleCrudHandler implements CrudHandler {
private static final String ENTITY_NAME = "role";
public static final String CREATE_VIEW = "role/create";
public static final String EDIT_VIEW = "role/edit";
#Resource
private RoleService roleService;
#Resource
private RoleValidator validator;
#Resource
private CrudHandlerFactory handlerFactory;
#PostConstruct
public void init() {
handlerFactory.register(ENTITY_NAME, this);
}
#Override
public GenericService getService() {
return roleService;
}
#Override
public Validator getCreateValidator() {
return validator;
}
#Override
public Validator getUpdateValidator() {
return validator;
}
#Override
public BaseEntity createEntity() {
return new Role();
}
#Override
public void preSave(BaseEntity entity) {
}
#Override
public String getCreateView() {
return CREATE_VIEW;
}
#Override
public String getUpdateView() {
return EDIT_VIEW;
}
}
If someone sees some ways to improve this, feel free to share. Hope this will be of use for someone.