Announcement Announcement Module
Collapse
No announcement yet.
MultiActionController with command object give ClassCastException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MultiActionController with command object give ClassCastException

    Hi! I'm new to Spring, the forums and also speak Spanish, so please forgive me in advance for any mistakes with the post.

    I have a MultiActionController that I'm trying to reuse for differents objects in my app. The idea is to have one super class, and then declare all subclasses only in xml, specifying the object that it controlls as a parameter. Is a little hard to explain, but I have all working, less one thing... I've a problem with command object

    Right now, I have a method like this that works perfectly:

    Code:
    public ModelAndView onSaveForm(HttpServletRequest request, HttpServletResponse response, ItemCategory itemCategory) throws Exception {
    		
    		try {
    			getMapper().insert(itemCategory);
    		} catch (Exception e) {
    			response.setStatus(700);
    			return null;
    		}
    ... more stuff
    }
    BUT, what I really need to have is something like this:

    Code:
    public ModelAndView onSaveForm(HttpServletRequest request, HttpServletResponse response, Object itemCategory) throws Exception {
    		
    		try {
    			getMapper().insert((ItemCategory)itemCategory); //this cast will be made by reflexion, this is just an example
    		} catch (Exception e) {
    			response.setStatus(700);
    			return null;
    		}
    ... more stuff
    }
    When I do that change, the command is REALLY an Object, so I receive a ClassClastException when I try to cast it.
    I've seen examples of this on the web, and no one is complaining about ClassClastException! what I'm doing wrong?? (per example: http://xmx1024.blogspot.com.ar/2006/...ller-with.html)

    My form is declared like this:

    Code:
    <form:form method="post" id="ItemCategory" name="ItemCategory" commandName="itemCategory" >
    my xml:

    Code:
    <bean id="ItemCategoryBean"  class="ar.com.itesa.erp.item.bean.ItemCategory" scope="prototype"  >
        </bean>
    	<bean id="ItemCategory"  class="ar.com.itesa.erp.item.controller.ItemCategoryController" scope="prototype"  >
    		<property name="mapper" ref="ItemCategoryMapper" />
    		<property name="basicObject" ref="ItemCategoryBean" />
    		<property name="viewsPath" value="items/configuration/category" />
        </bean>
        <bean name="/ItemCategory.html" class="org.springframework.web.servlet.mvc.multiaction.MultiActionController">
      		<property name="methodNameResolver" ref="paramResolver" />
      		<property name="delegate" ref="ItemCategory" /> 
    	</bean>
    And this is the method wich puts the data on the form:

    Code:
    public ModelAndView onShowFormForNew(HttpServletRequest request, HttpServletResponse response) throws Exception {
    		ModelAndView model = new ModelAndView(getViewsPath()+"/formNew");
    		model.addObject("itemCategory", new ItemCategory());
    		return model;
    	}
    Please help!

    Thank you in advance.
    Lucy

  • #2
    I've found a solution... maybe not the best one, but it works. I leave it here in case it could be useful for someone

    Now I've made my class to extend MultiActionController and added:

    Code:
    @Override
    	protected Object newCommandObject(Class clazz) throws Exception {
    		return basicObject.getClass().newInstance();
    	}
    AND changed my xml to:

    Code:
    <bean name="/ItemCategory.html" class="ar.com.itesa.erp.item.controller.ItemCategoryController">
      		<property name="methodNameResolver" ref="paramResolver" />
      		<property name="mapper" ref="ItemCategoryMapper" />
    		<property name="basicObject" ref="ItemCategory" />
    		<property name="viewsPath" value="items/configuration/category" />
    </bean>
    Now it's working like I wanted

    Comment

    Working...
    X