Spring MVC equivalent to ASP.NET RouteData

yonisha

I'm trying to extract a request routing data, similar to RouteData object in ASP.NET, to get the controller and action of the request. I'm trying to do it inside a filter, but specific Spring implementation will be sufficient.

Is there any way to get that?

Thanks.

cypressx

This cannot be done from inside a filter. You have to do it with a HandlerInterceptor implementation. In the preHandle method you can get the PathVariables like this:

Map pathVariables = (Map)request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);

Here's a short compiled sample:

Implement the HandlerMapping interface:

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class ParameterValidationHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

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

Register the interceptor in the application:

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new ParameterValidationHandlerInterceptor());
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Equivalent of RouteData.GetRequiredString in ASP.NET 5

How to access RouteData from an ASP.Net 5 Tag Helper in MVC 6

ASP.NET MVC 5 routing - create Html.ActionLink and parse ViewContext.RouteData.Values

Equivalent of "@section" in ASP.NET Core MVC?

Equivalent of Rake Routes in ASP.NET MVC

ASP.NET equivalent in ASP.NET MVC

ASP.NET Core equivalent for ASP.NET MVC BeginExecuteCore

ASP.NET MVC vs Spring MVC

Spring MVC vs ASP.NET (MVC?)

Asp.Net core get RouteData value from url

Changing RouteData in Asp.Net Core 3.1 in Middleware

What's the WebApi [FromUri] equivalent in ASP.NET MVC?

ASP.NET Core MVC equivalent of Start Action Specific Page

asp.net mvc core - equivalent of User.IsSignedIn()

MVC Exception Filter equivalent in Asp.net web forms

HttpRuntime.Cache Equivalent for asp.net 5, MVC 6

ASP.Net MVC TryUpdateModel() equivalent for using in a Windows Application

Spring boot Autowired annotation equivalent for .net core mvc

server.htmlencode of asp.net equivalent in asp.net mvc

ASP.NET Core equivalent of ASP.NET MVC 5's HttpException

How to access MVC RouteData in SignalR

In ASP.NET WebApi, why is RouteData's value type "object" and not "string"

ViewContext.RouteData.Values["Controller"] always Null - Asp.Net Core 2.1

What is the equivalent of System.Web.Mvc.Html.InputExtensions in ASP.NET 5?

ASP .NET 5 MVC 6 beta 7 upgrade : "Multiple assemblies with equivalent identity" issue

@Autowired equivalent in Spring.net

Asp .Net MVC Dropdown

Spring MVC equivalent of C# RoutePrefix

Spring MVC/WebAPI equivalent of C# RoutePrefix