Advertisement
Guest User

Untitled

a guest
Jul 6th, 2012
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. Spring 3.0 MVC binding Enums Case Sensitive
  2. @RequestMapping(method = RequestMethod.GET, value = "{product}")
  3. public ModelAndView getPage(@PathVariable Product product)
  4.  
  5. Unable to convert value "home" from type 'java.lang.String' to type 'domain.model.product.Product'; nested exception is java.lang.IllegalArgumentException: No enum const class domain.model.product.Product.home
  6.  
  7. public class ProductEnumConverter extends PropertyEditorSupport
  8. {
  9. @Override public void setAsText(final String text) throws IllegalArgumentException
  10. {
  11. setValue(Product.valueOf(WordUtils.capitalizeFully(text.trim())));
  12. }
  13. }
  14.  
  15. <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  16. <property name="customEditors">
  17. <map>
  18. <entry key="domain.model.product.Product" value="domain.infrastructure.ProductEnumConverter"/>
  19. </map>
  20. </property>
  21. </bean>
  22.  
  23. @InitBinder
  24. public void initBinder(WebDataBinder binder)
  25. {
  26. binder.registerCustomEditor(Product.class, new ProductEnumConverter());
  27. }
  28.  
  29. @InitBinder
  30. public void initBinder(WebDataBinder binder) {
  31.  
  32. binder.registerCustomEditor(Product.class,
  33. new CaseInsensitivePropertyEditor());
  34. }
  35.  
  36. public class ProductEditor extends PropertyEditorSupport{
  37.  
  38. @Override
  39. public void setAsText(final String text){
  40. setValue(Product.valueOf(text.toUpperCase()));
  41. }
  42.  
  43. }
  44.  
  45. @Override
  46. public void setAsText(final String text){
  47. Product product = null;
  48. for(final Product candidate : Product.values()){
  49. if(candidate.name().equalsIgnoreCase(text)){
  50. product = candidate;
  51. break;
  52. }
  53. }
  54. setValue(product);
  55. }
  56.  
  57. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  58. <property name="webBindingInitializer">
  59. <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
  60. <property name="propertyEditorRegistrars">
  61. <list>
  62. <bean class="com.xyz.MyPropertyEditorRegistrar"/>
  63. </list>
  64. </property>
  65. </bean>
  66. </property>
  67. </bean>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement