0

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.

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks,I soloved it by this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.