spring short-code-sheet
2019-02-23 本文已影响0人
泠泉
- 获取
Request
HttpServletRequest request =
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
- 获取
WebApplicationContext
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
GroovyService groovyService = (GroovyService)webApplicationContext.getBean("GroovyService");
- 访问引发认证跳转到当前代码块,获取引发跳转的URL
RequestCache requestCache = new HttpSessionRequestCache();
SavedRequest savedRequest = requestCache.getRequest(request, response);
String targetUrl = savedRequest.getRedirectUrl();
- 跳转到指定的url
RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
redirectStrategy.sendRedirect(request, response, "登陆页的url");
- 获取PathVariable
NativeWebRequest webRequest = new ServletWebRequest(request);
Map<String, String> pathVariableMap =
(Map<String, String>) webRequest.getAttribute(
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE,
RequestAttributes.SCOPE_REQUEST);
String id = pathVariableMap .get("id"); //get id pathvariable form request
- 获取调用此方法的上面的方法名、类
StackTraceElement[] stacks = (new Throwable()).getStackTrace();
for (StackTraceElement stack : stacks) {
System.out.println(stack.getClassName() + "-" + stack.getMethodName());
}