Announcement Announcement Module
Collapse
No announcement yet.
How to access Request parameters in Freemarker? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to access Request parameters in Freemarker?

    How to access Request parameters in Freemarker?

  • #2
    From Spring 2.0.1 changelog

    * FreeMarkerView exposes all standard FreeMarker hash models now (including session and request parameters)
    Anybody know how to get request parameters from freemarker template?

    Comment


    • #3
      The HttpRequestHashModel object is put into the model under the key "Request". You can then access any properties on this object just as you would with objects/attributes you put into the model.

      Comment


      • #4
        Originally posted by cwash5 View Post
        The HttpRequestHashModel object is put into the model under the key "Request". You can then access any properties on this object just as you would with objects/attributes you put into the model.
        But it is Request Attributes, not parameters?

        Comment


        • #5
          The HttpRequestHashModel actually contains the HttpServletRequest object so you can get anything from the request by navigating this object. ${Request.getRequest().getAttribute('yourattribute ')}

          Hope that makes sense....

          Comment


          • #6
            Originally posted by cwash5 View Post
            The HttpRequestHashModel actually contains the HttpServletRequest object so you can get anything from the request by navigating this object. ${Request.getRequest().getAttribute('yourattribute ')}

            Hope that makes sense....
            Not working for me.

            freemarker.core.InvalidReferenceException: Expression Request.getRequest is undefined on line 9, column 15 in ...

            Comment


            • #7
              Sorry been awhile since I've used freemarker. Try removing the "get" on the method calls.

              Code:
              ${Request.request.attribute['yourattribute']}
              If that doesn't work refer the the FreeMarker doc on how to navigate object graphs.

              Comment


              • #8
                Originally posted by cwash5 View Post
                Sorry been awhile since I've used freemarker. Try removing the "get" on the method calls.

                Code:
                ${Request.request.attribute['yourattribute']}
                If that doesn't work refer the the FreeMarker doc on how to navigate object graphs.
                Not working

                Expression Request.request is undefined on line 9, column 12 in ...

                Request.myattribute work, but I need Request.getParameter("my param").
                Last edited by lexius; Dec 22nd, 2006, 03:48 PM.

                Comment


                • #9
                  Ok... Enough guessing. Built a test that shows how this works.

                  To get access to Request Parameters use one of the following syntaxes where test is the request property
                  Code:
                  ${RequestParameters['test']}
                  or 
                  ${RequestParameters.test}
                  to get access to Request Attributes use one of the following where test is the request attribute

                  Code:
                  ${Request['test']}
                  or 
                  ${Request.test}
                  Hopefully that will work for you.

                  Comment


                  • #10
                    ${RequestParameters['test']}
                    or
                    ${RequestParameters.test}
                    It Works! Thank you very much!

                    Comment


                    • #11
                      Originally posted by lexius View Post
                      It Works! Thank you very much!
                      This works
                      Code:
                      ${RequestParameters.test}
                      but
                      Code:
                      ${Request.contextPath}
                      doesn't work. I want to retrieve a HttpServletRequest object, but how ?

                      Comment


                      • #12
                        Originally posted by yientau View Post
                        This works
                        Code:
                        ${RequestParameters.test}
                        but
                        Code:
                        ${Request.contextPath}
                        doesn't work. I want to retrieve a HttpServletRequest object, but how ?
                        Ok, probably because of sitemesh decorators that cause ${Request} not working. I have tried putting it outside the scope(in a macro)

                        it throws me the following errors
                        Code:
                        Error on line 37, column 11 in decorators/test.ftl
                        Expecting a string, date or number here, Expression Request is instead a freemarker.ext.servlet.HttpRequestHashModel
                        The problematic instruction:
                        ----------
                        ==> ${Request} [on line 37, column 9 in decorators/test.ftl]
                         in user-directive paginate [on line 27, column 9 in decorators/test.ftl]
                        ----------
                        Then I tried ${Request.size}, it throws me the following errors
                        Code:
                        Expression Request.size is undefined on line 37, column 11 in decorators/test.ftl.
                        The problematic instruction:
                        ----------
                        ==> ${Request.size} [on line 37, column 9 in decorators/test.ftl]
                         in user-directive paginate [on line 27, column 9 in decorators/test.ftl]
                        ----------
                        Any clues ?

                        Comment


                        • #13
                          accessing Request inside Freemarker

                          This is FYI. It took me a while to figure out the details and I hope
                          others don't need to waste the time.

                          Spring automatically registers "Request". However, the object registered
                          is FreeMarker's HttpRequestHashModel, which means
                          one can only access the request attributes but not other
                          request methods. If you really need to access those
                          request information, you need to add the request object
                          to the model yourself.

                          Comment

                          Working...
                          X