Spring MVC:基于User-Agent解析视图
Spring 版本:2.5.6
我想根据 User-Agent 标头的值将视图解析为特定的速度文件。
我当前的思路是类似于 UrlBasedViewResolver 的实现,以便根据匹配的正则表达式(键)将用户代理值映射(通过上下文)到特定目录(值)。
我几乎可以肯定有一种更简单的方法。
之前发布过关于基于用户代理的主题确定的类似问题。然而,我的理解是,主题更多地与静态(css、js)内容相关,而不是哪个文件处理实际的响应构造(HTML、XML 等)。
Spring Version: 2.5.6
I want to resolve the view to a specific velocity file based on the value of the User-Agent header.
My current line of thinking is an implementation similar to the UrlBasedViewResolver such that the user-agent value is Map'd (via context) to a specific directory(value) based on a matching a regular expression(key).
I am almost certain there is an easier way.
A similar question was previously posted regarding Theme determination based on User-Agent. However, my understanding is that Themes relate more to static (css,js) content, not which file handles the actual response construction (HTML,XML,etc).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里建议了另一个选项
,但是我解决了扩展 ContentNegotiatingViewResolver 并覆盖 resolveViewName 方法,我将 ViewResolver 称为 HttpHeaderParamViewResolver。扩展方法看起来像这样:
其中 headerParam="User-Agent" (或您喜欢的任何其他 HTTP 标头参数,这是在 bean xml 中定义的),之后您对其进行评估并确定视图名称。在我的例子中,HttpHeaderParamViewResolver 可以使用 Map 进行配置,其中键是要附加到实际 viewName 的前缀,值是用于评估标头参数值的 RegExp。它在应用程序上下文 XML 中看起来像这样:
这样,如果我的控制器调用名为 userDetails 的视图并使用 iPhone 访问应用程序,第一个模式会捕获它并附加 mobile- webkit 后缀,因此视图现在是 mobile-webkit-userDetails,然后传递给生成实际视图的 tilesViewResolver。
我探索了很多可能性,我认为这是我能想到的最简单、最灵活的。在这种情况下,选择完全不同的视图的能力至关重要,因为我们支持多种用户代理,从 WAP 到 iPhone 4 和支持 WebKit 的移动设备,因此视图在不同的用户代理之间发生巨大变化。另一个优点是您不再需要在视图上处理此问题,因为您可以根据需要拥有专业化的视图。另一个好的一面是,您可以很容易地实现这一点,而无需删除或更改您可能已经拥有的视图解析器,因为 ContentNegotiatingViewResolver 能够按照您的特定顺序将视图调用委托给其他视图解析器。定义。
不利的一面是,您可能会试图过度专业化视图,并最终得到大量的视图文件,从而使应用程序成为可维护的噩梦。
希望它有帮助。
There is an other option suggested here
However I resolved Extending a ContentNegotiatingViewResolver and overriding the resolveViewName method, I called my ViewResolver HttpHeaderParamViewResolver. The extended method looks something like this:
Where headerParam="User-Agent" (or any other HTTp Header parameter you like, this is defined in the bean xml), after that you evaluate it and determine the viewName. In My case the HttpHeaderParamViewResolver can be configured with a Map where the key is a prefix to be appended to the actual viewName and the value is a RegExp that will be used to evaluate the value of the header param. It looks something like this in the App Context XML:
That way the if my controller calls a view called userDetails and is accessing the application with an IPhone the first pattern catchs it and appends the mobile-webkit suffix so the view is now mobile-webkit-userDetails and its then passed to the tilesViewResolver which generates the actual view.
I explored a lot of possibilities and I think this is the easiest and most flexible I was able to came up with. In this case the ability to choose an entire different view was critical because we support a wide variety of user agents, from WAP to IPhone 4 and WebKit capable mobiles, so views change dramatically from user agent to user agent. Other advantage is that you no longer require to handle this issue on the view since you can have views as specialized as you like. Other bright side is that you can implement this quite easily without having to remove or change the view resolvers you might already have since ContentNegotiatingViewResolver has the ability to delegate the view call to other view resolvers in the specific sequence you define.
The down side is that you may be tempted to over specialize the views and end up with a ton of view files rendering the app a maintainable nightmare.
Hope it is helpful.
几个月前我也遇到了同样的问题!
在我们的移动项目(使用 Spring 2.5.6)中,我们最终使用带有 SimpleUrlHandler 的拦截器。这捕获了所有传入请求,并将 -m.jsp 添加到任何移动请求的末尾。
它涉及两个步骤:
1)向我们的标准 URL 映射器声明一个拦截器:
2)实现拦截器,该拦截器在用户代理中查找单词“Mobile”。
我在我的博客(关于移动 Web 开发的新的令人兴奋的世界)帖子中更详细地讨论了它:http://plumnash.com/it/iphone-web-development-using-spring/
I had the same problem a few months back!
On our mobile project (using Spring 2.5.6) we ended up using an interceptor with our SimpleUrlHandler. This caught all incoming requests and add -m.jsp to the end of any mobile requests.
It involved two steps:
1) declaring an interceptor to our standard URL Mapper:
and 2) implementing the Interceptor, which looked for the word 'Mobile' in the user-agent.
I talk about it in more detail on my blog (about the new exciting world of mobile web development) post: http://plumnash.com/it/iphone-web-development-using-spring/
不需要在 ViewResolver 中进行配置的替代方案可能涉及顶级 Velocity 文件,然后有条件地解析具有如下内容的子文件。
然而,实现一个新的或扩展现有的 ViewResolver 是一个非常简单的解决方案,也是我会采用的方式。
An alternative that doesn't require configuration in a ViewResolver might involve a top-level Velocity file, then conditionally parsing sub-files that has something like the following.
However, implementing a new or extending an existing ViewResolver is a pretty simple solution and would be the way I'd go with.
我将按照评论中的建议使用自定义视图解析器。 (并将我的应用程序升级到 Spring 3.0.0)
I am going with a custom view resolver as suggested in comments. (and upgrading my app to Spring 3.0.0)