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

Apply custom editors to @RequestParam parameters [SPR-4182] #8860

Closed
spring-issuemaster opened this issue Nov 28, 2007 · 8 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Nov 28, 2007

allnightlong opened SPR-4182 and commented

I already have a lot of custom editors, so why can't i use them with params:
Currently:
public void doSmth(@RequestParam("id" )Integer id){
Entity e = dao.get(id);
//.. actual work
}

Suggestion:
public void doSmth(@RequestParam("id" )Entity e){
//.. actual work
}


Affects: 2.5 final

2 votes, 2 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 3, 2007

Paul Mietz Egli commented

I was also surprised that I couldn't register a custom editor for request parameters. Here's some sample code that doesn't work with 2.5:

@Controller
public class TestController {

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}

@RequestMapping("/testDateParam")
protected void testDateParam(HttpServletRequest request,
    @RequestParam("dt")
    Date dt) {
System.err.println("date is "+ dt);
}

}

Calling this with /testDateParam?dt=2007-11-15 gives an exception in TypeConverterDelegate:

Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String[]] to required type [java.util.Date]: no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:237)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107)
at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:47)
... 37 more

Additionally, it seems that the method marked with @InitBinder isn't even called.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 3, 2007

Juergen Hoeller commented

Indeed, in Spring 2.5 final, @InitBinder methods are only called when binding to command/form objects, but not for request parameters. I intend to revise this for Spring 2.5.1.

Juergen

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 6, 2007

Paul Middelkoop commented

Is it also possible to bind form objects that aren't annotated with @ModelAttribute? At the moment you need to create two methods displaying a form ( "populate...") or use an extra, never used, parameter in the form method.

See http://forum.springframework.org/showthread.php?p=154820.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 6, 2007

Paul Middelkoop commented

Ignore my last comment.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 17, 2007

Arnold Smith commented

Any estimation on when this issue will be fixed? Eagerly waiting...

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 19, 2007

Juergen Hoeller commented

I've finally implemented this for Spring 2.5.1: @InitBinder methods apply to request parameters as well, with @InitBinder annotation values matched against request parameter names in this case.

This will be available in tonight's 2.5.1 snapshot (http://static.springframework.org/downloads/nightly/snapshot-download.php?project=SPR). Feel free to give it an early try!

Juergen

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 25, 2007

Arnold Smith commented

Juergen, Thanks for implementing this for 2.5.1.

Just a question... Will WebBindingInitializer registered editors be applied in this case, as well?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 26, 2007

Juergen Hoeller commented

Yes... I forgot to explictly mention this: A specified WebBindingInitializer will be applied to request parameter binding as well. This should already work in the latest 2.5.1 snapshots.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.