Dan*_*ega 5 java spring spring-mvc spring-boot
我正在尝试创建一个拦截器,使当前控制器名称和操作(方法)名称可用于我的视图。这似乎在大多数时候都很有效。
public class BaseInterceptor extends HandlerInterceptorAdapter {
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
String controllerName = "";
String actionName = "";
if( handler instanceof HandlerMethod ) {
// there are cases where this handler isn't an instance of HandlerMethod, so the cast fails.
HandlerMethod handlerMethod = (HandlerMethod) handler;
controllerName = handlerMethod.getBean().getClass().getSimpleName().replace("Controller", "");
actionName = handlerMethod.getMethod().getName();
}
modelAndView.addObject("controllerName", controllerName );
modelAndView.addObject("actionName", actionName );
}
}
Run Code Online (Sandbox Code Playgroud)
唯一不起作用的是当我使用 Spring Security 登录时。发生这种情况时,我会得到一个代理包装器作为控制器名称。有什么办法可以获取实际的控制器名称吗?
帖子$$EnhancerBySpringCGLIB$$3ef51261
如果您是否使用 spring security,这适用于获取控制器名称。
controllerName = handlerMethod.getBeanType().getSimpleName().replace("Controller", "");
不确定,但也许您可以从 RequestMappingHandlerMapping 获取信息。只需在拦截器中自动装配 RequestMappingHandlerMapping 并调用 getHandler 方法
归档时间: |
|
查看次数: |
5969 次 |
最近记录: |