Accessing the current request uri

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing the current request uri

dostermeier
Hi,

I'm writing an AttrProcessor and would like access to the current requestUri.  At the moment I'm doing this via


        RequestContext requestContext = (RequestContext) arguments.getContext().getVariables().get("springRequestContext");
        if (requestContext.getRequestUri().startsWith(prefix)) {

Is there a way to do this that does not tie me to the spring context?

What I am doing is creating this processor so that I can apply a class to a menu element based on the current uri, ie: to highlight the currently selected menu item.

Cheers,
-Daniel
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the current request uri

Zemi
Administrator
> Is there a way to do this that does not tie me to the spring context?

I think it is not possible right now.

The org.thymeleaf.context.WebContext object contains some information of the HttpServletRequest object, implementing the ContextObject design pattern, but it does not provice access to the Request.

Maybe Daniel could include additional information to the IWebContext interface in future releases, like requestUri, serverName, etc.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the current request uri

danielfernandez
Administrator
In reply to this post by dostermeier
Hi,

I'm afraid you are right in that currently there is no way to access the HttpServletRequest object directly from WebContext.

I can study this, and think about a way to make this access possible. If you wish, you can add a Feature Request to the issue system so that you can track the progress of this feature...

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the current request uri

Zemi
Administrator
> If you wish, you can add a Feature Request to the issue system so that you can track the progress of this feature...

I have created a feature request for this
    https://sourceforge.net/tracker/?func=detail&aid=3414848&group_id=509826&atid=2072662

though I know it is a bit against the idea of make Context interface independent of the underlying technology.

Reply | Threaded
Open this post in threaded view
|

Re: Accessing the current request uri

emilime
It would be useful to have an utility to access the current request path. Something like #selfURL
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the current request uri

dostermeier
Thanks for creating that feature request zemi.  

Just to provide some added context:

I'm trying to apply a css class to a menu item based on the current page URL.  So all pages under '/jobs/' should result in my jobs menu item being active.

            <ul class="nav">
                <li info:activeforprefix="'/jobs/'">Jobs</li>
                <li info:activeforprefix="'/grid/'">Grid</li>
                <li info:activeforprefix="'/admin/'">
                    Administration</li>
            </ul>

The implementation of the info:activeforprefix is:

        final String prefix = (String) StandardExpressionProcessor.processExpression(
                arguments, templateResolution, attributeValue
        );

        final Map<String, String> values = new HashMap<String, String>();

        RequestContext requestContext = (RequestContext) arguments.getContext().getVariables().get("springRequestContext");
        if (requestContext.getRequestUri().startsWith(prefix)) {
            values.put("class", "active");
        }

By the way, I'm loving thymeleaf.  It is refreshing to work with HTML files that actually look like HTML files :)

Cheers,
-Daniel
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the current request uri

FingolfinTEK
In reply to this post by dostermeier
I also had the need to access the current request object and I solved it by using a utility class for extracting the request object from request context; it may not be pretty but it works and at least it didn't include using reflection to override the method visibility :)

package org.springframework.web.servlet.support;

import javax.servlet.http.HttpServletRequest;

public final class RequestUtil {

    private RequestUtil() {
        // singleton
    }

    public static HttpServletRequest getRequestFromContext(final RequestContext p_context) {
        return p_context.getRequest();
    }
}

Then, you use it like this:

private void someMethod(final WebContext p_pageContext) {
        final RequestContext requestContext = (RequestContext) p_pageContext.getVariables().get(SpringContextVariableNames.SPRING_REQUEST_CONTEXT);
        final HttpServletRequest request = RequestUtil.getRequestFromContext(requestContext);
        //do something with the request object
}
Fingolfin like a shooting light
beneath a cloud, a stab of white,
sprang then aside, and Ringil drew
like ice that gleameth cold and blue,
his sword devised of elvish skill
to pierce the flesh with deadly chill.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the current request uri

danielfernandez
Administrator
In reply to this post by dostermeier
I've just uploaded a new 1.1.2-SNAPSHOT snapshot release that makes two new objects available for expression evaluation:

#httpServletRequest
#httpSession

These objects represent, as their name implies, the HttpServletRequest and HttpSession objects related to the WebContext object being used for processing the template. Note that these objects are only available in web environments.

See http://www.thymeleaf.org/faq.html#use-snapshot in order to learn how to use a snapshot release.

This feature will go into 1.1.2.

Regards,
Daniel.
PeZ
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the current request uri

PeZ
I have a question regarding #httpServletRequest usage with the test framework introduced with Spring MVC 3.2.

As explained here :http://blog.springsource.org/2012/11/12/spring-framework-3-2-rc1-spring-mvc-test-framework/, it's possible to execute tests without the need for a servlet container.
However, that means #httpServletRequest doesn't exist in that configuration.
Some of my templates use #httpServletRequest (in link tags) , thus generating an error :

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Base for link URL creation must be a non-null and non-empty String (currently: java.lang.String) (base:59)

Anyone know how I could 'bypass' this issue?