Announcement Announcement Module
Collapse
No announcement yet.
How to access LocalWebScriptContext from LocalWebScriptRequest Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to access LocalWebScriptContext from LocalWebScriptRequest

    I'm trying to retrieve request parameters that are passed in via the URL when my DeclarativeWebScript is executed and after looking at the source code and running my code through a debugger, it doesn't appear to be supported or implemented.

    The LocalWebScriptRequest object has a private member containing a LocalWebScriptContext and on that context there's a HashMap called 'tokens'. My request parameter is in this HashMap (spring/surf has done this auto-magically). The problem is that there is no getter for this LocalWebScriptContext or a way to get the tokens out of the HashMap.

    Is this a bug or not supported?

  • #2
    getContext()

    Agreed. The class currently is defined as follows ( both M3 and trunk):

    Code:
    public class LocalWebScriptRequest extends WebScriptRequestURLImpl
    {
        ....
        final private LocalWebScriptContext context;
        .....
    }

    It would be of great value if an accessor for "context" existed:

    Code:
    public class LocalWebScriptRequest extends WebScriptRequestURLImpl
    {
        ....
        final private LocalWebScriptContext context;
        .....
    
        public LocalWebScriptContext getContext() {
          return context;
        }
    }
    Regards,

    Bob

    Comment


    • #3
      I assume you want to get the to the page URL params within Java code and you are overriding one of the executeImpl() methods in a sub-class?

      You probably shouldn't cast the webscript request to LocalWebScriptRequest as that assumes a lot...

      You have a couple of options though, the "clean" way is to "bind" in the url params into the component webscript url, then the params will be automatically made available to the "args" map in the model for your webscript. So in your webscript descriptor, you do something like this:

      Code:
      <webscript>
        <shortname>Search Results List</shortname>
        <description>Search: Results List Component</description>
        <url>/components/search/search?myparam={foo}</url>
      </webscript>
      Note the {foo} token - this indicates to the Surf WebScript runtime that it should look for a page URL param called "foo" and if found, template the value into the URL for the webscript component. This means the component does not have to "know" it is running as a Surf local component and can use "args" map just as it would if it was called directly via a URL. This also means you can access those URL params in JavaScript via the "args" map, without having to walk the page.url.args structure.

      If you *don't* know the name of the param and so can't bind it onto the WebScript component URL or you don't want to do it this way, then you can access the current RequestContext via the ThreadLocal accessor:

      Code:
      RequestContext context = ThreadLocalRequestContext.getRequestContext();
      You can then call:
      Code:
      context.getParameter(key);
      or
      Code:
      context.getParameters();
      to retrieve any page URL params.

      Hope this helps,

      Kev

      Comment


      • #4
        Thanks Kev, that worked!

        Comment

        Working...
        X