Announcement Announcement Module
Collapse
No announcement yet.
Understanding FormController Flow Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          Thanks. This resolved most of the issues.

          Comment

          Working...
          X