Spring MVC,设计问题

发布于 2024-08-19 20:18:14 字数 434 浏览 3 评论 0原文

我开始在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风筝在阴天搁浅。 2024-08-26 20:18:14

控制器(在 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.

无言温柔 2024-08-26 20:18:14

如果您还没有这样做,请查看 Spring MVC 分步教程。

Spring MVC 教程

这将为您提供一个很好的示例如何构建一个简单的 Spring MVC Web 应用程序。

唯一的问题是它不包括在 Spring 2.5+ 中使用注释。您必须等待 Spring 3.0 MVC 教程发布,或者亲自深入了解 Spring 官方文档来学习如何使用它。

编辑

这是一个涉及控制表单的示例:

注意:Spring 3.0.0

您的控制器:

@Controller
@RequestMapping("/blog.htm")
public class BlogController{


  // Service layer class
  private final BlogManager blogManager;

  // Inject the blog manager into the constructor automatically
  @Autowired
  public BlogController(BlogManager blogManager){
    this.blogManager = blogManager
  }


  // Set up the form and return the logical view name
  @RequestMapping(method=RequestMethod.GET)
  public String setupForm(ModelMap model){
    model.addAttribute(new EntryForm());
    return "addBlog";
  }

  // Executed when posting the form
  @RequestMapping(method=RequestMethod.POST)
  public String addBlog(@ModelAttribute("entryForm")EntryForm entryForm){
    // Read the entry form command object from the form
    String text = entryForm.getText();

    // Call your service method
    blogManager.addEntry(text);

    // Usually redirect to a new logical view name
    return "redirect:/homepage";
  }  

}

表单命令对象:

public class EntryForm{

  private String text;

  // setters and getters for text
}

这是您的服务层类

public class BlogManager{

  private final BlogDAO blogDAO;

  @Autowired
  public BlogManager(BlogDAO blogDAO){
    this.blogDAO = blogDAO;
  }

  public void addEntry(String text){
    int blogID = 100;  // simple example id for a blog
    Blog blog = blogDAO.findById(blogID);
    blog.addEntry(text);
    blogDAO.update(blog);
  }

}

现在您更新您的 spring.xml 文件以将其全部结合在一起

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


 <!-- Scan for spring annotations -->
 <context:component-scan base-package="test.package"/>

 <!-- defined in the xml file and autowired into controllers and services --> 
 <bean id="blogManager" class="test.package.BlogManager" />
 <bean id="blogDAO" class="test.package.dao.BlogDAO" />

 <!-- Resolves logical view names to jsps in /WEB-INF/jsp folder -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /property>
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>        
</bean>


</beans>

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:

@Controller
@RequestMapping("/blog.htm")
public class BlogController{


  // Service layer class
  private final BlogManager blogManager;

  // Inject the blog manager into the constructor automatically
  @Autowired
  public BlogController(BlogManager blogManager){
    this.blogManager = blogManager
  }


  // Set up the form and return the logical view name
  @RequestMapping(method=RequestMethod.GET)
  public String setupForm(ModelMap model){
    model.addAttribute(new EntryForm());
    return "addBlog";
  }

  // Executed when posting the form
  @RequestMapping(method=RequestMethod.POST)
  public String addBlog(@ModelAttribute("entryForm")EntryForm entryForm){
    // Read the entry form command object from the form
    String text = entryForm.getText();

    // Call your service method
    blogManager.addEntry(text);

    // Usually redirect to a new logical view name
    return "redirect:/homepage";
  }  

}

Form command object:

public class EntryForm{

  private String text;

  // setters and getters for text
}

Here is your service layer class

public class BlogManager{

  private final BlogDAO blogDAO;

  @Autowired
  public BlogManager(BlogDAO blogDAO){
    this.blogDAO = blogDAO;
  }

  public void addEntry(String text){
    int blogID = 100;  // simple example id for a blog
    Blog blog = blogDAO.findById(blogID);
    blog.addEntry(text);
    blogDAO.update(blog);
  }

}

And now you update your spring.xml file to tie it all together

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


 <!-- Scan for spring annotations -->
 <context:component-scan base-package="test.package"/>

 <!-- defined in the xml file and autowired into controllers and services --> 
 <bean id="blogManager" class="test.package.BlogManager" />
 <bean id="blogDAO" class="test.package.dao.BlogDAO" />

 <!-- Resolves logical view names to jsps in /WEB-INF/jsp folder -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /property>
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>        
</bean>


</beans>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文