Announcement Announcement Module
Collapse
No announcement yet.
Binding @RequestHeader to model object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding @RequestHeader to model object

    Hi,

    I want to be able to bind a @RequestHeader (or @CookieValue) to a model object. This seems to not work (but it does work for @RequestParam). Am I missing something, or is this not supported?

    If it's not supported, is this a feature that can be added?

  • #2
    Originally posted by kludwig View Post
    Hi,

    I want to be able to bind a @RequestHeader (or @CookieValue) to a model object. This seems to not work (but it does work for @RequestParam).
    According to Spring MVC documentation it works fine: http://static.springsource.org/sprin.../html/mvc.html, 15.3.2.11 Mapping request header attributes with the @RequestHeader annotation

    But only in @Controllers.

    See example there.

    Please checkout also the source code of: org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker to see how it works.

    Comment


    • #3
      Thanks, any ideas on why this is so? This is specifically what I wonder about:

      Code:
      class MyModel {
          String s;
          String ua;
      
          // this works:
          public void setS(@RequestParam String s) { this.s = s;}
      
          // this doesn't:
          public void setUa(@RequestHeader("User-Agent") String ua) {
              this.ua = ua;
          }
      }
      
      // some other file:
      @Controller 
      public class MyController {
          @RequestMapping(...)
          void handle(MyModel m) {
              // m.s is mapped, m.ua is not. Kind of inconsistent and lame.
          }
      }

      Comment


      • #4
        Originally posted by kludwig View Post

        Code:
        class MyModel {
            String s;
            String ua;
        
            // this works:
            public void setS(@RequestParam String s) { this.s = s;}
        
            // this doesn't:
            public void setUa(@RequestHeader("User-Agent") String ua) {
                this.ua = ua;
            }
        }
        
        // some other file:
        @Controller 
        public class MyController {
            @RequestMapping(...)
            void handle(MyModel m) {
                // m.s is mapped, m.ua is not. Kind of inconsistent and lame.
            }
        }
        I think this is because you expect MyModel object in your controller method, and one of the request parameter is "a", so it will be bound to the "MyModel.a" field.

        Try removing @RequestParam and you will see, that this works too, I believe.

        So once you have following:

        Code:
        @RequestMapping("mapping")
        public void doSomething(MyModel myModel) {}
        And lets assume you call the method with following url:

        mapping?s=somevalue&ua=someothervalue, you get myModel with both "s" and "u"a set., "@RequestParam String s" does nothing in that case.

        Try this with your model, you will get it.

        Comment

        Working...
        X