Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Switch from a basic Controller to SimpleFormController

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    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
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

  3. #3

    Default

    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>

  4. #4

    Default

    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;"

  5. #5
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    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
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

  6. #6

    Default

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

  7. #7

    Default

    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.

  8. #8
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    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
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

  9. #9

    Default

    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]

  10. #10

    Default

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

Similar Threads

  1. View to forward to another controller?
    By justinp in forum Web
    Replies: 6
    Last Post: Apr 2nd, 2010, 01:53 PM
  2. Replies: 6
    Last Post: Jul 20th, 2007, 05:56 AM
  3. Replies: 5
    Last Post: Mar 28th, 2005, 08:29 AM
  4. Servlet & portlet packages (SimpleFormController)
    By mpetrashev in forum Architecture
    Replies: 0
    Last Post: Dec 2nd, 2004, 10:11 AM
  5. Replies: 1
    Last Post: Oct 14th, 2004, 01:48 PM

Posting Permissions

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