Results 1 to 5 of 5

Thread: Spring : Command object not getting populated

  1. #1
    Join Date
    Aug 2010
    Posts
    3

    Default Spring : Command object not getting populated

    Hi,

    I have two controllers , a simple form controller and a multiaction controller.

    Now, in simpleformcontroller, i want to redirect a request to multiaction controller.

    Here's code snippet in simpleformcontroller

    Code:
        protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
        MyObject myOb = (MyObject )command;
        system.out.println(myOb.toString);
        ModelAndView mav = new ModelAndView(new RedirectView("another.htm"));
        mav.addObject("Obj",myOb);
        return mav;
        }
    another.htm binds to a method in multiaction controller.

    Code:
        <bean id="MyController" class="MyController">
        <property name="methodNameResolver">
        <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
        				<property name="mappings">
        					<props>
        						<prop key="/another.htm">another</prop>
        					</props>
        		</property>
          </bean>
        </bean>
    and the code in multiactioncontroller is

    Code:
        public class MyController extends MultiActionController {
        	public ModelAndView another(HttpServletRequest request,
        			HttpServletResponse response, MyObject myObj) throws CRPMException  {
        		system.out.println(myObj.toString());
        
        }
        
        
        }
    The output is, all fields of Myobj are nulls in mutiactioncontroller whereas they have valid values when passed in simpleformcontroller.


    Am i missing something here or is this not the right way to pass command objects ?

    Any help is appreciated.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    That is not going to work/happen. A redirect is client side so your model isn't available anymore, only simple attributes (like string, int etc) in the model are appended to the url and are available in your next controller.

    If you want wizard like functionality I strongly suggest the usage of spring web flow.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2010
    Posts
    3

    Default

    HI Marten,

    Thanks for the response.
    Can we use session to store such objects. Is it a good practice. to do that ?

    Meanwhile i'll look into spring webflow as to what can be done.

  4. #4

    Default

    Storing your command object at the session level is pretty much the way to ensure it survives multiple invocation sequences. This is especially true if you are trying to chain calls from one controller to a completely different controller. While WebFlow may make sense, it may also be a more complex solution than is required if you only have a few pages to navigate.

    By the way... did you know that with the annotation based controllers, it's actually possible to combine the form behavior of a SimpleFormController with the multi action capability of a MultiActionController?

    You'd do something like the following
    Code:
    @Controller
    @SessionAttributes ("myCommandObj")
    public class MyController {
    ...
      /*
       * This is roughly analogous to a formBackingObject method in the old 
       * SimpleFormController approach
       */
      @RequestMapping("/somepath" method=RequestMethod.GET)
      public ModelAndView myGetMethodCall() {
         CommandObject commandObj = new CommandObject();
         // Initialize values of the command object
         // The name of the command object set here must match the name listed
         // above in the @SessionAttributes list
         return new ModelAndView("formViewName", "myCommandObj", commandObj);
       }
    
       // Roughly analogous to onSubmit but you can have more than one, much 
       // like a multiaction controller
       @RequestMapping("/postPath1", method=RequestMethod.POST)
       public ModelAndView myPostMethod1(@ModelAttribute("myCommandObj") CommandObject cmdObj, BindingResult result) {
         // Process the submit
         // return view for another form (i.e. page 2)
       }
    
       // Second submit method - both have access to the stored command
       // object because it is marked as a session attribute
       @RequestMapping("/postPath2", method=RequestMethod.POST)
       public ModelAndView myPostMethod2(@ModelAttribute("myCommandObj") CommandObject cmdObj, BindingResult result) {
          // Process the submit
          // Redirect to final page
       }
    }

  5. #5
    Join Date
    Aug 2010
    Posts
    3

    Default

    Hi msecrist,

    Thanks.
    Currently i will go ahead with
    request.getSession().setAttribute("MyObject ", myOb);

    Yes.. i am unfamiliar with either webflow and have not explored annotation based controllers. So this shud be fine i believe

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •