javahibernateweb.xmlopen-session-in-view

How to make OpenSessionInViewFilter exclude static resources


I have implemented the OpenSessionInViewFilter for my MVC webapp and it works almost perfect. Only problem is that it also creates a session for every image, js, css etc that are requested from the webserver. This i dont want.

Im using struts2, spring and hibernate and this is my web.xml

<filter>
    <filter-name>lazyLoadingFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

So because i am mapping url-pattern /* it also takes all the images etc.. I tried setting it to *.jsp, and *.action, but then i get the lazyloading-exceptions again... How should i do this? I've been looking for answers for 5 hours now and im getting a litle bit crazy in my head.

All i need to do is to make this filter IGNORE all the static resources. Thats it! And for everything else it can run. It sounds so simple, but its really really annoying me that i cant figure out how.

Any help would be greatly appriciated.

Do i need to extend the filter to write my own filter and exclude within it? And if so. How?

EDIT: It seems like i could set up filter-mappings for my static files at the top of the filter-chain. And then send those to a "ByPassFilter", thus bypassing the filterchain for these static resources. Is this the way to go??

Thanks guys!


Solution

  • As far as your implementation of having a Bypassfilter is considered , if you have such a filter in front then once you skip the next filter in filter chain then basically the rest of filters in the chain will also get skipped (which doesn't seem to be a desirable thing to do in most situations ) . Also as the filter invocations for a request behave like

    Filter1 -> Filter2 -->Struts Action/BL --> Filter 2 -->Filter 1

    Hence the OpenSessionInViewFilter will kick in after the request is processed in your struts action ( which can be avoided by placing another bypass filter after open session in view filter in web.xml ) . However overall it has always appeared undesirable to me to skip the entire filter chain for skipping a single filter .

    I haven't ever faced a need to skip OpenSessionInViewFilter ever , however if i had to do it then instead of having a Bypassfilter i would have a filter extending the OpenSessionInViewFilter filter which would be skipping my static resources from processing .