在jsp web项目中实例化模型对象
我使用 MVC 设计模式编写一个 Web 应用程序。应用程序应该连接到提供业务部分(模型)的 RMI 服务器,
我的问题是,我不知道应该在哪里实例化模型类并连接到 RMI,提供对所有 servlet..我添加了一个 ServletContextListener 并共享了 servlet 上下文的引用,但我认为这不是正确的方法,
提前致谢
Im writing an web application using the MVC design pattern.. the application should connect to a RMI server providing the business part (Model)
my problem is, I dont know where i should instantiate the model class and connect to the RMI, providing access to all servlets.. i added a ServletContextListener and shared the reference on the servlet context, but i dont think that is the right way to do it
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以从控制器实例化模型/业务类。最好在应用程序开始时为每个实例创建一个(仅创建一个)。您也可以对其进行延迟加载。
从 JSP 实例化模型/业务类意味着从视图实例化它们。当然,这违反了 MVC 模式。
您可以将模型/业务类设置为 Singleton,或者将引用保留在 Servlet 上下文中。两者都很好,后者更适合测试。如果您不将构造函数指定为
private
但只是有一个共同的理解,那就是它不适合在任何地方实例化它,那么前者也可以变得测试友好。You can instantiate your Model/Business classes from your Controller. Better still make a single instance (Just Create One) of each in the start of the application. You can also go lazy-loading about it.
Instantiating Model/Business classes from your JSPs means, instantiating them from View. That, of course, violates the MVC pattern.
Either you can make your Model/Business classes as Singleton, or keep the reference in Servlet Context. Both are fine and the latter is more test friendly. The former can be made test friendly too, if you don't specify the constructor as
private
but just have a common understanding that it's not there for instantiating it everywhere.