Accessing flash attributes in Spring Web flow

2019-04-18 02:34发布

问题:

I use,

   redirectAttributes.addFlashAttribute("msg","Level complete")

to access message on the redirected jsp.

How can I use this redirect attribute when I am redirecting to a Webflow ?

回答1:

When flash attribute is used to send data from one controller to a webflow we have to bind redirected flash attribute (from controller) to the response JSP page of the webflow. For this purpose we can maintain a backend FormAction class to bind the value to any scope of webflow. In flow xml we can can call custom method on entry of a view state.

Custom Method of FormAction class would be like

public void setupReferenceData(RequestContext context) throws Exception {
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getNativeRequest();
        Map<String, ?> inputFlashMap = RequestContextUtils.getInputFlashMap(request);
        if (inputFlashMap != null) {
            String flash = (String) inputFlashMap.get("flash");
            context.getRequestScope().put("flash1", flash);
        }
    }

This method call should be included in the entry section of a view state. So flow xml should have these portion.

<view-state id="request" view="hello">
            <on-entry>
                <evaluate expression="requestAction.setupReferenceData" />
            </on-entry>
            <transition on="next" to="helloend"/>
    </view-state>