Announcement Announcement Module
Collapse
No announcement yet.
Switch from a basic Controller to SimpleFormController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Switch from a basic Controller to SimpleFormController

    I currently have this code (see below) but I would like to switch this to extend SimpleFormController since I also need to use the view/form for saving the data on the screen. How do I get the same amount data (i.e. Hashmap) via a formBackingObject and access it using spring:bind and c:foreach?

    Code:
    public class PortfoliosController implements Controller
    {
        public ModelAndView handleRequest(HttpServletRequest request,
                HttpServletResponse response)
        {
            Map model = new HashMap();
            model.put("portfolios", portfolioManager.getPortfolios());
            model.put("programs", programReferenceManager.getPrograms());

  • #2
    If you are not editing this data, if it is just used for displaying the form, then you should override the referenceData() method instead and have that return the Map containing you data.

    If you are editing this data, then create a JavaBean backing object to hold the data and bind the user input to.

    Rob

    Comment


    • #3
      Thanks, I believe the JavaBean as formBackingObject approach is what I need to take.

      Tthe user will be editing the data (not text but all <select> drop-downs to manage a 3-column link table, i.e. mapping of 3 Keys to 3 data tables). The screen will look like this -- each bracketed field here is a drop-down; once the user picks their mapping, they click "SAVE ALL" and I need to save each row to the 3-column link table:

      Code:
        1     &#91;LAPTOP1&#93;     &#91;PERSON1&#93;     &#91;OFFICE1&#93;
        2     &#91;LAPTOP2&#93;     &#91;PERSON2&#93;     &#91;OFFICE2&#93;
        .
        .
        n     &#91;LAPTOPn&#93;     &#91;PERSONn&#93;     &#91;OFFICEn&#93;
      
                          <SAVE ALL>

      Comment


      • #4
        Sorry, one more question.

        Since my screens will have multiple rows (as many as there are in the database + 1 blank for adding a new mapping), can use indexed properties to bind to a List object in the JavaBean approach you recommended.

        For example:
        Code:
        setPortList&#40;List l&#41;
        List getPortList&#40;&#41;
        Then bind in the JSP/view as:
        Code:
        spring&#58;bind path="portfolios.portfoliolist&#91;$&#123;loopIndex&#125;&#93;"

        Comment


        • #5
          You *can* do this but the objects need to exist in the List already before binding to them - Spring cannot create the objects - it doesn't know what type they should be.

          Rob

          Comment


          • #6
            I understand. Thanks a bunch; you have been very helpful.

            Comment


            • #7
              I'm probably doing something silly here but I can't any rows back in the c:foreach even though the formBackingObject shows 2 rows being returned -- do I have to bind this in order to see the data? Here are the code excerpts:

              MY CONTROLLER CLASS
              Code:
                  protected Object formBackingObject&#40;HttpServletRequest request&#41; throws Exception 
                  &#123;
                      log.info&#40;"formBackingObject - List.size&#40;&#41; = "
                              + defaultPortfolioCommand.getDefaultPortfolioManager&#40;&#41;.getDefaultPortfolios&#40;&#41;.size&#40;&#41;&#41;;
                      
                      return defaultPortfolioCommand;
                  &#125;

              MY JSP/VIEW
              Code:
              <c&#58;forEach items="$&#123;defaultPortfolioCommand.defaultPortfolioManager.defaultPortfolios&#125;" var="defaultportfolios" varStatus="loopStatus">

              NOTE: defaultPortfolios is a java.util.List.

              Comment


              • #8
                Have you overriden the command name in your configuration? By default the command object is just called 'command' and here you are referring to 'defaultPortfolioCommand'.

                Rob

                Comment


                • #9
                  Thanks, that was it :-)

                  You guys are good!

                  By the way, since this is an editable form but there could be an unknown number of rows returned from the database, do I bind using something like:
                  Code:
                  <spring&#58;bind path="defaultPortfolioCommand.defaultPortfolioManager.defaultPortfolios&#91;$&#123;loopStatus.index&#125;&#93;.portId">
                  [/code]

                  Comment


                  • #10
                    Originally posted by robh
                    If you are not editing this data, if it is just used for displaying the form, then you should override the referenceData() method instead and have that return the Map containing you data.

                    If you are editing this data, then create a JavaBean backing object to hold the data and bind the user input to.

                    Rob
                    Hi, Rob. What if I'm not "editing" the data as in INPUT TYPE="text" fields but rather having SELECT drop-down (OPTIONs)? Here is a screenshot:
                    http://us.f2.yahoofs.com/bc/42eebd1e...VGs7CBocKX7V0Q

                    On each row, the user has 3 drop-downs (as shown above), essentially they map the link between 3 tables and I have to save this mapping to a 3-column (foreign keys) table.

                    For example, how do I get the "selectedIndex" of the options the user selected using Spring MVC/bind? Should I use referenceData + custom processing in onSubmit instead of doing both via in formBackingObject?

                    Any help would be appreciated. Thanks in advance.

                    Comment


                    • #11
                      In this case you need to a mixture of referenceData() and formBackingObject(). You use referenceData() to load the list of data and then formBackingObject() to load the actual selected item.

                      Rob

                      Comment


                      • #12
                        Thanks, that worked!

                        Now, I have a new problem. The first few rows (2 in the above screenshot) are pre-populated with existing database data, the last one remains blank, so the user can pick a new row to "add" by choosing the new mappings.

                        However, my onSubmit is not being called when I only "update" the first 2 rows. It only gets called when I select something in the last (blank) row.

                        How can I force onSubmit to be called each time?

                        Comment


                        • #13
                          Never mind, I just did this as a workaround -- not sure if this is a clean way.?
                          Code:
                          	protected ModelAndView processFormSubmission&#40;
                                      HttpServletRequest request,
                                      HttpServletResponse response,
                                      Object command,
                                      BindException errors&#41;
                          			throws ServletException, IOException
                          	&#123;
                          	    return onSubmit&#40;request, response, command, errors&#41;;
                          	&#125;

                          Comment


                          • #14
                            I can't think of a reason why the onSubmit() method is not being called. Can you step through the code using the debugger and see what happens when you submit?

                            Rob

                            Comment


                            • #15
                              I'm glad to see your response because I thought it was because I'm a newbie.

                              It was very strange but I found some article on the web that used onFormSubmission to call onSubmit, so I tried it as a long shot and it worked right away!

                              I even tried setting the "sessionForm" to true and false but it didn't help.

                              Comment

                              Working...
                              X