本文介绍了使用哪一个:OpenSessionInViewInterceptor或OpenSessionInViewFilter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难决定使用哪个Open Session In View:使用Spring MVC的拦截器配置OpenSessionInViewInterceptor,或者在web.xml的过滤器中配置OpenSessionInViewFilter?从我研究的内容来看,他们做的事情基本相同,但我试图理解每种类型的区别和用法。



两个是不能在web.xml中使用过滤器的人(比如说servlet 2.2和更早的版本),他们唯一的选择就是使用OpenSessionInViewInterceptor。不知何故,我倾向于拦截器只是因为我必须为我的项目创建一个自定义拦截器,所以我想在Spring MVC配置文件中将所有这些过滤器分组,而不是让web.xml中的OpenSessionInViewFilter和我的自定义Spring MVC配置文件中的拦截器。这真的是一个蹩脚的方式来决定使用哪一个,我的好奇心杀死我在这里。



任何人都可以分享你的想法吗?哪一个你们使用?



谢谢。

解决方案

你说,这两者或多或少是等价的。 Spring为它们提供了这两个选项,以便您可以选择最适合现有应用程序的那个。

如果您使用Spring MVC,那么使用拦截器是有意义的,因为它更容易配置并且更好地与Spring MVC集成。



但是,如果您不使用Spring MVC,并且仅在业务逻辑级别使用Spring,那么拦截器并不是真正的选择,并且过滤器变得更加合适。由于过滤器不是由Spring管理的,因此它们很难配置为与Spring集成,但这是一种折衷。


I'm having a hard time deciding which "Open Session In View" to use: configuring OpenSessionInViewInterceptor using Spring MVC's interceptor with or configuring OpenSessionInViewFilter in web.xml's filter? From what I have researched, they do pretty much the same thing, but I'm trying to understand the difference and the usage of each type.

The biggest difference between the two is for folks who can't use a filter in web.xml (say, servlet 2.2 and earlier), their only option is to use OpenSessionInViewInterceptor. Somehow, I'm leaning towards the interceptor simply because I have to create a custom interceptor for my project, so I'm thinking of grouping all these "filters" in Spring MVC config file rather than having OpenSessionInViewFilter in web.xml and my custom interceptor in Spring MVC config file. It's really a lame way to decide which one to use, and my curiosity kills me here.

Can anyone share your thoughts about this? Which one do you guys use?

Thanks.

解决方案

As you say, the two are more or less equivalent. Spring provides them both so that you can pick the one that fits best with your existing application.

If you use Spring MVC, then it makes sense to use the interceptor, since it's easier to configure and better integrates with Spring MVC.

However, if you don't use Spring MVC, and only use Spring at the business-logic level, then the interceptor isn't really an option, and the filter becomes more appropriate. Because filters are not managed by Spring, they're harder to configure to integrate with Spring, but that's the trade-off.

这篇关于使用哪一个:OpenSessionInViewInterceptor或OpenSessionInViewFilter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 00:34