Results 1 to 5 of 5

Thread: Understanding FormController Flow

  1. #1

    Default Understanding FormController Flow

    Hi,

    I am trying to understand if I am going about it incorrectly.

    I have a page: myPage.jsp, which I get to through Spring. Initially, the page is not supposed to have any data in it - just a form with two drop-downs that you can submit, and then, based on what you chose in the form, the same page will display data populated tables under the form.

    The controller which handles the initial display of the page looks roughly like this in code:

    Code:
    public class MyController extends SimpleFormController 
    {
    	private MyHelper helper;
    	
    	public void setHelper(MyHelper newHelper)
    	{
    		helper = newHelper;
    	}
    	
    	public MyHelper getHelper()
    	{
    		return helper;
    	}
    	
    	
    	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
    	{
    		MyForm formObject = (MyForm ) command;
    		
    		ModelAndView mav = new ModelAndView(getSuccessView());
    		
    		return mav;
    	}
    	
    	protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel)
    	{
    		return onSubmit(request,response,null,errors);
    	}
    	
    	protected Object formBackingObject(HttpServletRequest request)
    	{
    		MyForm pageForm = new MyForm ();
    		
    		String param1= request.getParameter("param1");
    		String param2= request.getParameter("param2");
    		
    		pageForm .setParam1(param1);
    		pageForm .setParam2(param2);
    
    
    		return pageForm ;
    	}
    }
    the problem that I see is that when the page is displayed initially, I got to onSubmit() from showForm(), and command is null. So I have to call formBackingObject from onSubmit(). This is turning very convoluted: calling onSubmit() from showForm(), and formBackingObject() from onSubmit().

    Am I not writing correct code? It just feels like there are things Spring would do for me without the need for cross-calling methods.

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

    Default

    You broke it yourself, you shouldn't override the showForm method... SPring will handle the things for you, check the javadocs of the Controller hierarchy.
    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

    Default

    Ok,
    So I removed the override of formBackingObject() and showForm() and now I get the following exception:

    Code:
    java.lang.IllegalStateException: Neither Errors instance nor plain target
    object for bean name 'pageForm' available as request attribute

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    You can override formBackingObject but you cannot override showFOrm. Also make sure the view is rendered through the DispatcherServlet and a Controller and not directly.
    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

  5. #5

    Default

    Thanks. This resolved most of the issues.

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
  •