繁体   English   中英

在签名中没有RedirectAttributes的方法中访问RedirectAttributes

[英]Accessing RedirectAttributes in method without RedirectAttributes in signature

是否可以在签名中没有RedirectAttributes的方法中访问RedirectAttributes? 例如,当覆盖以下方法时:

@Override
public void onAuthenticationSuccess(final HttpServletRequest req, final HttpServletResponse res,
        final Authentication auth) throws IOException, ServletException {

    // add something to RedirectAttributes here
    // redirectAttributes.addFlashAttribute("attr", "value");

    super.onAuthenticationSuccess(req, res, auth);
}

我正在使用Spring 3.2.2.RELEASE。

如您在DispatcherServlet类实现中所看到的,有常量:

public static final String FLASH_MAP_MANAGER_BEAN_NAME = "flashMapManager";
public static final String OUTPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".OUTPUT_FLASH_MAP";
public static final String FLASH_MAP_MANAGER_ATTRIBUTE = DispatcherServlet.class.getName() + ".FLASH_MAP_MANAGER";

Spring有一个名为RequestContextUtils的类,该类具有以下方法:

public static Map<String, ?> getInputFlashMap(HttpServletRequest request)
public static FlashMap getOutputFlashMap(HttpServletRequest request)    
public static FlashMapManager getFlashMapManager(HttpServletRequest request)

前两种方法将使您分别访问输入和输出Flash映射。 最后一个方法返回FlashMapManager,它具有许多方便的方法来处理Flash属性。 有关详细信息,请参见此接口的实现,尤其是AbstractFlashMapManager。

如果您的目标是“添加表明客户已首次进入主页的指示”,则HttpSession可以解决这个问题:

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
      throws IOException
{
    ...
    boolean firstLogin = <determine whether this is the first login>;
    <reset condition that caused firstLogin to be true>;
    ...
    HttpSession session = request.getSession(false);
    if (session != null) {
        session.setAttribute("firstLogin", firstLogin);
    }
    else {
        log.debug("No session");
    }
}

// Controller for some page
@RequestMapping(value = <your page>, method = RequestMethod.GET)
public String showPage(<some other args>, HttpSession session)
{
    ...
    Object firstLogin = session.getAttribute("firstLogin");
    Assert.isInstanceOf(Boolean.class, firstLogin, "firstLogin");
    if ((Boolean)firstLogin) {
        <handle first login>;
    }
    ...
    return <page>;
}

这对我有用。 其背后的逻辑是登录是在整个会话(可能包括多个请求)的上下文中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM