Announcement Announcement Module
Collapse
No announcement yet.
How to put two forms on one page in spring? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to put two forms on one page in spring?

    Hi folks,
    i'm new in spring and have a problems putting two forms on one page, i want each form will be bind to some SimpleFormController

    any help will be appreciate


    thanks in advance

  • #2
    Originally posted by yanv
    i'm new in spring and have a problems putting two forms on one page
    Can you tell us more about the problem?

    Comment


    • #3
      There are a number of posts about this, try searching.

      For my two cents, I would probably approach this by having each form post to a seperate controller. Have both controllers have the same success page.

      Comment


      • #4
        Two forms on one page

        Originally posted by socotech
        Can you tell us more about the problem?
        hey guys, first of all thanks for reply.
        i'm trying to have two separate forms with two distinct controllers on one JSP page but already on GET i receive following error:
        javax.servlet.ServletException: Neither Errors instance nor plain target
        object for bean name 'userInfo' available as request attribute

        I found that i can solve it by having two pages for each controller and when using include tag in my main page, i didn't try it yet and still optimistic to find more elegant way.

        Comment


        • #5
          I dont think this issue is related to having two forms in one JSP. This just means you're calling
          Code:
          <spring:bind path="userInfo">...</spring:bind>
          with no object named userInfo in your page context. Implement the method formBackingObject in your controller to return the object that you want to acess via the spring:bind tag, and call setCommandName("userInfo") in your controller's contstructor.

          alternatively, if you are building the page using a model, ensure the userInfo object is in your model.

          cheers, toby

          Comment


          • #6
            But my page has two forms and hence formBackingObject has to return two objects, i guess that when i redirect to my aforementioned page using SimpleUrlHandlerMapping i actually activate only one controller which is return only one object to access.
            is this the cause of the problem?

            Thank you.

            Comment


            • #7
              You have two options:
              I would use an AbstractCommandController implementation to build the formView page model, placing the two objects you need as command objects into the model.
              Code:
              public ModelAndView handle(...) {
                return new ModelAndView("formViewPage.jsp")
                  .addObject("cmdObjectOne" userInfo)
                  .addObject("cmdObjectTwo" companyInfo);
              }
              The alternative is to use the referenceData method of one of your form controllers, but this would make one controller depend on the other being present in the same page.

              cheers, toby

              Comment


              • #8
                thanks,
                it may be a stupid question but which object should be the CommandClass?

                Comment


                • #9
                  whatever the class of your userInfo object is. if you do this:
                  Code:
                  MyUserInfo userInfo = new MyUserInfo();
                  Then do this in you constructor:
                  Code:
                  setCommandClass(MyUserInfo.class);
                  The command class is just whatever you want spring to bind the form fields to automatically. It can be any java bean you like.

                  Comment


                  • #10
                    Thank you.

                    Comment

                    Working...
                    X