Skip to content

Allow @PathVariable to have default value on @ModelAttribute methods [SPR-12305] #16910

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

Closed
spring-projects-issues opened this issue Oct 6, 2014 · 8 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 6, 2014

Manuel Jordan opened SPR-12305 and commented

The link shared above is going to be used to ask for many "improvements"

Here the summary of my request: If @RequestParam has the option to set a default value, would be viable add the same feature to @PathVariable?

It to work together with @ModelAttribute methods, in the same way @RequestParam does.

The reason in the link, see "Addition" and "Second Addition" sections

I want avoid create many @Controller's


Affects: 4.0.7

Reference URL: http://stackoverflow.com/questions/25900993/spring-mvc-request-scope-trying-to-update-a-command-object-with-binder-setdisa

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

An optional @PathVariable argument on @ModelAttribute methods makes sense. The challenge is that on @RequestMapping methods it doesn't make sense so I don't think an annotation attribute is a choice here. It would help one case but cause confusion for the most common case.

Note that today @PathVariable can be used with Map<String, String> which gives you all path variables. It's less convenient since you have to parse Strings but it's one way.

We can consider some way to allow optional @PathVariable argument on @ModelAttribute methods as long as it is explicit. For example the combination @PathVariable with java.util.Optional from JDK 1.8 on @ModelAttribute methods.

@spring-projects-issues
Copy link
Collaborator Author

Manuel Jordan commented

Good to know it can has a solution, but I don't understand your explanation, in your convenience, would you share a simple snippet code (how a draft in a new comment) to understand your idea and see if it resolves my "Addition" and "Second Addition" sections of my shared link?

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Sure, for example:

@ModelAttribute("deportistaActualizar")
public Deportista populateActualizarFormulario(@PathVariable Map<String, String> pathVars){
    logger.info("populateActualizarFormulario - pathVars: {}", pathVars);
    String id = pathVars.get("id");
    return (id != null) ? this.fakeMultipleRepository.findDeportista(id) : null;
}

@spring-projects-issues
Copy link
Collaborator Author

Manuel Jordan commented

Note that today @PathVariable can be used with Map<String, String> which gives you all path variables. It's less convenient since you have to parse Strings but it's one way.

I don't remember have read an example about that on Spring Reference documentation.

Thanks by the snippet code. I understand now. Since it is a Map<String, String> I am assuming it should work for the following scenario:

it to represents the URL:

deportista/{id}/detalle/something/{abc}

Then:

@ModelAttribute("deportistaActualizar")
public Deportista populateActualizarFormulario(@PathVariable Map<String, String> pathVars){
    logger.info("populateActualizarFormulario - pathVars: {}", pathVars);
    String id = pathVars.get("id");
    String abc = pathVars.get("abc"); //NEW 
    return (id != null) ? this.fakeMultipleRepository.findDeportista(id) : null;
}

Am I correct?

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Yes that's correct. It's basically a way to get all path variables in a map. The same pattern is also supported in other places (e.g. @RequestHeader, @RequestParam).

@spring-projects-issues
Copy link
Collaborator Author

Manuel Jordan commented

Thank you, I have checked the Spring Framework Reference Documentation and there is no an example about @PathVariable working together with Map<String, String> I have no checked the same for @RequestHeader, @RequestParam too

Should I create a new issue?

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Sure, thanks.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Oct 19, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants