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
Let @ModelAttribute work according with one or many specific @RequestMapping [SPR-12303] #16908
Comments
Rossen Stoyanchev commented The question has come up before. The issue is there is no straight-forward, sure way to reference an |
Manuel Jordan commented Let me create a simple sample app for this weekend and share it later. Thanks in advance |
Manuel Jordan commented Something like this @Controller
public class PersonController {
@ModelAttribute("person")
public Person createPerson()
return new Person();
}
@ModelAtrribute("countries")
public Set<Country> countries(){
return set of countries
}
@ModelAtrribute("languages")
public Set<Languages> languages(){
return set of languages like Spanish English Portuguese etc...
}
@RequestMapping(value="/person/register.htm", for GET)
public String registerPerson(…){
}
@RequestMapping(value="/person/register.htm", for POST)
public String registerPerson(…){
}
@RequestMapping(value="/person/update.htm", for GET)
public String updatePerson(…){
}
@RequestMapping(value="/person/update.htm", for POST)
public String updatePerson(…){
}
@RequestMapping(...)
public String showPersons(…){
...
}
@RequestMapping(GET: to let do a search by name, code, dates etc.. )
public String findPerson(…){
...
}
@RequestMapping(POST: to show person details)
public String findPerson(…){ // or showDetailsPerson(...)
...
}
@RequestMapping(value="" for Get and JSON)
public @ResponseBody Person getPersonJson(@RequestParam ..…){
}
@RequestMapping(...)
public @ResponseBody Set<Person> showPersonsJson( JSON ){
...
}
//more
} Therefore we have the following
Now about the
Therefore, I want avoid create many I thought something like (a new annotation @ModelAttribute("person")
@ForRequestMapping{values={"url1","url2","url3"}}
public Person createPerson()
return new Person();
} Or perhaps (new attribute) @ModelAttribute("person", for-request-mappings={"url1","url2","url3"})
public Person createPerson()
return new Person();
} has sense? (Even more, other problem, what to do in case of have two URL /person/register.htm but for HTTP methods Get and Post) If my requested improvement has no sense, what I should do to around this situation about execute unnecessarily a Thanks in advance |
Rossen Stoyanchev commented @ModelAttribute("person", for-request-mappings={"url1","url2","url3"}) Not an option. It would have no meaning when @RequestMapping("/whatever")
@ModelAttribute("person", for-request-mapping="huh?") As for Outside the specific example, I do see what you're trying to do. As I said this has been requested more than once before. It's not for lack of desire, we just never had a clean way to support it. Perhaps we can do something with Java 8 method references. We just need a good annotation name. |
Manuel Jordan commented About
I did not write about that. So @RequestMapping("/whatever")
@ModelAttribute("person", for-request-mapping="huh?") has no sense for me too.
Yes, but is my worst scenario. The point is keep the high in cohesion in a
of course, no worries, I know you all are very busy.
We know that the default behaviour is: a @ModelAttribute("languages")
public Set<Language> generateLanguages(){
….
return ….
}
@RequestMapping(value="createperson.htm" Get/Post)
@ExecuteModelAttribute(values={this.generateLanguages, this.method02})// needs some improvement
public String createPerson(…){
...
} See the new annotation has sense my idea? |
Rossen Stoyanchev commented
Right but these are the consequences of adding an attribute to
The |
Manuel Jordan commented
Mmm, who should be agnostic?
or
Agree, but both know that starting from Spring 4, it uses Java 8 in peace. For prior releases Spring 3.2.x the developer would use the old school (creating many controllers). It would be consider it for Spring 4.2 or Spring 5. In the same way when |
Rossen Stoyanchev commented Alright we can consider a potential |
Manuel Jordan commented Sounds great!, thank you very much! |
Rossen Stoyanchev commented Actually method references don't really apply for this purpose, i.e. are not usable outside of lambda expressions. |
Manuel Jordan opened SPR-12303 and commented
The link shared above is going to be used to ask for many "improvements"
Here the summary of my request: I want to know if is possible add a new annotation or perhaps a new attribute for
@ModelAttribute
to only be executed according of a specific@RequestMapping
.I could have many
@RequestMapping
's (creation GET/POST, search GET/POST, edit or update GET/POST, reports GET, JSON GET/POST, etc).Therefore for each potential call for any of these
@RequestMapping
's, a possible unneccessary call to a@ModelAttribute
is going to be happenA
@Controller
could has 10@RequestMapping
's and few@ModelAttribute
's (to create an object model and collections) and they should be executed according to only 1 or 2@RequestMapping
's.Therefore for the rest, the others 8
@RequestMapping
, the@ModelAttribute
's are going to be executed unneccessarily.I want avoid create many
@Controller
'sAffects: 4.0.7
Reference URL: http://stackoverflow.com/questions/25900993/spring-mvc-request-scope-trying-to-update-a-command-object-with-binder-setdisa
The text was updated successfully, but these errors were encountered: