Announcement Announcement Module
No announcement yet.
Model Attribute as interface? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Model Attribute as interface?


    I have interfaces for my domain objects because I need to have different implementations. However, I have a form whose modelAttribute is an implemented domain object.

    My problem is that in the Controller when I call:

    	@RequestMapping(method = RequestMethod.POST)
    	public ModelAndView processSubmit(
    			@ModelAttribute("myObject") IObject interface,
    			HttpServletRequest request) throws Exception {}
    I get the error where Spring cannot instantiate an interface. At this point in the controller I don't know what implementation is being passed in. Which is why I wanted to have the interface as the parameter. Can someone please help with this. I do not want to several controllers, I just want one controller and separate forms that can have different implementations of the domain object as the modelAttribute.

    Thank you

  • #2
    Did you ever get an answer to this. I have the same issue.


    • #3
      @randyn: could your please explain your use case a little bit more?
      based on what would you want spring to create the correct instance?

      maybe it can be solved with generics.


      • #4
        I am using a FactoryBean to create instances of the Impl class, so ideally, would like a way for ModelAttribute to do the same