如何在我的 Spring MVC 网站中提取常见的视图代码片段
这是我的问题:
我的 Spring MVC 网站将严重依赖外部 JavaScript/CSS/图像。该域需要是可配置的。另外,我希望能够将页眉和页脚等部分放入可以包含的 JSP 中。将图像添加到 JSP 时,我需要能够使用可配置的外部静态内容域生成图像的链接。
到目前为止我的解决方案:
到目前为止,我认为我可以使用 JSP 模板的组合来加载输出我的配置值的配置类(以便 JSP 可以使用这些配置值)。我想我真正遇到的唯一问题是,现在我要求所有 JSP 导入模板,然后导入这些配置类以访问静态内容 URL。
我的问题:
上面概述的解决方案有意义吗?这真的是最好的方法吗?人们通常采取什么措施来解决这个问题?还有哪些其他解决方案可用于抽象和重用 JSP 组件?
注释:
- 我正在使用 Spring MVC 3...我对 Spring 和 MVC 总体来说有点陌生
- 这不是一个太大的网站... 也不会被完整使用 生产环境。
Here is my problem:
My Spring MVC website is going to rely a lot on external JavaScripts/CSS/Images. This domain needs to be configurable. Also, I'd like to be able to throw pieces such as my header and footer into JSPs that can be included. When adding an image to a JSP, I need to be able to generate a link to the image using the configurable external static content domain.
My Solution so far:
So far I'm thinking I can use a combination of JSP templates which will load configuration classes which output my configuration values (so that the JSPs can use these configuration values). I guess the only problem I really have with this is that now I require all of my JSPs to import the template and then also import those configuration classes to access the static content URL.
My Question:
Does my solution outlined above make sense? Is it really the best approach? What have people generally done to solve this problem? What other solutions are available to abstracting and reusing JSP components?
Notes:
- I'm using Spring MVC 3... I'm a bit new to Spring and MVC in general
- This isn't an overly large website...
nor will it be used in a full
production environment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring MVC 在C 部分非常出色,在M 部分也相当不错,但在V 部分却有点乏善可陈。它将所有视图职责委托给其他框架,通常是普通 JSP/Freemarker/Velocity。在大多数情况下这很好,因为这些视图框架提供了大量用于组装复合视图的机制。不过,它们非常基本。
然而,有时您想要更严格的东西,这就是 Tiles 或 Sitemesh 出现。这些框架专门从组件中组装 Web 视图,并且大多与它们所使用的 MVC 框架无关。
Spring MVC 既不会帮助也不会阻碍您使用这些框架,并且它们彼此集成得很好(SiteMesh 可以,无论如何,我对 Tiles 不太熟悉,但我听说它做得很好)。
Spring MVC is excellent on the C part, pretty good on the M, but but a bit of a no-show on the V. It delegates all view responsibilities to other frameworks, usually vanilla JSP/Freemarker/Velocity. This is fine in most cases, since those view frameworks provide plenty mechanisms for assembling composite views. They're pretty basic, though.
However, sometimes you want something more rigorous, which is where the likes of Tiles or Sitemesh come in. These frameworks specialise in assembling web views from component parts, and are mostly agnostic of the MVC framework that they work with.
Spring MVC will neither help nor hinder your use of these frameworks, and they integrate with each other pretty well (SiteMesh does, anyway, I'm not that familiar with Tiles, but I hear it does the job well).