navee
navee

Reputation: 551

How to get request url in jsp in SpringMVC?

I mapped page /folder/hello.jsp to controller /hello in Spring Mvc.

Now I want to get the controller mapped url /hello in my jsp by jstl ${pageContext.request.requestURL},but i get /folder/hello.jsp.

I tried HttpServletRequest.getRequestURL() in the controller and got the currect url that's I want to get.But I don't want to change my controller.

Upvotes: 0

Views: 719

Answers (1)

Master Slave
Master Slave

Reputation: 28519

As far as I know there is no Spring construct that will support this. In my view, its best to write an interceptor and add the mapping to a model attribute. Something as simple as

public class MapToModelInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
         modelAndView.addObject("mapping", request.getRequestURI());
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }

}

and the config

<interceptors>
    <interceptor>
        <mapping path="/**" />
        <beans:bean class="org.example.interceptors.MapToModelInterceptor"></beans:bean>
    </interceptor>
</interceptors>

Upvotes: 1

Related Questions