如何在 Java Web 应用程序之间共享通用组件?
环境
- IDE:Netbeans 6.9
- 应用服务器:Glassfish 3
- 框架:Spring、Hibernate、Struts 2
问题
我有 2 个 Web 应用程序。我想在它们之间共享资源 - 即身份验证表单 jsp 和其他资产(js - yui、jquery/images/css)。我将添加更多也需要访问这些通用组件的 Web 应用程序。
作为最后的手段,我将创建另一个仅包含公共资产的 Web 项目 - 通过 http://common.mydomain 包括它们。
其他人是如何实现这一目标的?我可以创建一个 Web 资源 jar/war 并将其包含在两个应用程序中吗?
我正在尝试,因此会根据任何进展更新问题。
非常感谢。
Environment
- IDE: Netbeans 6.9
- App Server: Glassfish 3
- Frameworks: Spring, Hibernate, Struts 2
Problem
I have 2 web applications. I want to share resources between them both - i.e. authentication form jsp and other assets (js - yui,jquery/images/css). I will be adding more web applications that will also require access to these common components.
As a last resort I will create another web project with just the common assets - including them via http://common.mydomain.
How has anyone else achieved this? Can I create a web resource jar/war and include this in both applications?
I am experimenting so will update question with any progress.
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将这些“通用”组件放入单独的模块(或多个单独的模块)中,并从应用程序的其他部分建立对该模块的依赖关系。 (我不确定它在 NetBeans 中是如何调用的,在 IntelliJ IDEA 中它被称为模块,在 Eclipse 中它被称为模块 - 它是同一工作区中的一个单独的项目)。
Place these 'common' components in a separate module (or in several separate modules) and make a dependency to this module from other parts of your app. (I'm not sure how is it called in NetBeans, in IntelliJ IDEA it's called module, in Eclipse - it's a separate project in the same workspace).
在 Eclipse 中,您必须创建一个实用程序项目和一个包含实用程序项目和当前 Web 应用程序的 Ear 项目。然后,您可以将通用代码移至实用程序项目,并将耳朵部署到 glassfish。
公共资源必须通过类路径定位。这对于 JSF 2 来说相当容易
In eclipse you must create an utility project and an ear project containing the utility project and your current web applications. You can then move common code to the utility project, and deploy the ear to glassfish.
Common resources must be located through the classpath. This is rather easy with JSF 2
Netbeans 有一个功能可以做到这一点。仅为您的共享组件创建一个新项目。将项目列为需要它的项目的依赖项,并确保在构建过程中这两个项目都在 IDE 中打开。
与 IDE 无关的方式(我在自己的项目中使用。)
将是:
(如果您是唯一的开发人员,您也许可以使用
mvn install
来解决这个问题)Netbeans has a feature to do this. Create a new project just for your shared components. List the project as a dependency of the projects that need it and make sure both projects are open in the IDE during the build process.
The IDE agnostic way(which I'm using in my own projects.)
would be to:
(if you're the only dev, you might be able to get around this with
mvn install
)