---
title: "Spring Special Code"
author: "zenius"
date: "2009-03-03T09:04:39Z"
tags: []
slug: "spring-special-code"
draft: false
lastmod: "2009-03-16T23:07:35Z"
id: 341974
url: "https://zenius.iteye.com/blog/341974"
---

**Set flow and menu item mapping**

```java

 
    public void setLocationMaps(MenuComponent[] menuComponents) {

        for (MenuComponent menuComponent: menuComponents) {
           if (null == this.locationMapping.get(menuComponent.getLocation()) 
                   || null != menuComponent.getParent()) {
               this.locationMapping.put(menuComponent.getLocation(), menuComponent);
           }
           if (menuComponent.getMenuComponents().length > 0) {
               this.setLocationMaps(menuComponent.getMenuComponents());
           }
        }
    }


```



**Once Request filter**
```java

public class MenuFilter extends OncePerRequestFilter {

    /**
     * This method looks for a "locale" request parameter. If it finds one, it sets it as the preferred locale
     * and also configures it to work with JSTL.
     * 
     * @param request the current request
     * @param response the current response
     * @param chain the chain
     * @throws IOException when something goes wrong
     * @throws ServletException when a communication failure happens
     */
    @SuppressWarnings("unchecked")
    public void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                 FilterChain chain)
            throws IOException, ServletException {


        chain.doFilter(request, response);

        // Reset thread-bound LocaleContext.
        LocaleContextHolder.setLocaleContext(null);
    }
}


```
**Flow Execution listener and external context**

```java

public class MenuFlowLister extends FlowExecutionListenerAdapter {

    public void requestProcessed(RequestContext context) {
        String id = context.getActiveFlow().getId();

        MenuRepository menus = (MenuRepository) context.getExternalContext().getApplicationMap()
                .get(MenuRepository.MENU_REPOSITORY_KEY);
    }
}

```


**Get session attributes**

```java

        Object mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
        RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
        String test;
        synchronized (mutex) {
            test = (String)attributes.getAttribute("zenius", RequestAttributes.SCOPE_SESSION);
        }
        return test;

```