OpenSessionInViewFilter v.s. OpenSessionInViewInterceptor

July 31, 2008 at 7:24 am | Posted in Uncategorized | 2 Comments
Tags: ,

Spring provides two implementation to achieve open session in view pattern.

OpenSessionInViewFilter
OpenSessionInViewInterceptor

Both of them will keep hibernate session open until view is rendered. However, even though both provides same function, they are implemented on fundamentally different techniques.

Interceptor:

  • use Spring MVC function
  • small granularity to apply
  • configure in applicationContext.xml, [servlet_name]-servlet.xml, or spring config file.

* use DispatcherServlet (Spring MVC) interceptor function to keep opening hibernate session.

* If you only need to open hibernate session for specific controllers, interceptor is a best choice because it can be configured to each controller(actually, handler mapping level).

e.g.) one controller uses hibernate, but another controller uses different technology.

Filter:

  • use ServletFilter function
  • large granularity to apply
  • configure in web.xml
  • Spring MVC is not required.

* servlet specification

Advertisements

Expose Java5 Enum to JSP(JSTL)

July 28, 2008 at 8:46 pm | Posted in Uncategorized | Leave a comment
Tags:

Currently, Enum cannot be directly referenced in JSTL

We cannot do these:

  • ${ my.package.Enum }
  • ${ someEnum == MyEnum }

So, in order to access enums easily, Enums need to be in HttpServletRequest (request scope) or ServletContext(application scope).

  1. Store in ServletContext (Application scope)
  2. Store in HttpServletRequest (request scope, request.setAttribute())
    • ServletFilter (servlet)
    • HandlerInterceptor (spring)
    • Controller (spring, web-layer-action)

Sample Enum Class

public enum Language {
    EN("English"),
    JP("Japanese");

    private String name;

    Language(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

HandlerInterceptor

public class EnumExposureHandlerInterceptor extends HandlerInterceptorAdapter {

    private Set<Class<? extends Enum>> enumClasses = new HashSet<Class<? extends Enum>>();

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

        for (Class<? extends Enum> enumClass : enumClasses) {
            final String enumClassName = enumClass.getSimpleName();  // class of enum : Language
            final Enum[] enums = enumClass.getEnumConstants();  // enum instances : EN, JP,...

            // put them into map, so that we can access like this : Language.EN, Language.JP ...
            Map<String, Enum> map = new HashMap<String, Enum>(enums.length);
            for (Enum anEnum : enums) {
                map.put(anEnum.name(), anEnum);
            }

            // seting to scope
            request.setAttribute(enumClassName, map);
        }

    }

    public void setEnumClasses(Set<Class<? extends Enum>> enumClasses) {
        this.enumClasses = enumClasses;
    }
}

Spring Configuration

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap" ref="urlMap"/>
<property name="interceptors">
	<list>
            <bean class="mypackage.EnumExposureHandlerInterceptor">
<property name="enumClasses">
                    <set>
                        <value>mypackage.Language</value>
                    </set>
                </property>
             </bean>
         </list>
    </property>
</bean>

Blog at WordPress.com.
Entries and comments feeds.