Question

I need to retrieve parameter from url. It's a simple case when I do this on WebPage level - it could be done by sth like this:

    @MountPath(INDEX_PREFIX + "/${" + OBJECT_ID_KEY + "}")
    public class IndexPage extends WebPage
    {
            public IndexPage(PageParameters parameters)
            {
                    String id = parameters.get(OBJECT_ID_KEY).toString();

                    // etc...
            }
    }

But I need it to be done on session level - in getRoles() method.

I've done this in that way:

    @Override
    public Roles getRoles()
    {
            final Request request = RequestCycle.get().getRequest();
            List<String> segments = request.getUrl().getSegments();
            if (segments != null && segments.size() > 1 && INDEX_PREFIX.equals(segments.get(0)))
            {
                    String id = segments.get(1);
            }

            // etc...
    }

Url structure looks like this: www.my.page.com/index/5

index - INDEX_PREFIX

5 - OBJECT_ID_KEY

Is there a better way???

Was it helpful?

Solution

You already have the Request in your hand - now just follow the API :-)

Request req = RequestCycle.get().getRequest();
IRequestParameters params = req.getRequestParameters();
StringValue idValue = params.getParameterValue("id");

And there you go.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top