简体   繁体   English

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

[英]Accessing RedirectAttributes in method without RedirectAttributes in signature

Is it possible to access RedirectAttributes in method without RedirectAttributes in signature? 是否可以在签名中没有RedirectAttributes的方法中访问RedirectAttributes? For example, when overriding method like one below: 例如,当覆盖以下方法时:

@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);
}

I'm using spring 3.2.2.RELEASE. 我正在使用Spring 3.2.2.RELEASE。

As you can see in the DispatcherServlet class implementation, there are constants: 如您在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 has a class called RequestContextUtils, which has methods: Spring有一个名为RequestContextUtils的类,该类具有以下方法:

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

The first two methods will give you an access to input and output flash maps respectively. 前两种方法将使您分别访问输入和输出Flash映射。 The last method returns FlashMapManager, which has a number of convinient methods to work with flash attributes. 最后一个方法返回FlashMapManager,它具有许多方便的方法来处理Flash属性。 See implementations of this interface for details, specifically AbstractFlashMapManager. 有关详细信息,请参见此接口的实现,尤其是AbstractFlashMapManager。

If your goal is "to add indication that the customer has landed on the home page for the first time," then HttpSession may do the trick: 如果您的目标是“添加表明客户已首次进入主页的指示”,则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>;
}

This works for me. 这对我有用。 The logic behind this is that the login is in the context of the entire session that, presumably, comprises multiple requests. 其背后的逻辑是登录是在整个会话(可能包括多个请求)的上下文中。

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

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