在类似的Web应用程序之间共享Web层代码(控制器和JSP)的最佳实践 [英] Best practices for sharing web-tier code (Controllers and JSPs) between similar web apps

查看:98
本文介绍了在类似的Web应用程序之间共享Web层代码(控制器和JSP)的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重写一些老化的Web应用程序.特别是有两个非常非常相似,但是今天没有共享任何代码,我打算解决这个问题.

I'm working on rewriting some aging web applications. There are two in particular that are very, very similar, yet share no code today and I aim to fix that.

正在使用Maven,Spring MVC和Sitemesh重写项目.

The projects are being rewritten with Maven, Spring MVC, and Sitemesh.

模型层代码很容易使用JAR共享.但是我不知道在相似的应用程序之间共享通用Web层代码(JSP和控制器)的任何好方法.

Model tier code is easy enough to share using JARs. But I don't know of any good ways to share common web-tier code (JSPs and Controllers) between similar apps.

这里有一些背景.这些应用是网上商店.一个是普通商店(think amazon.com),用户可以登录该商店,搜索产品,添加到购物车并结帐.另一个基本上是相同的,只是它是一个打孔站点.产品浏览和购物车部分相同.但是,登录和签出是完全不同的.

Here's some background. These apps are webstores. One is a normal store (think amazon.com) that a user can sign into, search products, add to a shopping cart, and check out. The other is basically the same thing, only it's a punchout site. The product browse and shopping cart portions are identical. Sign in and checkout, however, are completely different.

我简化了,但这足以说明问题.在产品浏览和购物车部分中,有很大一部分网络层代码应该可以在两者之间共享.

I'm oversimplifying, but it's enough to illustrate the problem. There's a significant portion of web-tier code in the product browse and shopping cart sections that should be able to be shared between the two.

我认为不可能仅基于环境变量或来自其他数据库的设置而以模式"运行相同的WAR文件.差异之一是完全不同的Spring Security配置.最好还是将另一个站点的登录和签出控制器排除在组件扫描之外,以使任何人都无法通过URL操作以某种方式越过错误的站点.

I don't think it's possible to simply have the same WAR file running as either "mode" based on an environment variable or settings from a different database. One of the differences is a completely different Spring Security config. It would also be preferable to leave the other site's sign-in and checkout controllers out of the component-scan so nobody can somehow cross over to the wrong one with URL manipulation.

我最初开始使用Maven配置文件并进行过滤,以将两个不同的配置集(web.xml,spring config等)保留在同一WAR项目中.根据选择的Maven配置文件,使用不同的配置集(为清楚起见,使用不同的名称)构建生成的WAR.这违反了一个pom会产生一个工件的Maven原则.

I originally started using Maven Profiles and filtering to keep two different config sets (web.xml, spring configs, etc.) in the same WAR project. Based on which Maven profile is selected, the resulting WAR is built with a different config set (and a different name for clarity). This violates the Maven principal that one pom produces one artifact.

是否有更好的方法可以做到这一点? Maven WAR叠加层如何?我看到有人在谈论使用叠加来共享通用资源,例如CSS,JS,图像,甚至某些通用JSP.但是我看不到有人提到以这种方式共享控制器之类的类.

Is there a better way to do this? What about Maven WAR Overlays? I see people talking about using overlays to share common resources like CSS, JS, images, and even some common JSPs. But I don't see anyone mention sharing classes like Controllers this way.

我可以将Controller类下推到JAR,但是从逻辑上讲,似乎它们应该保留在各自的JSP中.而且,JSP也不能下推到JAR(对吗?).

I could push the Controller classes down to the JARs, but logically it seems they should stay with their respective JSPs. And JSPs can't also be pushed down to the JARs (right?).

我还考虑过使其成为一个包含多个WAR文件的EAR-一个WAR用于常见的购物体验,另一个WAR用于适当的登录和结帐.我相信可以在同一个EAR中的两个WAR之间共享会话,但是我不确定它是否可以与Spring的会话范围bean配合使用.我听说它们没有真正存储在会话中.我还必须弄清楚如何处理用于页眉/页脚的Sitemesh装饰器.需要将相同的Sitemesh配置及其资源复制到两个WAR中,对吗?因此,最终,购物WAR工件在每种情况下都将有所不同.

I also thought about making it an EAR containing multiple WAR files -- one WAR for the common shopping experience, and another WAR for the appropriate sign-in and checkout. I believe the session can be shared between two WARs in the same EAR, but I'm not sure if it plays nice with Spring's session-scope beans. I hear they're not really stored in the session. I'd also have to figure out what to do about the Sitemesh decorators being used for the header/footer. The same Sitemesh config and its resources would need to be copied into both WARs, right? So in the end, the shopping WAR artifact would still be different in each circumstance.

我必须相信其他人以前已经处理过这个问题.我是否在以错误的方式思考?这类事情有通用的解决方案吗?

I have to believe that other people have dealt with this before. Am I thinking about it the wrong way? Is there a common solution for this sort of thing?

推荐答案

做好与复制粘贴作战的工作.为什么说很难共享JSP?您可以使用maven依赖插件将它们从共享jar中复制出来:

Good work doing battle against copy-paste. Why do you say it's hard to share JSPs? You can copy them out of a shared jar using the maven dependency plugin:

 <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-dependency-plugin</artifactId>
         <version>2.4</version>
         <executions>
           <execution>
             <id>unpack</id>
             <phase>package</phase>
             <goals>
               <goal>unpack</goal>
             </goals>
             <configuration>
               <artifactItems>
                 <artifactItem>
                   <groupId>com.example</groupId>
                   <artifactId>webapp-common</artifactId>
                   <version>1.0-SNAPSHOT</version>
                   <outputDirectory>[target jsp directory]</outputDirectory>
                   <includes>**/*.jsp</includes>
                 </artifactItem>
               </artifactItems>
             </configuration>
           </execution>
         </executions>
       </plugin>

这篇关于在类似的Web应用程序之间共享Web层代码(控制器和JSP)的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆