如何在Spring MVC的jsp中获取请求url?

时间:2014-11-20 08:39:56

标签: spring-mvc

我在Spring Mvc中将页面/folder/hello.jsp映射到控制器/hello

现在我想通过jstl /hello在我的jsp中获取控制器映射的网址${pageContext.request.requestURL},但我得到/folder/hello.jsp

我在控制器中尝试了HttpServletRequest.getRequestURL()并获得了我想要获得的当前网址。但我不想更改我的控制器。

1 个答案:

答案 0 :(得分:1)

据我所知,没有Spring结构支持这一点。在我看来,最好编写一个拦截器并将映射添加到模型属性中。像

这样简单的东西
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 {
    }

}

和配置

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