Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why redirectAttributes.addFlashAttribute next return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;? #1544

Open
omelet3032 opened this issue May 21, 2024 · 1 comment · May be fixed by #1553
Open

Why redirectAttributes.addFlashAttribute next return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;? #1544

omelet3032 opened this issue May 21, 2024 · 1 comment · May be fixed by #1553

Comments

@omelet3032
Copy link

@PostMapping("/owners/{ownerId}/edit")
	public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId,
										 RedirectAttributes redirectAttributes) {
		if (result.hasErrors()) {
			redirectAttributes.addFlashAttribute("error", "There was an error in updating the owner.");
			return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
		}

		owner.setId(ownerId);
		this.owners.save(owner);
		redirectAttributes.addFlashAttribute("message", "Owner Values Updated");
		return "redirect:/owners/{ownerId}";
	}

Im looking this code.
if client want to see "There was an error in updating the owner.",
i think that return "redirect:/owners/{ownerId}";' is right, not VIEWS_OWNER_CREATE_OR_UPDATE_FORM;

I dont understand why use 'redirectAttributes.addFlashAttribute'

Maybe Spring developer probably has a specific intent.
Could you check this code?

@dsyer
Copy link
Member

dsyer commented May 28, 2024

You are right that it is a mistake I think, but wrong about the return value - it is better to render the owner details directly if there is an error. The RedirectAttributes are discarded if the response is not a redirect, so the mistake is harmless. If anyone wants to fix it please reference this issue in the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants