Spring MVC,设计问题
我开始在 Java EE 和 Spring 下进行 Web 开发,并有一个高级设计问题:
我设置了一个基本的 shell:我编写了一些域对象(主要是各种对象属性的 getter 和 setter,我最终希望将其显示在页)。我想知道一旦我编码了一些 DAO,将所有内容联系在一起的正确方法是什么。
假设一个简单的博客包含各种博客条目。我已经为博客条目编写了一个类,其中包含与条目相对应的适当属性。控制器访问这些域对象、调用适当的 DAO、将数据打包到模型中并调用视图的适当流程是什么?
正如您所知,我对这一切如何组合在一起以及分层架构应该如何工作感到非常困惑。如果需要任何说明,请告诉我。谢谢。
更新:感谢您的以下回答!我还有另一个问题:我的服务层类应该是单例吗?有人可以解释一下为什么他们应该/不应该是单身人士吗?谢谢!
I am starting out with web development under Java EE and spring and have a high level design question:
I have a basic shell set up: I have coded some domain objects (mainly getters and setters for various object attributes which I ultimately want displayed on the page). I am wondering what the proper way to tie everything together is once I have some DAOs coded.
Assuming a simple blog with various blog entries. I have coded a class for the blog entries containing the appropriate attributes corresponding to an entry. What is the appropriate flow for the controller to access these domain objects, call the appropriate DAO, pack the data into the model and invoke the view?
As you can tell I am pretty confused as to how this all fits together, and how tiered architectures should work. Please let me know if any clarification is needed. Thanks.
Update: Thanks for the answers below! I have another question: should my service layer classes be singletons? Could someone please explain why they should/shouldn't be singletons? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
控制器(在 2.5+ 中使用注释,使其成为 POJO)在注入的服务层类上执行方法。服务层注入了 DAO 以从持久性中获取对象。业务逻辑位于域对象内部。
Controller (use Annotations in 2.5+ so that it's a POJO) performs a method on a Service Layer class, which is injected. The Service Layer is injected with a DAO to get objects from persistence. The business logic is inside of the Domain Objects.
如果您还没有这样做,请查看 Spring MVC 分步教程。
Spring MVC 教程
这将为您提供一个很好的示例如何构建一个简单的 Spring MVC Web 应用程序。
唯一的问题是它不包括在 Spring 2.5+ 中使用注释。您必须等待 Spring 3.0 MVC 教程发布,或者亲自深入了解 Spring 官方文档来学习如何使用它。
编辑
这是一个涉及控制表单的示例:
注意:Spring 3.0.0
您的控制器:
表单命令对象:
这是您的服务层类
现在您更新您的 spring.xml 文件以将其全部结合在一起
If you haven't done so already, check out the Spring MVC step-by-step tutorial.
Spring MVC tutorial
This will give you an excellent example of how to construct a simple Spring MVC web application.
The only problem is it doesn't cover using Annotations in Spring 2.5+. You'll have to either wait for the Spring 3.0 MVC tutorial to release, or dig through the official Spring documentation yourself to learn how to use it.
EDIT
Here's an example involving controlling a form:
Note: Spring 3.0.0
Your controller:
Form command object:
Here is your service layer class
And now you update your spring.xml file to tie it all together