Announcement Announcement Module
No announcement yet.
Pre and Post form controllers Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pre and Post form controllers

    If im understanding Spring correctly all requests to do something go through the DispatcherServlet and are handled by a controller before a response comes back (as a jsp in my case) .

    So say I want to display a list of objects in a page I need to write a controller and then a page to view the data. Now if that page has an option to change something on that page (such as server side sorting) which doesnt require display of further pages just an update of some sort to the current page should that be handled by the same controller or a different controller.

    Similarly if the page has another option to do something which results in another page such as 'edit object' should that be handled by this controller or another controller and if it should be handled by another controller would that be the same one that dealt with validation of the changes the user makes to the form when they actually edit it.

    Should the same controller be used for:
    displaying a page the first time
    redisplaying the current page
    tasks on that page
    leaving the page.

    In essence, what I am asking is:
    1. Are Controllers related to Pages or Actions
    2. Is PreDisplay and PostDisplay handled by the same controller

  • #2
    Some people call what you are doing master/detail screens. What I have been doing is creating two controllers, on for list and one for edit. For example, say you want to have a list of users and then be able to edit the users. Create a UsersController that extends AbstractController. In the handleRequestInternal method, get the list of users and store it in the model for the view. A good url mapping for this would be / Subsequent requests to the same url could include addtional parameters to sort, filter or page the list. Your code in handleRequestInternal should handle that. The first page might be /, the second page / The first request would use default values for index and pageSize, and the second request would use the values in the request parameters. You could also have parameters like sortProperty, sortOrder(asc|desc), etc.

    The the second controller should be UserController and you want to extend SimpleFormController. The URL Mapping would be / You need to override two methods. First, override formBackingObject like this:

    protected Object formBackingObject(HttpServletRequest request) throws Exception {
            Long id = HTTPUtil.getLongParameter(request,"id");
            User user = null;
            if(id != null && id.longValue() > 0) {
                return userService.get(id);
            } else {
                return new User();
    This assumes that you have a userService with a get method that takes the primary key and gets the object from the database. If there is no id in the request, it is assumed you are doing an add, otherwise it is an edit. Then you can override onSubmit, where you can put your logic to create or update the record in the database. When you make an HTTP GET request to /, formBackingObject is called, but onSubmit is not. When you do an HTTP POST (which happens when the HTML form is submitted), that is when onSubmit gets called.


    • #3
      ThanKs Paul, I think I understand you, so from waht you are saying the answers to my quesions are

      1. Are Controllers related to Pages or Actions
      Controllers generally relate to pages, there is one controller for ListUsers and one controller for Add/Edit User

      2. Is PreDisplay and PostDisplay handled by the same controller
      Yes, the controller should be able to handle whether this is the first time the page will be displayed or whether it is another request which will result in the same page being displayed , but possibly in a modified form (such as sorted)

      Just to clarify is is the .do bit a defacto standard. I take it it doesnt do anything it is just the mapping in the urlmapping.


      • #4
        Sounds like you got it


        • #5
          hi paul ,

          i have a form in the listing page to facilitate filtering
          and paging

          have four buttons first,previous,next ,last and also some text boxes
          to specify filtering criteria..

          i need to get all the values inside the form + which button was
          clicked inside handleRequestInternal method

          if i am using
          <input type="submit" name="first" value="First" class="formbutton"></td>

          <input type="submit" name="next" value="Next" class="formbutton"></td>

          it works

          but i need to use image there
          so i tried

          <INPUT type="image" src="images/icon_first.gif" name="first" value="first">

          <INPUT type="image" src="images/icon_next.gif" name="next" value="next">

          but i am not getting info about which button was clicked

          i need to use image any way..

          i am using

          if (request.getParameter("first") != null)
          inside handleRequestInternal method

          can u please help me with that ..
          or is there any better way of doing the same thing

          thanx in advance