Announcement Announcement Module
Collapse
No announcement yet.
Does Spring MVC not handle binding request params to model? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does Spring MVC not handle binding request params to model?

    I'm familiar with Struts (1 & 2) and JSF. I'm familiar with Spring, but I'm only now looking at Spring MVC. I'm halfway through the big "Spring MVC" chapter in "Pro Spring 2.5" and it seems like I've missed something.

    I think there was perhaps one sentence in this chapter about how Spring MVC processes request parameters and sets model data from those request parameters. It appeared to be done completely manually, as opposed to some sort of automatic binding, as is done in Struts and JSF. Have I missed something, or is this book missing some important information, or does Spring MVC simply not work like this?

  • #2
    Spring does it automatically so not sure where you get it from that it is done manually....

    Comment


    • #3
      Request parameters are directly accesible in views, using the standard JSTL notation ${myparameter}

      There is one exception. Scope proxied beans can't be directly accessed in pages. The problem is that if bean name is "mybean", they are internally named scopedTarget.mybean, and can't be accessed.

      Code:
      <bean id="mybean" class="com.MyBean" scope="session">
          <aop:scoped-proxy proxy-target-class="false"/>
      </bean>
      This does not happen when you use Spring + JSF. You may use any bean in any scope, directly in the page (request, session, or even custom scopes), using JSF EL

      Comment


      • #4
        Originally posted by Marten Deinum View Post
        Spring does it automatically so not sure where you get it from that it is done manually....

        Ok, can you refer to anything in the Spring documentation that talks about this and describes how it works? I've also looked there, and I can't find anything that even mentions this.

        Comment


        • #5
          Originally posted by Rober2D2 View Post
          Request parameters are directly accesible in views, using the standard JSTL notation ${myparameter}
          I'm not talking about accessing request parameters in views, I'm talking about processing of form posts, injecting request parameter values into model variables, so business logic can execute actions based on those posts.

          Comment


          • #6
            I can't help but laugh a little bit because the functionality you're describing is, in my opinion, one of the key differentiators belonging to Spring.

            With that said, while I'm not personally familiar with the Pro Spring 2.5 literature I'm hoping that it at least makes mention of Data Binding and Property Editor functionality within the context of Controllers. The book that introduced me to Spring MVC back in 2006 entitled Expert Spring MVC and Web Flow has a great little chapter entitled "The Controller Menagerie" which explains the workflow of a controller quite well.

            Comment


            • #7
              Originally posted by jglynn View Post
              I can't help but laugh a little bit because the functionality you're describing is, in my opinion, one of the key differentiators belonging to Spring.
              Just curious, but if you say it's a key differentiator, what are you comparing it with? The same functionality is available in Struts (either 1 or 2), although you may be referring to the ability to bind to POJOs, which didn't appear until Struts 2 (released almost 2 years ago).

              Originally posted by jglynn View Post
              With that said, while I'm not personally familiar with the Pro Spring 2.5 literature I'm hoping that it at least makes mention of Data Binding and Property Editor functionality within the context of Controllers. The book that introduced me to Spring MVC back in 2006 entitled Expert Spring MVC and Web Flow has a great little chapter entitled "The Controller Menagerie" which explains the workflow of a controller quite well.
              Reviewing the section titled "Command Controllers" I see it refers to the ability to bind parameters to command objects, but still leaves out any detail in that process.

              Reviewing the "Controller Menagerie" chapter in "Expert Spring MVC and Web Flow" (Books24x7 is very handy) I see where it describes how this works to the level of detail I was expecting. That resolves my concerns. I didn't look at that book before because it was 2 years old.

              Comment


              • #8
                Originally posted by dkarr View Post
                I'm not talking about accessing request parameters in views, I'm talking about processing of form posts, injecting request parameter values into model variables, so business logic can execute actions based on those posts.
                You may use SimpleFormController. It parses the request, and constructs an object from it. A few tips:

                Call setCommandClass and setCommandName in the controller constructor. commandName is the name of the html form. commandClass is the class you want to use as form (a POJO class)

                handleRequestInternal: This is a protected method, which is the first executed on every request. If you override, don't forget to call the super method, as it calls the methods I describe later. Object, is not available still

                formBackingObject: This method reads from the request and creates an object from it. Normally, it makes everything automatically, and there is no need to override

                processFormSubmission: This method executes only when controller receives a post request. It receives the object recovered by formbacking object. Just make a cast to the class you configured with setCommandClass. By default, it validates form, and calls onSubmit

                setFormView: Sets the default view of the controller

                setSuccessView: Default view to go if form info is successfully validated

                setValidator: Allows to inject a bean to validate the form

                onSubmit: Method executed if form is successfully validated. By default, it redirects to successview

                showForm: Method executed by default on GET request, or if validation fails. Used to prepare view.

                The API: http://static.springframework.org/sp...ontroller.html

                And reference: http://static.springframework.org/sp...plecontrollers

                It may seem a little complex at first, but think that there is no need to override all methods above.

                Comment


                • #9
                  Sorry, I linked to the class for portlets. The one for servlets is:

                  http://static.springframework.org/sp...ontroller.html

                  Comment

                  Working...
                  X