美文网首页
spring short-code-sheet

spring short-code-sheet

作者: 泠泉 | 来源:发表于2019-02-23 15:59 被阅读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());
    }
    

    相关文章

      网友评论

          本文标题:spring short-code-sheet

          本文链接:https://www.haomeiwen.com/subject/ubjpyqtx.html